Skip to content

Instantly share code, notes, and snippets.

@gbuesing
Last active November 19, 2016 21:23
Show Gist options
  • Save gbuesing/e2f0b94ae3d2f9b0c6848b82ace51201 to your computer and use it in GitHub Desktop.
Save gbuesing/e2f0b94ae3d2f9b0c6848b82ace51201 to your computer and use it in GitHub Desktop.
Transloadit blocking assembly runner
# Blocks until response received
# Designed to be run from a background job, not in request-response
# Usage:
# assembly = Transloadit::Rails::Engine.template :my_template
# response = TransloaditBlockingAssemblyRunner.run assembly
# results = response['results']
module TransloaditBlockingAssemblyRunner
class Error < StandardError; end
class TimeoutError < Error; end
class AssemblyError < Error; end
MAX_RESPONSE_CHECKS = 60 * 5
def self.run assembly
response = assembly.submit!
response_checks = 0
until response.finished?
response_checks += 1
if response_checks > MAX_RESPONSE_CHECKS
raise TimeoutError, "MAX_RESPONSE_CHECKS exceeded"
end
sleep 1
response.reload!
end
if response.error?
raise AssemblyError, response['message']
else
response
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment