swarm-cronjob creates jobs on a time-based schedule on Swarm with a dedicated service in a distributed manner that configures itself automatically and dynamically through labels and Docker API.
- Continuously updates its configuration (no restart)
- Cron implementation through go routines
- Allow to skip a job if the service is currently running
- Timezone can be changed for the scheduler
TZ
: The timezone assigned to the scheduler (defaultUTC
)LOG_LEVEL
: Log level (defaultinfo
)LOG_JSON
: Enable JSON logging output (defaultfalse
)
Create a service that uses the swarm-cronjob image :
$ docker service create --name swarm_cronjob \
--mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock \
--env "LOG_LEVEL=info" \
--env "LOG_JSON=false" \
--constraint "node.role == manager" \
crazymax/swarm-cronjob
Alternatively, you can deploy the stack swarm_cronjob.yml :
docker stack deploy -c swarm_cronjob.yml swarm_cronjob
Now that we have a swarm-cronjob instance up and running, we will deploy new services.
Create a new stack based on this one (date). You can include any configuration as long as you abide with the following conditions :
- Set
command
to run the task command - Set
replicas
to0
to avoid running task as soon as the service is deployed - Set
restart_policy.condition
tonone
. This is needed for a cronjob, otherwise the task will restart automatically - Add labels to tell swarm-cronjob that your service is a cronjob :
swarm.cronjob.enable
: Set to true to enable the cronjob (required)swarm.cronjob.schedule
: CRON expression format to use (required)swarm.cronjob.skip-running
: Do not start a job if the service is currently running (optional)
Once ready, deploy your cron stack on the swarm cluster :
docker stack deploy -c date.yml date
💡 More examples can be found here
swarm-cronjob binaries are available in releases page.
Choose the archive matching the destination platform and extract swarm-cronjob:
$ cd /opt
$ wget -qO- https://github.com/crazy-max/swarm-cronjob/releases/download/v1.0.0/swarm-cronjob_1.0.0_linux_x86_64.tar.gz | tar -zxvf - swarm-cronjob
After getting the binary, it can be tested with ./swarm-cronjob --help
or moved to a permanent location.
When launched manually, swarm-cronjob can be killed using Ctrl+C
:
$ ./swarm-cronjob --help
usage: swarm-cronjob [<flags>]
Create jobs on a time-based schedule on Swarm. More info on
https://github.com/crazy-max/swarm-cronjob
Flags:
--help Show context-sensitive help (also try --help-long and --help-man).
--timezone="UTC" Timezone assigned to the scheduler.
--log-level="info" Set log level.
--log-json Enable JSON logging output.
--version Show application version.
Here is a sample output:
$ docker service logs swarm_cronjob_app
swarm_cronjob_app.1.nvsjbhdhiagl@default | Thu, 13 Dec 2018 20:04:37 UTC INF Starting swarm-cronjob v0.1.0
swarm_cronjob_app.1.nvsjbhdhiagl@default | Thu, 13 Dec 2018 20:04:37 UTC INF Add cronjob for service date_test with schedule 0 * * * * *
swarm_cronjob_app.1.nvsjbhdhiagl@default | Thu, 13 Dec 2018 20:05:00 UTC INF Start date_test (exit 0 ; complete)
swarm_cronjob_app.1.nvsjbhdhiagl@default | Thu, 13 Dec 2018 20:06:00 UTC INF Start date_test (exit 0 ; complete)
swarm_cronjob_app.1.nvsjbhdhiagl@default | Thu, 13 Dec 2018 20:07:00 UTC INF Start date_test (exit 0 ; complete)
swarm_cronjob_app.1.nvsjbhdhiagl@default | Thu, 13 Dec 2018 20:08:00 UTC INF Start date_test (exit 0 ; complete)
swarm_cronjob_app.1.nvsjbhdhiagl@default | Thu, 13 Dec 2018 20:09:00 UTC INF Start date_test (exit 0 ; complete)
swarm_cronjob_app.1.nvsjbhdhiagl@default | Thu, 13 Dec 2018 20:10:00 UTC INF Start date_test (exit 0 ; complete)
$ docker service logs date_test
date_test.1.o1d5mn4gjff3@default | Thu Dec 13 20:11:01 UTC 2018
date_test.1.5askx244las2@default | Thu Dec 13 20:09:00 UTC 2018
date_test.1.4lz5ez2waekk@default | Thu Dec 13 20:12:00 UTC 2018
date_test.1.135qzpxd1ui3@default | Thu Dec 13 20:13:01 UTC 2018
date_test.1.hngject056n3@default | Thu Dec 13 20:10:00 UTC 2018
All kinds of contributions are welcome 🙌!
The most basic way to show your support is to star 🌟 the project, or to raise issues 💬
But we're not gonna lie to each other, I'd rather you buy me a beer or two 🍻!
MIT. See LICENSE
for more details.