Swarm-cronjob #
Swarm-cronjob brings a lot of convenience to the table. “Jobs” are configured via labels. It works well in a distributed setup and even implements basic locking mechanisms so the tasks don’t build up when one is running already.
Setup #
Deploy the following to your endpoint:
|
|
A few things to note:
swarm-cronjob
will use docker APIs to look for new containers (more on this below). While convenient for this use-case, access to the Docker APIs has security implications.- Take note of the
LOG_LEVEL
in case jobs don’t work as expected. - Don’t run
:latest
:-)
To save some time, we've assembled the stack and a `Makefile` (for `make deploy`) into a repository on Github: [hostwithquantum/example-swarm-cronjob](https://github.com/hostwithquantum/example-swarm-cronjob)
After a successful deployment of the stack, swarm-cronjob will wait for new services and directions. No further restarts are required.
Example #
Add the following to your docker-compose.yml
:
|
|
The example runs the backup service/cronjob every 30 minutes.
For brevity, we omit the configuration of the `mc` client, but see the [official documentation](https://docs.min.io/docs/minio-client-complete-guide) for help.
Tips #
The format used in cron expression in swarm-cronjob is not not the usual `crond`-format you may be familiar with. Instead of minutes (on the left), this expression starts with seconds. See [the documentation](https://pkg.go.dev/github.com/robfig/cron?utm_source=godoc#hdr-CRON_Expression_Format) for details.
When multiple jobs are deployed, execution may need to be spread out:
- each job requires certain resources to start and run
- executing many of jobs in parallel can impact other services