How to avoid Rufus scheduler being called as many

2019-06-10 08:36发布

问题:

I am running a rails app in Heroku where I use Rufus Scheduler.

My app uses more than one dyno and the scheduler is running on each dyno rather than just one, so my scheduled events are firing multiple times (once for each dyno).

How can I avoid this?

回答1:

You should add a new process type in your Procfile that runs your Rufus scheduler process:

web: unicorn -c app/config/unicorn.rb ... # Your existing web dyno process
scheduler: rake rufus:scheduler # Add rake task for rufus scheduler process

You can then use heroku ps:scale to set one scheduler dyno and N web dynos.

EDIT

You have a Rails application which currently contains your rufus scheduler. When you deploy that application to Heroku, your Rails application code and required libraries are installed on each dyno (via one or more buildpacks). The dyno is a virtual machine that runs a process within your application.

By default, you have a dyno that runs a Rails web app server. If you add a second type of dyno, it will have the same codebase and libraries, but run a different process. In this case, that second process will be your Rufus scheduler process.