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?
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.
Then just run
rake schedulerto 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:Then start your app with Foreman: (be sure to
gem install foremanfirst)This will invoke both processes simultaneously.