Skip to content

Instantly share code, notes, and snippets.

@ncri
Created Oct 3, 2012
Embed
What would you like to do?
Delayed::Worker.backend = :active_record
Delayed::Worker.max_attempts = 1
module Delayed
class Worker
# Do num jobs and return stats on success/failure.
# Exit early if interrupted.
def work_off(num = 100)
success, failure = 0, 0
num.times do
case reserve_and_run_one_job
when true
success += 1
when false
failure += 1
else
break # leave if no work could be done
end
break if $exit # leave if we're exiting
end
if Delayed::Job.count == 0 and !Rails.env.development?
@client ||= ::Heroku::Client.new(ENV['HEROKU_USER'], ENV['HEROKU_PASSWORD'])
@client.ps_scale(ENV['APP_NAME'], type: 'worker', qty: 0)
end
return [success, failure]
end
end
module MessageSending
def delay(options = {})
unless Rails.env.development?
@client ||= ::Heroku::Client.new(ENV['HEROKU_USER'], ENV['HEROKU_PASSWORD'])
@client.ps_scale(ENV['APP_NAME'], type: 'worker', qty: 1)
end
DelayProxy.new(PerformableMethod, self, options)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment