Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 31 additions & 2 deletions src/scheduler.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2
3