diff --git a/nag b/nag index b2812b8..003b912 100755 --- a/nag +++ b/nag @@ -651,20 +651,20 @@ _next_for_rule() { _time_of_day="$(date -d "@${_timestamp}" +%H:%M:%S)" case "${_rule}" in - hourly) printf "%s" "$((_timestamp + 3600))" ;; - daily) date -d "$(date -d "@${_timestamp}" +%Y-%m-%d) + 1 day ${_time_of_day}" +%s ;; - weekly) _next_matching_day "${_timestamp}" "${_time_of_day}" "$(date -d "@${_timestamp}" +%u)" ;; + hour) printf "%s" "$((_timestamp + 3600))" ;; + day) date -d "$(date -d "@${_timestamp}" +%Y-%m-%d) + 1 day ${_time_of_day}" +%s ;; + week) _next_matching_day "${_timestamp}" "${_time_of_day}" "$(date -d "@${_timestamp}" +%u)" ;; weekday) _next_matching_day "${_timestamp}" "${_time_of_day}" "1 2 3 4 5" ;; weekend) _next_matching_day "${_timestamp}" "${_time_of_day}" "6 7" ;; - mon) _next_matching_day "${_timestamp}" "${_time_of_day}" "1" ;; - tue) _next_matching_day "${_timestamp}" "${_time_of_day}" "2" ;; - wed) _next_matching_day "${_timestamp}" "${_time_of_day}" "3" ;; - thu) _next_matching_day "${_timestamp}" "${_time_of_day}" "4" ;; - fri) _next_matching_day "${_timestamp}" "${_time_of_day}" "5" ;; - sat) _next_matching_day "${_timestamp}" "${_time_of_day}" "6" ;; - sun) _next_matching_day "${_timestamp}" "${_time_of_day}" "7" ;; - monthly) _next_month "${_timestamp}" "${_time_of_day}" ;; - yearly) _next_year "${_timestamp}" "${_time_of_day}" ;; + mon) _next_matching_day "${_timestamp}" "${_time_of_day}" "1" ;; + tue) _next_matching_day "${_timestamp}" "${_time_of_day}" "2" ;; + wed) _next_matching_day "${_timestamp}" "${_time_of_day}" "3" ;; + thu) _next_matching_day "${_timestamp}" "${_time_of_day}" "4" ;; + fri) _next_matching_day "${_timestamp}" "${_time_of_day}" "5" ;; + sat) _next_matching_day "${_timestamp}" "${_time_of_day}" "6" ;; + sun) _next_matching_day "${_timestamp}" "${_time_of_day}" "7" ;; + month) _next_month "${_timestamp}" "${_time_of_day}" ;; + year) _next_year "${_timestamp}" "${_time_of_day}" ;; esac } @@ -1072,6 +1072,67 @@ stop() { printf "Stopped alarm %s.\\n" "${_target_id}" } +# check ####################################################################### + +describe "check" < "${_ALARMS_FILE}" + else + _ALARMS=("${_new_alarms[@]}") + _write_alarms + fi + _release_lock +} + # at ########################################################################## describe "at" <> "${NAG_DIR}/alarms" +} + +@test "check fires expired one-shot and removes it" { + local _past_ts=$(( $(date +%s) - 60 )) + write_alarm "$(printf "1\t%s\t\tpast alarm" "${_past_ts}")" + + # Record what was fired. + local _fired="${NAG_DIR}/fired" + export NAG_CMD="${NAG_DIR}/recorder" + cat > "${NAG_CMD}" <<'SCRIPT' +#!/usr/bin/env bash +printf "%s\n" "$2" >> "${NAG_DIR}/fired" +SCRIPT + chmod +x "${NAG_CMD}" + + run "${_NAG}" check + [ "${status}" -eq 0 ] + + # Alarm should have been removed. + [[ ! -s "${NAG_DIR}/alarms" ]] || [ "$(wc -l < "${NAG_DIR}/alarms")" -eq 0 ] + + # Notification should have fired. + grep -q "past alarm" "${_fired}" +} + +@test "check reschedules expired repeating alarm" { + local _past_ts=$(( $(date +%s) - 60 )) + write_alarm "$(printf "1\t%s\tday\tdaily alarm" "${_past_ts}")" + + run "${_NAG}" check + [ "${status}" -eq 0 ] + + # Alarm should still exist with a future timestamp. + [ -s "${NAG_DIR}/alarms" ] + local _new_ts + _new_ts="$(cut -f2 "${NAG_DIR}/alarms" | head -1)" + local _now + _now="$(date +%s)" + (( _new_ts > _now )) +} + +@test "check does not fire future alarms" { + local _future_ts=$(( $(date +%s) + 3600 )) + write_alarm "$(printf "1\t%s\t\tfuture alarm" "${_future_ts}")" + + run "${_NAG}" check + [ "${status}" -eq 0 ] + + # Alarm should still be there, unchanged. + [ -s "${NAG_DIR}/alarms" ] + grep -q "future alarm" "${NAG_DIR}/alarms" +} + +@test "check fires all missed alarms" { + local _past1=$(( $(date +%s) - 120 )) + local _past2=$(( $(date +%s) - 60 )) + write_alarm "$(printf "1\t%s\t\tmissed one" "${_past1}")" + write_alarm "$(printf "2\t%s\t\tmissed two" "${_past2}")" + + local _fired="${NAG_DIR}/fired" + export NAG_CMD="${NAG_DIR}/recorder" + cat > "${NAG_CMD}" <<'SCRIPT' +#!/usr/bin/env bash +printf "%s\n" "$2" >> "${NAG_DIR}/fired" +SCRIPT + chmod +x "${NAG_CMD}" + + run "${_NAG}" check + [ "${status}" -eq 0 ] + + # Both should have fired. + grep -q "missed one" "${_fired}" + grep -q "missed two" "${_fired}" + + # Both should be removed (one-shots). + [[ ! -s "${NAG_DIR}/alarms" ]] || [ "$(wc -l < "${NAG_DIR}/alarms")" -eq 0 ] +} + +@test "help check shows check usage" { + run "${_NAG}" help check + [ "${status}" -eq 0 ] + [[ "${output}" =~ "Usage:" ]] + [[ "${output}" =~ "check" ]] +}