1
0
Fork 0
mirror of https://github.com/yavook/kiwi-cron.git synced 2024-11-24 00:13:00 +00:00

Merge tag '0.2.0' into develop

Modularity and easier randomization
This commit is contained in:
Jörn-Michael Miehe 2022-03-03 00:27:42 +01:00
commit 5400937cf4
3 changed files with 19 additions and 22 deletions

View file

@ -14,6 +14,12 @@ Just drop your scripts into the relevant directory under `/kiwi-cron`, that's it
You will likely want to automate some tasks regarding your `docker` infrastructure. You will likely want to automate some tasks regarding your `docker` infrastructure.
That's why the `kiwi-cron` images package a current `docker-cli` you can just mount your `docker.sock` in its containers and use `docker` commands normally. That's why the `kiwi-cron` images package a current `docker-cli` you can just mount your `docker.sock` in its containers and use `docker` commands normally.
## Time Zones
`kiwi-cron` images include the `tzdata` package and automatically handle `/etc/localtime` on startup. By default, "Etc/UTC" is set as the container time zone.
To use a different time zone, change the container environment variable `TZ` to your liking, e.g. "Europe/Berlin".
## Simple jobs ## Simple jobs
On startup, `kiwi-cron` checks for possible job files in the `/kiwi-cron` directory structure. On startup, `kiwi-cron` checks for possible job files in the `/kiwi-cron` directory structure.
@ -21,27 +27,21 @@ On startup, `kiwi-cron` checks for possible job files in the `/kiwi-cron` direct
For each subdirectory, a random valid cron schedule is generated, so that: For each subdirectory, a random valid cron schedule is generated, so that:
- `/kiwi-cron/hourly` runs once every hour (random minute) - `/kiwi-cron/hourly` runs once every hour (random minute)
- `/kiwi-cron/daily` runs once every day (random nighttime value) - `/kiwi-cron/daily` runs once every day (random nighttime value between 9 pm and 3 am)
- `/kiwi-cron/weekly` runs once every weekend (random nighttime value on Saturday or Sunday) - `/kiwi-cron/weekly` runs once every weekend (random nighttime value on Saturday or Sunday)
- `/kiwi-cron/monthly` runs once every month (random nighttime value on a random day) - `/kiwi-cron/monthly` runs once every month (random nighttime value on a random day <= 28)
- `/kiwi-cron/yearly` and `/kiwi-cron/annually` runs once a year (random nighttime value on a random day in January or February) - `/kiwi-cron/yearly` and `/kiwi-cron/annually` runs once a year (random nighttime value on a random day <= 28 in January or February)
Cron schedules are regenerated once on each startup, only for directories that have files. Cron schedules are regenerated once on each startup, only for directories that have files.
## Time Zones ## Granularity: The `/kiwi-cron/every` directory
`kiwi-cron` images include the `tzdata` package and automatically handle `/etc/localtime` on startup. By default, "Etc/UTC" is set as the container time zone.
To use a different time zone, change the container environment variable `TZ` to your liking, e.g. "Europe/Berlin".
## Finer granularity: The `/kiwi-cron/every` directory
Directories like `/kiwi-cron/every/5_minutes` will run scripts every 5 minutes. Directories like `/kiwi-cron/every/5_minutes` will run scripts every 5 minutes.
`kiwi-cron` picks up on that format and generates valid `cron` schedules on startup. `kiwi-cron` picks up on that format and generates valid `cron` schedules on startup.
You can define schedules to be run every N minutes, hours, days, or months by creating the corresponding directories. This way, you can define schedules to be run every N minutes, hours, days, or months by creating the corresponding directories.
Scheduling for every N weeks (or years) doesn't work that way; jobs in those directories will instead be run every week (or every year). Scheduling for every N weeks (or years) doesn't work though; jobs in those directories will instead be run every week (or every year).
## Inspection ## Inspection

View file

@ -5,29 +5,26 @@ cs_units="${2}"
cs_command="${3}" cs_command="${3}"
if [ "${cs_every}" = "1" ]; then if [ "${cs_every}" = "1" ]; then
cs_every="" cs_every="*"
else else
cs_every="/${cs_every}" cs_every="*/${cs_every}"
fi fi
# generate valid random schedule values
# generally, "nighttime" and "weekend" will be preferred.
case "${cs_units}" in case "${cs_units}" in
minute) minute)
echo "*${cs_every} * * * * ${cs_command}" echo "${cs_every} * * * * ${cs_command}"
;; ;;
hour) hour)
echo "R *${cs_every} * * * ${cs_command}" echo "R ${cs_every} * * * ${cs_command}"
;; ;;
day) day)
echo "R R *${cs_every} * * ${cs_command}" echo "R R ${cs_every} * * ${cs_command}"
;; ;;
week) week)
echo "R R * * R ${cs_command}" echo "R R * * R ${cs_command}"
;; ;;
month) month)
echo "R R R *${cs_every} * ${cs_command}" echo "R R R ${cs_every} * ${cs_command}"
;; ;;
year) year)
echo "R R R R * ${cs_command}" echo "R R R R * ${cs_command}"

View file

@ -8,7 +8,7 @@ read -r rs_minute rs_hour rs_day rs_month rs_weekday rs_command
rs_minute=$(( RANDOM % 60 )) rs_minute=$(( RANDOM % 60 ))
[ "${rs_hour}" = "R" ] && \ [ "${rs_hour}" = "R" ] && \
rs_hour=$(( (21 + RANDOM % 7) % 24 )) # prefer nighttime rs_hour=$(( (21 + RANDOM % 6) % 24 )) # prefer nighttime
# rs_hour=$(( RANDOM % 24 )) # don't prefer nighttime # rs_hour=$(( RANDOM % 24 )) # don't prefer nighttime
[ "${rs_day}" = "R" ] && \ [ "${rs_day}" = "R" ] && \