Monitor Rails cron jobs written with whenever

On this page

Whenever turns a Ruby schedule file into crontab lines, and that is all it does: it does not watch the jobs, and it writes the crontab only when you ask. A custom job type adds the ping to every job you define, in one place.

How can a scheduled job fail silently on Rails with the whenever gem?

  • Running whenever on its own only prints the converted schedule. It does not read or write your crontab; the crontab changes only when you run whenever --update-crontab.
  • Without --user, the crontab is written for the current user, so tasks that need another user's permissions fail when cron runs them.
  • Job output goes to the address in the MAILTO variable, which is often unset or unread. The output redirection of a job type (:output) decides where else it goes.
  • Commands run in cron's small environment. The default job template in whenever can be changed to run through a login shell (bash -l -c) when a job needs its usual environment.
  • If a deploy forgets to update the crontab, the old schedule keeps running, or none does.

What are the limits of Rails with the whenever gem?

LimitWhat the documentation says
Writing the crontabwhenever --update-crontab writes it; plain whenever only shows the result.
Crontab userThe current user unless you pass --user.
Job typescommand, runner and rake are built in; job_type defines your own.
OutputJob output is mailed to the address in MAILTO; :output in a job type controls redirection.
CapistranoA recipe updates the crontab on deploy, and set :whenever_identifier keeps entries separate per application and stage.

How do you add a CronMissed ping to Rails with the whenever gem?

Define a job type that appends a curl to the usual rails runner line, then pass the ping URL as the ping option on each job. The ping runs only if the job exits 0.

config/schedule.rb
# config/schedule.rb
set :output, "log/cron.log"

job_type :runner_ping, "cd :path && :bundle_command :runner_command -e :environment ':task' :output && curl -fsS --max-time 10 --retry 5 --retry-connrefused -o /dev/null :ping"

every :day, at: "3:15 am" do
  runner_ping "Report.nightly", ping: "https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10"
end
shell
bundle exec whenever --update-crontab
crontab -l

Which check settings fit Rails with the whenever gem?

Create a cron check with the same time and the server's time zone; whenever interprets times in the server's local zone. Grace should cover the run time of the Rails boot plus the job, often 10 minutes. If you need failures reported at once, change the job type to send the exit status instead of using &&.

How do you confirm the check works?

  1. Run bundle exec whenever and read the generated line to make sure the curl is at its end.
  2. Run bundle exec whenever --update-crontab, then crontab -l to confirm the entry is installed for the right user.
  3. Run the generated command by hand and confirm the ping arrives.

Sources

Questions and answers

Why a job type and not a ping inside each Ruby method?
A job type covers every job that uses it and also catches the case where Rails fails to boot, which no code inside the app could report.
Where do deploys fit in?
Run whenever --update-crontab as part of the deploy, with an identifier per application, then add a check so a deploy that forgets it raises an alert when the next run is missed.
Know when this job stops

Create a check, add the ping to the job, and CronMissed alerts you the first time a run is late or reports a failure.

Start monitoring