Whenever gem

We support 'whenever' schedules as defined in your app

If you have the whenever gem in your Gemfile (and have a corresponding schedule defined in config/schedule.rb) then Cloud 66 will automatically setup your desired whenever schedules as crontab jobs on the appropriate servers.

Should you wish to remove these jobs, simply remove the schedule file or gem and redeploy. You can target specific servers by using roles in your config/schedule.rb definition.

Instead of using whenever jobs, you may find it more beneficial to use scheduled rake tasks. There are a number of benefits with this approach:

  1. See your jobs (and run history) via the UI, and run them on demand
  2. You can modify their scheduling via the UI
  3. You can get alerts if your job fails

Depending on whether you’re running your task as a runner, rake or command, you will need to reference environment variables differently. For example, if using a command, you’ll need to reference your environment in bash syntax, eg. $STACK_PATH.

Note

Whenever jobs will be run on all your app servers by default - but you can also be more specific about where they should run.

Target all servers

env :PATH, ENV['PATH']

every 10.minutes do
  command "a_dummy_command"
end
every 45.minutes do
  command "a_dummy_command"
end

Target your web server(s)

env :PATH, ENV['PATH']

every 10.minutes, :roles => [:app] do
  rake "test:example"
end

Target your database server

env :PATH, ENV['PATH']

every 10.minutes, :roles => [:db] do
  command "a_dummy_command"
end

Target your Redis server

env :PATH, ENV['PATH']

every 10.minutes, :roles => [:redis] do
  command "a_dummy_command"
end

Target the first server in a server group

env :PATH, ENV['PATH']

def primary_runner(command)
  runner("if ENV['PRIMARY'] == 'true'; #{command}; end")
end

every 15.minutes, roles: [:app] do
  primary_runner 'MyClass.some_job' # runs on primary app server only
  runner 'MyClass.another_job' # runs on all app servers
end

This will only run MyClass.some_job on your primary app server, and MyClass.another_job on all app servers. It checks whether or not each server has the PRIMARY environment variable set to true.

Note

You should include the line env :PATH, ENV['PATH'] at the top of your config/schedule.rb file to avoid command not found errors.

You can view your generated crontab jobs with:

crontab -l

 

You must be logged in to comment on this article