5

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?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
ed1t
  • 8,719
  • 17
  • 67
  • 110

1 Answers1

4

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.

Rico Sta. Cruz
  • 526
  • 5
  • 14