|
1 | 1 | # Using Cron for Scheduled Deployments |
2 | 2 |
|
3 | | -Dispenser provides a `cron` feature to schedule deployments or restarts of your services at specific intervals. This is useful for tasks that need to run periodically, such as batch jobs, or for ensuring services are restarted regularly for maintenance. |
| 3 | +Dispenser supports cron scheduling to deploy or restart services at specific intervals. This is useful for batch jobs, backups, ETL processes, or periodic maintenance restarts. |
4 | 4 |
|
5 | | -## How it Works |
| 5 | +## Configuration |
6 | 6 |
|
7 | | -You can add a `cron` attribute to any `[[instance]]` block in your `dispenser.toml` configuration file. The value of this attribute is a cron expression that defines the schedule for the deployment. |
| 7 | +Add a `cron` field to the `[dispenser]` section in your `service.toml`: |
8 | 8 |
|
9 | | -When a `cron` schedule is defined for an instance, Dispenser will trigger a redeployment of the corresponding Docker Compose service according to the schedule. This is equivalent to running `docker-compose up -d --force-recreate` for the service. |
| 9 | +```toml |
| 10 | +[dispenser] |
| 11 | +watch = false |
| 12 | +initialize = "on-trigger" |
| 13 | +cron = "0 0 2 * * *" # Every day at 2 AM |
| 14 | +``` |
10 | 15 |
|
11 | | -The cron scheduler uses a 6-field format that includes seconds: |
| 16 | +### Cron Expression Format |
| 17 | + |
| 18 | +Dispenser uses a 6-field format (with seconds): |
12 | 19 |
|
13 | 20 | ``` |
14 | 21 | ┌───────────── second (0 - 59) |
15 | 22 | │ ┌───────────── minute (0 - 59) |
16 | 23 | │ │ ┌───────────── hour (0 - 23) |
17 | | -│ │ │ ┌───────────── day of the month (1 - 31) |
| 24 | +│ │ │ ┌───────────── day of month (1 - 31) |
18 | 25 | │ │ │ │ ┌───────────── month (1 - 12) |
19 | | -│ │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday) |
20 | | -│ │ │ │ │ │ |
| 26 | +│ │ │ │ │ ┌───────────── day of week (0 - 6, Sunday = 0) |
21 | 27 | │ │ │ │ │ │ |
22 | 28 | * * * * * * |
23 | 29 | ``` |
24 | 30 |
|
25 | | -You can use online tools like [crontab.guru](https://crontab.guru/) to help generate the correct cron expression. Note that many online tools generate 5-field expressions, so you may need to add the seconds field (`*` or `0`) at the beginning. |
| 31 | +**Common expressions:** |
| 32 | +- `0 0 2 * * *` - Daily at 2 AM |
| 33 | +- `0 0 */6 * * *` - Every 6 hours |
| 34 | +- `0 30 9 * * 1-5` - Weekdays at 9:30 AM |
| 35 | +- `0 0 0 1 * *` - First day of each month |
| 36 | +- `*/10 * * * * *` - Every 10 seconds |
| 37 | + |
| 38 | +Use [crontab.guru](https://crontab.guru/) for help (add `0` for seconds field). |
| 39 | + |
| 40 | +## Examples |
| 41 | + |
| 42 | +### Scheduled Backup Job |
| 43 | + |
| 44 | +```toml |
| 45 | +[service] |
| 46 | +name = "backup-job" |
| 47 | +image = "my-backup:latest" |
26 | 48 |
|
27 | | -## Use Cases |
| 49 | +[[volume]] |
| 50 | +source = "./backups" |
| 51 | +target = "/backups" |
28 | 52 |
|
29 | | -### Scheduled-Only Deployments |
| 53 | +restart = "no" |
30 | 54 |
|
31 | | -You can use `cron` without an `images` attribute. This is ideal for services that run on a schedule such as ETLs or batch processing tasks, and do not have a corresponding image to monitor for updates. |
| 55 | +[dispenser] |
| 56 | +watch = false |
| 57 | +initialize = "on-trigger" |
| 58 | +cron = "0 0 2 * * *" # Daily at 2 AM |
| 59 | +``` |
32 | 60 |
|
33 | | -**Example:** |
34 | | -The following configuration will run the `hello-world` service every 10 seconds. Since there is no image to watch, the deployment is only triggered by the cron schedule. |
| 61 | +### ETL Job Every Hour |
35 | 62 |
|
36 | 63 | ```toml |
37 | | -# dispenser.toml |
| 64 | +[service] |
| 65 | +name = "etl-processor" |
| 66 | +image = "my-etl:latest" |
| 67 | +command = ["python", "process.py"] |
38 | 68 |
|
39 | | -[[instance]] |
40 | | -path = "hello-world" |
41 | | -cron = "*/10 * * * * *" |
| 69 | +restart = "no" |
| 70 | + |
| 71 | +[dispenser] |
| 72 | +watch = false |
| 73 | +initialize = "on-trigger" |
| 74 | +cron = "0 0 * * * *" # Every hour |
42 | 75 | ``` |
43 | 76 |
|
44 | | -The `docker-compose.yaml` for this service might look like this. It is important to set `restart: no` to prevent the container from restarting automatically after its task is complete. It will wait for the next scheduled run from Dispenser. |
| 77 | +### Periodic Restart with Image Watching |
| 78 | + |
| 79 | +```toml |
| 80 | +[service] |
| 81 | +name = "worker" |
| 82 | +image = "my-worker:latest" |
45 | 83 |
|
46 | | -```yaml |
47 | | -# hello-world/docker-compose.yaml |
| 84 | +restart = "always" |
48 | 85 |
|
49 | | -version: "3.8" |
50 | | -services: |
51 | | - hello-world: |
52 | | - image: hello-world |
53 | | - restart: no |
| 86 | +[dispenser] |
| 87 | +watch = true |
| 88 | +initialize = "immediately" |
| 89 | +cron = "0 0 4 * * *" # Restart daily at 4 AM |
54 | 90 | ``` |
55 | 91 |
|
56 | | -### Scheduled Restarts with Image Monitoring |
| 92 | +This configuration will: |
| 93 | +- Deploy when a new image is detected |
| 94 | +- Also restart daily at 4 AM (even if no new image) |
57 | 95 |
|
58 | | -You can use `cron` in combination with image monitoring. In this case, Dispenser will deploy a new version of your service under two conditions: |
59 | | -1. A new Docker image is detected in the registry. |
60 | | -2. The `cron` schedule is met. |
| 96 | +## Options |
61 | 97 |
|
62 | | -This is useful for services that should be restarted periodically, even if no new image is available. |
| 98 | +### `initialize` |
63 | 99 |
|
64 | | -**Example:** |
65 | | -The following configuration watches the `nginx:latest` image and also restarts the service every minute. |
| 100 | +- `immediately` (default) - Start when Dispenser starts |
| 101 | +- `on-trigger` - Only start when cron fires or image updates |
66 | 102 |
|
67 | | -```toml |
68 | | -# dispenser.toml |
| 103 | +### `watch` |
69 | 104 |
|
70 | | -[[instance]] |
71 | | -path = "nginx" |
72 | | -# Will restart the service every minute or when the nginx image gets updated |
73 | | -cron = "0 */1 * * * *" |
74 | | -images = [{ registry = "docker.io", name = "nginx", tag = "latest" }] |
75 | | -``` |
| 105 | +- `true` - Monitor registry for image updates |
| 106 | +- `false` - Only run on cron schedule |
| 107 | + |
| 108 | +### `restart` |
76 | 109 |
|
77 | | -By using the `cron` feature, you can extend Dispenser's capabilities beyond continuous deployment to include scheduled task orchestration. You can find more examples in the `example` directory of the project. |
| 110 | +Use `restart = "no"` for one-time jobs to prevent automatic restarts between scheduled runs. |
0 commit comments