schedule script

master
Ken Barbour 2022-07-05 17:02:13 -04:00
parent 413b1fc53c
commit d693dfe4af
2 changed files with 170 additions and 0 deletions

103
schedule.sh 100755
View File

@ -0,0 +1,103 @@
#!/bin/bash
## Schedules upcoming passes
##
## usage: __PROG__ [options]
##
set -o pipefail
prog="$0"
me=${HELP:-$(basename "${prog}")}
rootdir=$(dirname $(realpath ${BASH_SOURCE}))
source "${rootdir}/lib/utils.sh"
function process_args() {
while (( "$#" ))
do
case $1 in
## --hours <integer>
'--hours')
hours=${2}
shift
;;
## --dir <path> Directory to place data (default: ./<month>/<day>)
'--dir')
output_path=${2}
shift
;;
## --help, -h
'--help' | '-h')
usage
exit
;;
*)
logerr "Unknown argument %s" ${1}
exit 1
;;
esac
shift
done
}
# Reformat a satellite name from 'NOAA 15' to '--noaa-15'
# @param string satellite
# @output string to STDOUT
function satellite_name_flag() {
local satellite=${1}
echo $satellite |
sed -e 's/\(.*\)/\L\1/' |
sed -e 's/[[:blank:]]\+/-/g' |
sed -e 's/^/--/'
}
function schedule_pass() {
local time=${2}
local duration=${3}
local satellite=${*:4}
local at=${1}
${at:-'at'} -q w -t "$(echo $time | cut -d'.' -f1)" <<EOF 2>&1 2>&1 | grep -oP '(?<=job\s)[0-9]+' >> wxrx-jobs
sleep $(echo ${time} | cut -d'.' -f2)
wxrx run $(satellite_name_flag "${satellite}") --duration ${duration} >> ./wxrx-log
EOF
}
# @param string mock for `atd`
function schedule_passes() {
local at=${1:-'at'}
while read -r line; do
schedule_pass 'mock_at' ${line}
done
}
function unschedule_passes() {
# local jobfile=${1:-'.wxrx-jobs'}
# for line in $(cat ${jobfile}); do
# atrm $line
# done
# >${jobfile}
atrm $(atq -q w | cut -f1)
}
# if sourced, return here. the rest of this script has side effects
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
return
fi
# -- Default Values --
hours=${hours:=24}
# -- Do work below here --
process_args $@
(cd ${output_path:-.}; unschedule_passes; wxrx predict --look-ahead "${hours}" --date-format '+%Y%m%d%H%M.%S' |
schedule_passes)

View File

@ -0,0 +1,67 @@
#!/usr/bin/bash
prog="$0"
me=`basename "$prog"`
unit=$(realpath $(dirname "$0")/../schedule.sh)
fixture_dir=$(realpath $(dirname "$0")/fixtures)
printf "WARNING! The unit under test may use atd. Check your atd queues.\n"
setUp() {
cd "${SHUNIT_TMPDIR}"
cp ${fixture_dir}/satellites.tle ${SHUNIT_TMPDIR}
stdoutF="${SHUNIT_TMPDIR}/stdout"
stderrF="${SHUNIT_TMPDIR}/stderr"
>"${stdoutF}"
>"${stderrF}"
}
mock_i=1
mock_at() {
cat > ${SHUNIT_TMPDIR}/at-input
printf "job %s at Foo Bar\n" "${mock_i}"
echo "FOO!" 1>&2
mock_i=`expr ${mock_i} + 1`
}
test_satellite_name_flag() {
source ${unit}
assertSame "--noaa-15" "`satellite_name_flag 'NOAA 15'`"
assertSame "--noaa-16" "`satellite_name_flag 'NOAA 16'`"
assertSame "--noaa-19" "`satellite_name_flag 'NOAA 19'`"
}
test_schedule_pass() {
source ${unit}
schedule_pass 'mock_at' '202207052000.05' '1234' 'NOAA-17' >${stdoutF} 2>${stderrF}
assertTrue "unexpected error status" $?
assertNull "unexpected error output" "`cat ${stderrF}`"
assertNull "unexpected stdout" "`cat ${stdoutF}`"
assertNotNull "no input to at" "`cat ${SHUNIT_TMPDIR}/at-input`"
cat <<EOF >expected
sleep 05
wxrx run --noaa-17 --duration 1234 >> ./wxrx-log
EOF
assertSame "`cat expected`" "`cat ${SHUNIT_TMPDIR}/at-input`"
}
test_schedule_passes() {
source ${unit}
schedule_passes 'mock_at' <<EOF > "${stdoutF}" 2>"${stderrF}"
202207052019.43 917 NOAA 15
202207052249.35 946 NOAA 18
202207060837.35 904 NOAA 15
202207060936.43 932 NOAA 19
EOF
assertTrue "unexpected error status" $?
assertNull "unexpected error output" "`cat ${stderrF}`"
cat ${stderrF}
cat ${stdoutF}
}
. shunit2