Read more

Whenever: Don't forget leading zeros for hours!

Judith Roth
June 08, 2018Software engineer at makandra GmbH

Whenever is a Ruby gem that provides a nicer syntax for writing and deploying cron jobs.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Leading zeros are important for whenever if you use the 24-hours format!

This schedule.rb:

every 1.day, at: '3:00', roles: [:primary_cron] do
  runner 'Scheduler.delay.do_things'
end

will lead to this crontab entry (crontab -l) with the default configuration:

0 15 * * * /bin/bash -l -c 'cd /var/www/my-project/releases/20180607182518 && bin/rails runner -e production '\''Scheduler.delay.do_things'\'''

Which would run on 3pm instead of 3am.

Using the leading zero will lead to the expected behaviour:

schedule.rb

every 1.day, at: '03:00', roles: [:primary_cron] do
  runner 'Scheduler.delay.do_things'
end

crontab -l:

0 3 * * * /bin/bash -l -c 'cd /var/www/my-project/releases/20180608095252 && bin/rails runner -e staging '\''Scheduler.delay.do_things'\'''

The reason for the described behavior is that the underlying datetime parsing gem "chronic" defaults to a 12 hour clock Show archive.org snapshot . You could configure it to a 24 hour clock Show archive.org snapshot to stop worrying about leading zeros:

set :chronic_options, hours24: true

Another interesting chronic option is "week_start", which defaults to :sunday.

Posted by Judith Roth to makandra dev (2018-06-08 12:01)