diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2022-12-03 03:33:30 +0100 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2022-12-03 03:33:30 +0100 |
commit | 6148b355ff329f44947886ff4210eeab3db8b241 (patch) | |
tree | f793443f3fc98a2b71053261a88e9475a6cdaece /docker | |
parent | minor fix (diff) | |
download | cgitize-6148b355ff329f44947886ff4210eeab3db8b241.tar.gz cgitize-6148b355ff329f44947886ff4210eeab3db8b241.zip |
docker: refactor entrypoint.sh
Diffstat (limited to 'docker')
-rwxr-xr-x | docker/entrypoint.sh | 65 |
1 files changed, 44 insertions, 21 deletions
diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 6e755b3..5674e36 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -7,32 +7,55 @@ set -o errexit -o nounset -o pipefail -schedule="${SCHEDULE:-once}" - -case "$schedule" in - once) +readonly base_dir=/usr/src + +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 -- "$base_dir" &&$( printf -- ' %q' "$@" )" +} + +setup_cron_task() { + local schedule + schedule="${SCHEDULE:-once}" + + if [ "$schedule" = once ]; then exec "$@" - ;; - 15min) schedule='*/15 * * * *' ;; - hourly) schedule='0 * * * *' ;; - daily) schedule='0 0 * * *' ;; - weekly) schedule='0 0 * * 1' ;; - monthly) schedule='0 0 1 * *' ;; - *) ;; -esac + fi -script="#!/bin/bash -cd /usr/src &&$( printf -- ' %q' "$@" )" + schedule="$( schedule_to_cron "$schedule" )" -echo "$script" > /task.sh -chmod +x /task.sh + make_task_script "$@" > /task.sh + chmod +x /task.sh -# Run the task once when the container is started, regardless of schedule. -/task.sh + # Run the task once when the container is started, regardless of schedule. + /task.sh -crontab="$schedule /task.sh + local crontab + crontab="$schedule /task.sh # This is the new crontab." -echo "$crontab" | crontab - + echo "$crontab" | crontab - + exec crond -f +} + +main() { + setup_cron_task "$@" +} -exec crond -f +main "$@" |