1
0
Fork 0
mirror of https://github.com/yavook/kiwi-cron.git synced 2025-04-07 08:23:01 +00:00
kiwi-cron/libexec/kiwi-cron/scheduler

76 lines
1.6 KiB
Bash
Executable file

#!/bin/sh
this_script="$( readlink -f "${0}" )"
this_dir="${this_script%/*}"
dir_has_no_files () {
dhnf_directory="${1}"
# count files in directory
dhnf_filecount="$(
find "${dhnf_directory}" -type f -mindepth 1 -maxdepth 1 \
| wc -l
)"
# true if empty
test "${dhnf_filecount}" -eq "0"
}
dir_to_unit () {
dtu_dirname="${1}"
case "${dtu_dirname}" in
# truncated "daily"
dai)
echo "day"
;;
# truncated "annually"
annual)
echo "year"
;;
*)
echo "${dtu_dirname}"
;;
esac
}
# check *ly dirs in "/kiwi-cron" directory
schedule_dirs="$(
find "/kiwi-cron" -type d -name "*ly" -mindepth 1 -maxdepth 1
)"
for schedule_dir in ${schedule_dirs}; do
# ignore if no files
if dir_has_no_files "${schedule_dir}"; then
continue
fi
# infer units by directory name
dir_name="${schedule_dir##*/}"
units="${dir_name%ly}"
units="$( dir_to_unit "${units}" )"
"${this_dir}/create_schedule" "1" "${units}" "run-parts '${schedule_dir}'"
done
# check dirs in "/kiwi-cron/every" directory
every_schedule_dirs="$(
find "/kiwi-cron/every" -type d -mindepth 1 -maxdepth 1
)"
for schedule_dir in ${every_schedule_dirs}; do
# ignore if no 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}' )"
units="${units%s}"
"${this_dir}/create_schedule" "${every}" "${units}" "run-parts '${schedule_dir}'"
done