Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry MediaConvert operations using retriable #84

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions active_encode.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "rails"
spec.add_dependency "retriable"

spec.add_development_dependency "aws-sdk-cloudwatchevents"
spec.add_development_dependency "aws-sdk-cloudwatchlogs"
Expand Down
24 changes: 22 additions & 2 deletions lib/active_encode/engine_adapters/media_convert_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'aws-sdk-cloudwatchlogs'
require 'aws-sdk-mediaconvert'
require 'file_locator'
require 'retriable'

require 'active_support/json'
require 'active_support/time'
Expand Down Expand Up @@ -60,6 +61,26 @@ def initialize(msg = nil, encode = nil)
end
end

class RetriableClient
def initialize
@retry_params = {on: Aws::MediaConvert::Errors::ServiceError, base_interval: 1}
endpoint = Retriable.retriable(@retry_params) { Aws::MediaConvert::Client.new.describe_endpoints.endpoints.first.url }
@client = Aws::MediaConvert::Client.new(endpoint: endpoint)
end

def method_missing(method, *args, &block)
if @client.respond_to?(method)
Retriable.retriable(@retry_params) { @client.send(method, *args, &block) }
else
super
end
end

def respond_to?(method)
@client.respond_to?(method)
end
end

attr_accessor :role, :output_bucket
attr_writer :log_group, :queue

Expand Down Expand Up @@ -285,8 +306,7 @@ def cloudwatch_logs
end

def mediaconvert
endpoint = Aws::MediaConvert::Client.new.describe_endpoints.endpoints.first.url
@mediaconvert ||= Aws::MediaConvert::Client.new(endpoint: endpoint)
@mediaconvert ||= RetriableClient.new
end

def s3_uri(url, options = {})
Expand Down
23 changes: 23 additions & 0 deletions spec/integration/media_convert_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,27 @@ def reconstitute_response(fixture_path)
end
end
end

describe "retry" do
it "recovers from Aws::MediaConvert errors" do
mediaconvert.stub_responses(:get_job, [
Aws::MediaConvert::Errors::TooManyRequestsException.new(nil, 'Too Many Requests'),
reconstitute_response("media_convert/job_progressing.json")
])
expect(ActiveEncode::Base.find(job_id)).to eq(running_job)
end

it "raises when the retries run out" do
mediaconvert.stub_responses(:get_job, [
Aws::MediaConvert::Errors::TooManyRequestsException.new(nil, 'Too Many Requests 1'),
Aws::MediaConvert::Errors::TooManyRequestsException.new(nil, 'Too Many Requests 2'),
Aws::MediaConvert::Errors::TooManyRequestsException.new(nil, 'Too Many Requests 3'),
reconstitute_response("media_convert/job_progressing.json")
])
expect { ActiveEncode::Base.find(job_id) }.to raise_error do |error|
expect(error).to be_a(Aws::MediaConvert::Errors::TooManyRequestsException)
expect(error.message).to eq('Too Many Requests 3')
end
end
end
end