← Back to all snippets

How to run whenever cron jobs for a specific environment

David Anderson | May 15, 2019

When you only want to run whenever gem cron tasks for a specific environment.

Add a production_cron and a staging_cron to your config/deploy files and set the whenever_roles variable in `config/deploy.rb’

server 'deploy@', user: 'deploy', roles: %w{web app db production_cron}

server 'deploy@321.321.1.1', user: 'deploy', roles: %w{web app db staging_cron}

set :whenever_roles, -> { "#{fetch(:stage)}_cron" }
set :whenever_environment, -> { fetch(:stage) }
set :whenever_identifier, -> { "#{fetch(:application)}_#{fetch(:stage)}" }

Now add roles: [] to your task definitions.

  every 1.minutes, roles: [:production_cron] do
    rake 'notifications:process'
  every 1.day, at: ['2:00 am'], roles: [:production_cron, :staging_cron] do
    rake 'db:backups:capture'



Drop your email in the box below to subscribe to my newsletter. Once per week you'll get Ruby/Rails tips, guides, job postings, and general thoughts from the web developer trenches.