aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/docker/schedule.sh
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2022-12-03 17:54:29 +0100
committerEgor Tensin <Egor.Tensin@gmail.com>2022-12-03 17:57:25 +0100
commit11b442988dc4bb8c1d562c9768e8a3fb685b4443 (patch)
treea65b229fbe18564e75b1c1adcd8cd3922188e764 /docker/schedule.sh
parentv4.0.4 (diff)
downloadcgitize-11b442988dc4bb8c1d562c9768e8a3fb685b4443.tar.gz
cgitize-11b442988dc4bb8c1d562c9768e8a3fb685b4443.zip
docker: factor out cron stuff into schedule.sh
It can be easily reused that way.
Diffstat (limited to 'docker/schedule.sh')
-rwxr-xr-xdocker/schedule.sh60
1 files changed, 60 insertions, 0 deletions
diff --git a/docker/schedule.sh b/docker/schedule.sh
new file mode 100755
index 0000000..0f3fa20
--- /dev/null
+++ b/docker/schedule.sh
@@ -0,0 +1,60 @@
+#!/usr/bin/env bash
+
+# Copyright (c) 2021 Egor Tensin <Egor.Tensin@gmail.com>
+# This file is part of the "cgitize" project.
+# For details, see https://github.com/egor-tensin/cgitize.
+# Distributed under the MIT License.
+
+set -o errexit -o nounset -o pipefail
+shopt -s inherit_errexit lastpipe
+
+schedule_to_cron() {
+ local schedule
+ for schedule; do
+ case "$schedule" in
+ 15min) echo '*/15 * * * *' ;;
+ hourly) echo '0 * * * *' ;;
+ daily) echo '0 0 * * *' ;;
+ weekly) echo '0 0 * * 1' ;;
+ monthly) echo '0 0 1 * *' ;;
+ *)
+ echo "$schedule"
+ ;;
+ esac
+ done
+}
+
+make_task_script() {
+ echo "#!/bin/bash
+cd -- "$( pwd )" && $( printf -- ' %q' "$@" )"
+}
+
+setup_cron_task() {
+ local schedule
+ schedule="${SCHEDULE:-once}"
+
+ if [ "$schedule" = once ]; then
+ exec "$@"
+ fi
+
+ schedule="$( schedule_to_cron "$schedule" )"
+
+ make_task_script "$@" > /task.sh
+ chmod +x /task.sh
+
+ # Run the task once when the container is started, regardless of schedule.
+ /task.sh
+
+ local crontab
+ crontab="$schedule /task.sh
+# This is the new crontab."
+
+ echo "$crontab" | crontab -
+ exec crond -f
+}
+
+main() {
+ setup_cron_task "$@"
+}
+
+main "$@"