Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rufus scheduler implementation in rails 3

I have an app running with apache + passenger in production. Currently I initialize the rufus scheduler in a initializer and register jobs reading from a db in that initializer. Way apache/passenger works is that it creates multiple process/instance of the app which causes the scheduler to get initialized multiple times and will schedule duplicate jobs.

What is the correct of implementing this so that the scheduler is a singleton object?

like image 203
ed1t Avatar asked Oct 10 '22 16:10

ed1t


1 Answers

You probably want to implement Rufus Scheduler as a separate worker process outside your application.

Instead of putting it as an initializer, I would implement a Rake task that starts it.

# Rakefile
desc "Starts the Scheduler worker"
task :scheduler do
  require 'path/to/your/scheduler/file'

  scheduler.join
end

Then just run rake scheduler to start it in the background.


Bonus: Since your app now needs 2 processes side by side, use Foreman to manage the multiple processes of your application. You can do this by creating a file called Procfile:

# Procfile
web:       thin start -p 4242
scheduler: rake scheduler

Then start your app with Foreman: (be sure to gem install foreman first)

$ foreman start

This will invoke both processes simultaneously.

like image 171
Rico Sta. Cruz Avatar answered Oct 14 '22 03:10

Rico Sta. Cruz