Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regularly purge stale Resque workers on Heroku?

I've got Resque workers that typically shouldn't take longer than about 1-5 minutes to run, but frequently those workers will get "stuck" and go idle, clogging up workers and doing nothing.

So I'd like to regularly check for workers that have been running longer than X time and purge them. But I need to do this automatically, so I don't have to personally go in and manually clear them (Resque.workers.each {|w| w.unregister_worker}) every few hours.

This needs to work on Heroku.

like image 756
Shpigford Avatar asked Jun 13 '13 14:06

Shpigford


1 Answers

Put this into a rake task:

allocated_time = 60 * 60 # 1 hour
Resque::WorkerRegistry.working.each do |worker|
  if (worker.started <=> Time.now - allocated_time) < 1
    worker.unregister
  end
end

Use heroku scheduler, you can set it to minimum of 10 minutes if that suites.

like image 149
Marc Greenstock Avatar answered Sep 28 '22 10:09

Marc Greenstock