From 7ae0325a2d19a6866b599090d89b3706ac213263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Thu, 3 Mar 2022 00:05:14 +0100 Subject: [PATCH] externalize schedule randomization --- libexec/kiwi-cron/create_schedule | 30 ++++++---------------------- libexec/kiwi-cron/randomize_schedule | 25 +++++++++++++++++++++++ libexec/kiwi-cron/scheduler | 29 +++++++++++++++++++++------ 3 files changed, 54 insertions(+), 30 deletions(-) create mode 100755 libexec/kiwi-cron/randomize_schedule diff --git a/libexec/kiwi-cron/create_schedule b/libexec/kiwi-cron/create_schedule index 5a09075..2d31a64 100755 --- a/libexec/kiwi-cron/create_schedule +++ b/libexec/kiwi-cron/create_schedule @@ -13,41 +13,23 @@ fi # generate valid random schedule values # generally, "nighttime" and "weekend" will be preferred. -# shellcheck disable=SC3028 # RANDOM is defined in alpine's /bin/sh -cs_rand_min=$(( RANDOM % 60 )) - -# shellcheck disable=SC3028 -# cs_rand_hour=$(( RANDOM % 24 )) -cs_rand_hour=$(( (21 + RANDOM % 7) % 24 )) - -# shellcheck disable=SC3028 -cs_rand_day=$(( RANDOM % 31 + 1 )) - -# shellcheck disable=SC3028 -# cs_rand_weekday=$(( RANDOM % 7 )) -cs_rand_weekday=$(( (6 + RANDOM % 2) % 7 )) - -# shellcheck disable=SC3028 -# cs_rand_month=$(( RANDOM % 12 + 1 )) -cs_rand_month=$(( RANDOM % 2 + 1 )) - case "${cs_units}" in minute) - echo "*${cs_every}" "*" "*" "*" "*" "${cs_command}" + echo "*${cs_every} * * * * ${cs_command}" ;; hour) - echo "${cs_rand_min}" "*${cs_every}" "*" "*" "*" "${cs_command}" + echo "R *${cs_every} * * * ${cs_command}" ;; day) - echo "${cs_rand_min}" "${cs_rand_hour}" "*${cs_every}" "*" "*" "${cs_command}" + echo "R R *${cs_every} * * ${cs_command}" ;; week) - echo "${cs_rand_min}" "${cs_rand_hour}" "*" "*" "${cs_rand_weekday}" "${cs_command}" + echo "R R * * R ${cs_command}" ;; month) - echo "${cs_rand_min}" "${cs_rand_hour}" "${cs_rand_day}" "*${cs_every}" "*" "${cs_command}" + echo "R R R *${cs_every} * ${cs_command}" ;; year) - echo "${cs_rand_min}" "${cs_rand_hour}" "${cs_rand_day}" "${cs_rand_month}" "*" "${cs_command}" + echo "R R R R * ${cs_command}" ;; esac diff --git a/libexec/kiwi-cron/randomize_schedule b/libexec/kiwi-cron/randomize_schedule new file mode 100755 index 0000000..c5e938a --- /dev/null +++ b/libexec/kiwi-cron/randomize_schedule @@ -0,0 +1,25 @@ +#!/bin/sh + +# shellcheck disable=SC3028 # RANDOM is defined in alpine's /bin/sh + +read -r rs_minute rs_hour rs_day rs_month rs_weekday rs_command + +[ "${rs_minute}" = "R" ] && \ + rs_minute=$(( RANDOM % 60 )) + +[ "${rs_hour}" = "R" ] && \ + rs_hour=$(( (21 + RANDOM % 7) % 24 )) # prefer nighttime + # rs_hour=$(( RANDOM % 24 )) # don't prefer nighttime + +[ "${rs_day}" = "R" ] && \ + rs_day=$(( RANDOM % 28 + 1 )) # don't mess with February 31st + +[ "${rs_month}" = "R" ] && \ + rs_month=$(( RANDOM % 2 + 1 )) # prefer January & February + # rs_month=$(( RANDOM % 12 + 1 )) # don't prefer January & February + +[ "${rs_weekday}" = "R" ] && \ + rs_weekday=$(( (6 + RANDOM % 2) % 7 )) # prefer weekends + # rs_weekday=$(( RANDOM % 7 )) # don't prefer weekends + +echo "${rs_minute} ${rs_hour} ${rs_day} ${rs_month} ${rs_weekday} ${rs_command}" diff --git a/libexec/kiwi-cron/scheduler b/libexec/kiwi-cron/scheduler index e9b79aa..aca5381 100755 --- a/libexec/kiwi-cron/scheduler +++ b/libexec/kiwi-cron/scheduler @@ -1,5 +1,7 @@ #!/bin/sh +# shellcheck disable=SC3001 # process substitution are defined in alpine's /bin/sh + this_script="$( readlink -f "${0}" )" this_dir="${this_script%/*}" @@ -42,7 +44,7 @@ schedule_dirs="$( )" for schedule_dir in ${schedule_dirs}; do - # ignore if no files + # ignore dirs without files if dir_has_no_files "${schedule_dir}"; then continue fi @@ -52,7 +54,9 @@ for schedule_dir in ${schedule_dirs}; do units="${dir_name%ly}" units="$( dir_to_unit "${units}" )" - "${this_dir}/create_schedule" "1" "${units}" "run-parts '${schedule_dir}'" + "${this_dir}/create_schedule" \ + "1" "${units}" "run-parts '${schedule_dir}'" \ + | "${this_dir}/randomize_schedule" done # check dirs in "/kiwi-cron/every" directory @@ -61,16 +65,29 @@ every_schedule_dirs="$( )" for schedule_dir in ${every_schedule_dirs}; do - # ignore if no files + # ignore dirs without files if dir_has_no_files "${schedule_dir}"; then continue fi # infer schedule params by directory name dir_name="${schedule_dir##*/}" - every="$( echo "${dir_name}" | awk -F_ '{print $1}' )" - units="$( echo "${dir_name}" | awk -F_ '{print $2}' )" + case "${dir_name}" in + *_*) # looks like: every/5_minutes + read -r every units < \ + <( echo "${dir_name}" | tr '_' ' ' ) + ;; + + *) # probably looks like: every/day + every="1" + units="${dir_name}" + ;; + esac + + # remove trailing "s" if present units="${units%s}" - "${this_dir}/create_schedule" "${every}" "${units}" "run-parts '${schedule_dir}'" + "${this_dir}/create_schedule" \ + "${every}" "${units}" "run-parts '${schedule_dir}'" \ + | "${this_dir}/randomize_schedule" done