-
-
Save ahoward/cf2ace8aeb2981db2059 to your computer and use it in GitHub Desktop.
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
# this code _must_ return a hash since the result hash is used to log information about the job run. | |
# | |
# by using catch/throw via 'return_result' it's easy to control the return value while still decorating | |
# it with key info, such as 'elapsed' time. | |
# | |
def run | |
# | |
result = | |
Map.new( | |
:job => '', | |
:placard => {}, | |
:status => '' | |
) | |
return_result = lambda{|*_| throw :result, result.merge!(_.shift || {})} | |
# | |
begin | |
# | |
start = Time.now | |
data = nil | |
params = nil | |
placard = nil | |
catch(:result) do | |
# | |
s3_job = self | |
bucket = s3_job.bucket | |
key = s3_job.key | |
uuid = Placard.pluck_uuid(key) || raise("no uuid found in #{ key.inspect }") | |
# | |
job = bucket.objects[key] | |
url = job.public_url.to_s.split('?').first | |
result[:job] = url | |
# | |
unless s3_job.lock(:blocking => false) | |
return_result[:status => 'locked'] | |
end | |
# | |
begin | |
# relay data from s3 and nuke it up there to un-clog the s3 job queue... | |
# | |
unless s3_job.data.present? | |
unless job.exists? | |
return_result[:status => 'missing'] | |
end | |
begin | |
buf = job.read | |
data = JSON.parse(buf) | |
rescue AWS::S3::Errors::NoSuchKey | |
return_result[:status => 'no-key'] | |
else | |
try_hard do | |
buf = job.read | |
data = JSON.parse(buf) | |
end | |
end | |
if data | |
s3_job.set(:data, data) | |
end | |
end | |
# | |
unless((data = s3_job.data).present?) | |
return_result[:status => 'no-data'] | |
end | |
# bail if the placard we are about to make already exists | |
# | |
if((placard = Placard.where(:uuid => uuid).first)) | |
s3_job.set(:placard_id, placard.id) | |
return_result[:status => 'existing', :placard => {:uuid => placard.uuid, :url => placard.url}] | |
end | |
# grab the placard params indicated by the job | |
# | |
try_hard do | |
params = JSON.parse(URI.parse(data['placard_url']).read) | |
end | |
# move the placard image into a canonical location. eg: /u/42/original/image.png | |
# | |
if( (image_s3_url = params['image_s3_url']).present? ) | |
uri = Addressable::URI.parse(image_s3_url.split('?').first) | |
path = uri.path | |
src_key = path.sub(%r{^/*#{ bucket.name }/*}, '') | |
dst_key = Upload.next_s3_key!(image_s3_url) | |
src = bucket.objects[src_key] | |
raise "#{ src_key } does not exist!?" unless src.exists? | |
try_hard do | |
src.copy_to(dst_key, :acl => :public_read, :cache_control => "max-age=#{ 15.minutes }, public") | |
end | |
dst = bucket.objects[dst_key] | |
params['url'] = dst.url_for(:read).to_s.split('?').first | |
end | |
# make the damn thing | |
# | |
map = try_hard do | |
Map.for(Placard.create_from_params(params)) | |
end | |
placard = Placard.find(map.get(:placard, :id)) | |
s3_job.set(:placard_id, placard.id) | |
return_result[:status => 'success', :placard => {:uuid => placard.uuid, :url => placard.url}] | |
ensure | |
s3_job.unlock | |
end | |
end | |
ensure | |
result[:elapsed] = (Time.now - start).to_f.round(3) | |
if((e = $!)) | |
result[:error] = "#{ e.message }(#{ e.class }) @ #{ Array(e.backtrace).first || '?' }" | |
end | |
end | |
# | |
return result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment