πŸ“… Create jobs on a time-based schedule on Swarm
Clone or download
Latest commit a977cab Jan 14, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Initial version Dec 13, 2018
.res Update examples Dec 13, 2018
app Fix non-cronjob services added to cronjob list (Issue #2) Jan 14, 2019
cmd Fix non-cronjob services added to cronjob list (Issue #2) Jan 14, 2019
.dockerignore Initial version Dec 13, 2018
.editorconfig Initial version Dec 13, 2018
.gitignore Initial version Dec 13, 2018
.travis.yml Bump version Jan 14, 2019
CHANGELOG.md Bump version Jan 14, 2019
Dockerfile Fix build args Dec 21, 2018
LICENSE Initial version Dec 13, 2018
README.md Typo (Issue #1) Dec 20, 2018
build.sh Small typo Dec 21, 2018
go.mod Initial version Dec 13, 2018
go.sum Initial version Dec 13, 2018
main.go Fix maxprocs Jan 14, 2019

README.md

Version Build Status Docker Stars Docker Pulls Docker Repository on Quay
Go Report Code Quality Donate Paypal

About

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.

Features

  • 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

Docker

Environment variables

  • TZ : The timezone assigned to the scheduler (default UTC)
  • LOG_LEVEL : Log level (default info)
  • LOG_NOCOLOR : Disables the colorized output (default false)

Quickstart

Deploy swarm-cronjob

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_NOCOLOR=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

Deploy a stack

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 to 0 to avoid running task as soon as the service is deployed
  • Set restart_policy.condition to none. 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

Logs

Now check the logs of your services :

$ 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

How can I help ?

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 🍻!

Paypal

License

MIT. See LICENSE for more details.