From f0b831b6fabc00dbc5217336da2b8013f309d3a2 Mon Sep 17 00:00:00 2001 From: Steven Massaro Date: Fri, 30 May 2025 08:56:25 -0500 Subject: [PATCH] adjust delay to account for runtime length --- README.md | 11 ++++++----- src/scheduler.sh | 33 +++++++++++++++++++++++++++++++-- version.txt | 2 +- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5a70737..5dafc64 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,12 @@ A simple Docker image that repeatedly runs a command with a specified delay. #### Environment variables: -| Name | Required | Default value | Example value | Description | -|-----------------|----------|---------------|---------------------------------------------------------------|-------------------------------------------------| -| `COMMAND` | Yes | | `echo Hello` | | -| `AFTER_COMMAND` | No | | `echo Hello after` | Command that should be executed after `COMMAND` | -| `DELAY` | No | `1d` | `7d` (or any other permissible value for the `sleep` command) | | +| Name | Required | Default value | Example value | Description | +|----------------------|----------|---------------|---------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `COMMAND` | Yes | | `echo Hello` | | +| `AFTER_COMMAND` | No | | `echo Hello after` | Command that should be executed after `COMMAND` | +| `DELAY` | No | `1d` | `7d` (or any other permissible value for the `sleep` command) | | +| `ADJUST_FOR_RUNTIME` | No | `true` | `false` | If set to true, subtracts the execution time of the task from the delay, ensuring the scheduler runs at consistent intervals (e.g., the same time each day). | ### As the base image of another project diff --git a/src/scheduler.sh b/src/scheduler.sh index dd137ae..5e99fcd 100644 --- a/src/scheduler.sh +++ b/src/scheduler.sh @@ -1,10 +1,39 @@ #!/bin/sh echo "$(date) - start scheduler" + +# Convert a duration like "1d" or "3600" into seconds +duration_to_seconds() { + case "$1" in + *d) echo $(( ${1%d} * 86400 )) ;; + *h) echo $(( ${1%h} * 3600 )) ;; + *m) echo $(( ${1%m} * 60 )) ;; + *s) echo $(( ${1%s} )) ;; + *) echo "$1" ;; # assume seconds if no suffix + esac +} + +DELAY="${DELAY:-1d}" +ADJUST_FOR_RUNTIME="${ADJUST_FOR_RUNTIME:-true}" + while :; do echo "$(date) - execute" + start_time=$(date +%s) ./execute.sh + end_time=$(date +%s) + + if [ "$ADJUST_FOR_RUNTIME" = "true" ]; then + runtime=$(( end_time - start_time )) + delay_seconds=$(duration_to_seconds "$DELAY") + sleep_time=$(( delay_seconds - runtime )) + if [ "$sleep_time" -lt 0 ]; then + echo "$(date) - Execution took longer than the delay ($runtime > $delay_seconds), skipping sleep" + sleep_time=0 + fi + else + sleep_time=$delay_seconds + fi - echo "$(date) - sleep for ${DELAY:-1d}" - sleep "${DELAY:-1d}" + echo "$(date) - sleeping for $sleep_time seconds" + sleep "$sleep_time" done diff --git a/version.txt b/version.txt index d8263ee..e440e5c 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -2 \ No newline at end of file +3 \ No newline at end of file