Skip to content

πŸ“… Create jobs on a time-based schedule on Swarm

License

Notifications You must be signed in to change notification settings

breitsmiley/swarm-cronjob

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

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" \
  crazy-max/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.

About

πŸ“… Create jobs on a time-based schedule on Swarm

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 63.9%
  • Shell 28.8%
  • Dockerfile 7.3%