Skip to content

Instantly share code, notes, and snippets.

@mikeabiezzi
Created June 25, 2012 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeabiezzi/2986068 to your computer and use it in GitHub Desktop.
Save mikeabiezzi/2986068 to your computer and use it in GitHub Desktop.
Resque: Trap TERM, stop processing job, and re-enqueue the job.
03:54+00:00 app[worker.1]: *** got: (Job{jobs} | Job | [1, 100])
03:55+00:00 app[worker.1]: Processed 1
03:56+00:00 app[worker.1]: Processed 2
03:57+00:00 app[worker.1]: Processed 3
03:58+00:00 app[worker.1]: Processed 4
03:59+00:00 app[worker.1]: Processed 5
04:00+00:00 app[worker.1]: Processed 6
04:01+00:00 app[worker.1]: Processed 7
04:02+00:00 app[worker.1]: Processed 8
04:03+00:00 app[worker.1]: Processed 9
04:03+00:00 heroku[worker.1]: State changed from up to stopping
04:04+00:00 heroku[api]: Scale to web=1, worker=0 by mike@quickleft.com
04:04+00:00 app[worker.1]: Processed 10
04:05+00:00 app[worker.1]: Processed 11
04:06+00:00 heroku[worker.1]: Stopping all processes with SIGTERM
04:06+00:00 app[worker.1]: Processed 12
04:06+00:00 app[worker.1]: *** Exiting...
04:07+00:00 app[worker.1]: Processed 13
04:07+00:00 app[worker.1]: Enqueued Job (14,100)
04:07+00:00 app[worker.1]: *** done: (Job{jobs} | Job | [1, 100])
04:07+00:00 heroku[worker.1]: Stopping all processes with SIGTERM
04:09+00:00 heroku[worker.1]: Process exited with status 0
class Job
class << self
attr_accessor :stop
end
@queue = :jobs
def self.perform from, through
(from..through).each do |item|
if Job.stop
Resque.enqueue Job, item, through
puts "Enqueued Job (#{item},#{through})"
return
end
sleep 1
puts "Processed #{item}"
end
end
end
trap('TERM') do
Job.stop = true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment