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.
# 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With