Posted 11 months ago. Visible to the public. Repeats.

Decide whether cronjobs should run on one or all servers

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 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:

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

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

deploy.rb

Copy
set :whenever_roles, %i[cron primary_cron]

schedule.rb

Copy
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.

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Judith Roth
Last edit:
11 months ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Judith Roth to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more