All Collections › Docker Clusters › Cron Jobs

Cron Jobs

Run recurring tasks in your app's image on a schedule

Updated

Cron jobs on a Docker cluster run in your app's live image, with the same environment variables and volumes as its processes. You can add them in Hatchbox or list them in your repository.

How cron jobs run

Each job runs on the cluster's cron server, where Hatchbox writes it to /etc/cron.d/<app>. At the scheduled time, cron starts a new container from the current release with bin/run, runs the command, and removes the container when it finishes.

Because bin/run always uses the current release, a deploy doesn't have to touch the cron file. A job that starts after a deploy runs the new code.

Schedules use standard cron syntax and run in UTC, the time zone Hatchbox servers use.

Adding cron jobs in Hatchbox

Open your app's Cron Jobs tab and click Add a cron job. Give it a name, a schedule like 0 3 * * * or @hourly, and the command to run, like bin/rails runner 'Report.daily'.

Each job's name must be unique within the app. If you connect AppSignal or Honeybadger, the name is the job's check-in, so you're alerted when a job doesn't run.

Defining cron jobs in .hatchbox/cron.yml

To keep your schedule in your repository, commit a .hatchbox/cron.yml file. Each key is a job's name, with a schedule and a command:

daily_report:
  schedule: "0 3 * * *"
  command: bin/rails runner 'Report.daily'

cleanup_sessions:
  schedule: "@hourly"
  command: bin/rails cleanup:sessions

Each deploy reads the file:

  • A file Hatchbox can't use fails the deploy before anything is built, with an error saying what to fix. That includes invalid YAML, an invalid schedule, or a key other than schedule and command.
  • Once the new release is live, the app's jobs are updated to match the file. Jobs it adds are created, jobs it changes are updated, and jobs it no longer lists are removed.
  • Jobs are matched by name, so a job keeps its monitoring when you change its schedule or command. Renaming a job in the file replaces it with a new one.
  • If a job you added in Hatchbox has the same name as one in the file, the file takes it over, monitoring included, so it doesn't run twice.

Quote a schedule or command that starts with @ or contains a colon followed by a space, like "@daily" or "bin/rails runner 'Report.run(since: 1.day.ago)'". Otherwise YAML reads it as something else.

On the Cron Jobs tab, these jobs are listed under From .hatchbox/cron.yml. Their name, schedule and command can only be changed in the file, and they can't be deleted in Hatchbox: remove them from the file and deploy. You can still set up their AppSignal or Honeybadger monitoring in Hatchbox.

.hatchbox/cron.yml works on clusters without Docker too, so you can move your jobs into it before moving an app to a Docker cluster.

Moving from whenever

Docker clusters don't run the whenever gem's config/schedule.rb. If your app has one, the deploy log warns that it's ignored. Move its jobs to .hatchbox/cron.yml or the Cron Jobs tab:

whenevercron.yml
every 10.minutesschedule: "*/10 * * * *"
every 1.hourschedule: "@hourly"
every 1.day, at: "4:30 am"schedule: "30 4 * * *"
every :sunday, at: "12pm"schedule: "0 12 * * 0"
runner "Report.daily"command: bin/rails runner 'Report.daily'
rake "cleanup:sessions"command: bin/rails cleanup:sessions

The container already starts in your app's directory with its environment set, so leave out whenever's cd, bundle exec wrappers and output redirects. Once the jobs are moved, you can remove config/schedule.rb and the whenever gem.

Debugging cron jobs

On the cron server, cron's own log shows when each job started:

journalctl -u cron

A job's output goes to the journal, tagged with your app's name followed by .run. For an app named myapp:

journalctl -t myapp.run

Add -f to follow it live. To try a job's command by hand, run it the way cron does, as the deploy user:

/home/deploy/myapp/bin/run bin/rails runner 'Report.daily'