Last active
November 19, 2016 21:23
-
-
Save gbuesing/e2f0b94ae3d2f9b0c6848b82ace51201 to your computer and use it in GitHub Desktop.
Transloadit blocking assembly runner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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