Scheduled Jobs
A scheduled job is a command that runs at regular intervals based on a
predefined schedule. The scheduled job has access to the same environment and
binaries as the application runtime. You can configure a scheduled job when you
create an app, when you update an existing app, or on other configuration
layers such as .deploio.yaml.
Specifying a Scheduled Job on App Creation
To specify a scheduled job when creating an application, pass the scheduled job
flags to the nctl create app command:
--scheduled-job-command="bundle exec rails runner" Command to execute to start the scheduled job.
--scheduled-job-name=scheduled-1 Name of the scheduled job to add.
--scheduled-job-size=micro Size (resources) of the scheduled job (defaults to "micro").
--scheduled-job-schedule="* * * * *" Cron notation string for the scheduled job (defaults to "* * * * *").
--scheduled-job-time-zone="Europe/Zurich" Time zone the schedule is evaluated in, such as "Europe/Zurich" (defaults to "UTC").
--scheduled-job-retries=0 Number of times the job restarts on failure (default is 0, maximum is 5).
--scheduled-job-timeout=5m Timeout of the job (default is 5m, minimum is 1m, maximum is 30m).
$ nctl create app --scheduled-job-command="sleep 5; date" --scheduled-job-name="myjob-1"
The scheduled job runs according to the --scheduled-job-schedule parameter,
which uses the standard five-field cron notation:
┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6, Sunday is 0)
│ │ │ │ │
* * * * *
The following table shows some common schedules:
| Schedule | Runs |
|---|---|
*/5 * * * * | Every 5 minutes |
0 * * * * | At the start of every hour |
0 3 * * * | Every day at 03:00 |
0 9 * * 1-5 | At 09:00, Monday through Friday |
0 6 1 * * | At 06:00 on the first day of the month |
Deploio evaluates the schedule in the time zone you set with
--scheduled-job-time-zone. If you don't set a time zone, the schedule uses
UTC. Don't add a CRON_TZ= or TZ= prefix to the schedule itself.
For the full syntax, including lists, ranges, and shortcuts such as @daily,
see the
Kubernetes CronJob schedule syntax.
If a scheduled job runs longer than its defined interval, Deploio skips the next scheduled execution and lets the running job finish without interruption.
Specifying a Scheduled Job on Other Configuration Layers
Because scheduled jobs are part of a Deploio application's configuration, you
can also define them on other configuration layers.
For example, you can specify a scheduled job in your .deploio.yaml Git
configuration file:
scheduledJobs:
- command: sleep 60; date
name: scheduled-1
retries: 0
schedule: "*/5 * * * *"
timeZone: "Europe/Zurich"
size: micro
timeout: 5m0s
Updating and Deleting Scheduled Jobs
To update a scheduled job, pass the same --scheduled-job-* flags to nctl update app:
$ nctl update app --scheduled-job-command="sleep 50; date" --scheduled-job-name="myjob-1"
To delete a scheduled job by name, use the --delete-scheduled-job flag with
nctl update app:
--delete-scheduled-job=DELETE-SCHEDULED-JOB Delete a scheduled job by name.
$ nctl update app --delete-scheduled-job="myjob-1"
Viewing the Status of a Scheduled Job
If a scheduled job fails, the associated release does not fail and continues running. To view the detailed status of a scheduled job, retrieve the release in YAML format:
$ nctl get releases brave-scrambler-vc78b -o yaml
[...]
status:
atProvider:
scheduledJobStatus:
- name: scheduled-1
replicaObservation:
- replicaName: go-scheduled-scheduled-1-29038220
status: succeeded
The status.atProvider.scheduledJobStatus field at the bottom of the release
output shows the detailed status of the scheduled job. Deploio also writes
scheduled job logs to the application log, which you can view with the nctl logs app command.