Decide whether cronjobs should run on one or all servers

Posted Over 6 years ago. Visible to the public. Repeats.

Understanding your type of cronjob

Some cronjobs must only run on a single server. E.g. when you run nightly batch operations on the database, it should probably run on a single server. Running it on multiple servers would likely result in deadlocks or corrupt data.

Some cronjobs must always run on all servers. E.g. starting a sidekiq process on reboot.

Configuring whenever

If not configured otherwise, cronjobs defined in whenever's Show archive.org snapshot schedule.rb will only be written to the crontab of the server with the db role in deploy.rb.

If there are cronjobs that should run on all servers, you will have to change the default setting of whenever_roles.

What works well for us is using the roles cron and primary_cron:

  • cron for all jobs that should run on all servers
  • primary_cron for jobs that should run only on one server

From the docs Show archive.org snapshot :

If a server's role is listed in the whenever_roles, then it will have all jobs added to its crontab that either list that role in their :roles arg or that don't have a :roles arg.

Example

production.rb

server "server1.example.com", :app, :web, :db, :cron, :primary_cron, primary: true
server "server2.example.com", :app, :web, :cron

deploy.rb

set :whenever_roles, %i[cron primary_cron]

schedule.rb

every :reboot do
  rake 'sidekiq:start', output: { standard: nil }
end

every 10.minutes, at: 2, roles: [:primary_cron] do
  bundle_exec 'bin/jobs/my_single_server_job'
end

Note: If sidekiq does not run on all servers, you could use a dedicated role sidekiq for the restart entry.

Last edit
5 months ago
Felix Eschey
License
Source code in this card is licensed under the MIT License.
Posted to makandra dev (2018-01-12 13:53)