jam-cloud/ruby/lib/jam_ruby/resque/jam_tracks_builder.rb

76 lines
2.1 KiB
Ruby

require 'json'
require 'resque'
require 'resque-retry'
require 'net/http'
require 'digest/md5'
module JamRuby
class JamTracksBuilder
extend JamRuby::ResqueStats
@queue = :jam_tracks_builder
def log
@log || Logging.logger[JamTracksBuilder]
end
attr_accessor :jam_track_right_id
def self.perform(jam_track_right_id)
jam_track_builder = JamTracksBuilder.new()
jam_track_builder.jam_track_right_id = jam_track_right_id
jam_track_builder.run
end
def run
begin
log.info("jam_track_builder job starting. jam_track_right_id #{jam_track_right_id}")
begin
@jam_track_right = JamTrackRight.find(jam_track_right_id)
# bailout check
if @jam_track_right.signed
log.debug("package is already signed. bailing")
return
end
# track that it's started ( and avoid db validations )
JamTrackRight.where(:id => @jam_track_right.id).update_all(:signing_started_at => Time.now, :should_retry => false)
JamRuby::JamTracksManager.save_jam_track_right_jkz(@jam_track_right)
length = @jam_track_right.url.size()
md5 = Digest::MD5.new
@jam_track_right.finish_sign(length, md5.to_s)
log.info "Signed jamtrack to #{@jam_track_right[:url]}"
rescue Exception => e
# record the error in the database
post_error(e)
# and let the job fail, alerting ops too
raise
end
end
end
# set @error_reason before you raise an exception, and it will be sent back as the error reason
# otherwise, the error_reason will be unhandled-job-exception
def post_error(e)
begin
# if error_reason is null, assume this is an unhandled error
unless @error_reason
@error_reason = "unhandled-job-exception"
@error_detail = e.to_s
end
@jam_track_right.finish_errored(@error_reason, @error_detail)
rescue Exception => e
log.error "unable to post back to the database the error #{e}"
end
end
end
end