feat: 15 minute grace period for alarms, or day grace period for yearly alarms

This commit is contained in:
Lewis Wynne 2026-04-02 00:58:44 +01:00
parent 42a355a55f
commit daf816d919
2 changed files with 66 additions and 3 deletions

18
nag
View file

@ -1165,9 +1165,25 @@ check() {
if (( _timestamp <= _now ))
then
# Fire it.
local _age=$(( _now - _timestamp ))
local _should_fire=0
if (( _age <= 900 ))
then
# Within 15 minutes, fire.
_should_fire=1
elif [[ "${_rule}" == "year" ]] && \
[[ "$(date -d "@${_timestamp}" +%Y-%m-%d)" == "$(date +%Y-%m-%d)" ]]
then
# Yearly alarm, still the same day.
_should_fire=1
fi
if (( _should_fire ))
then
${NAG_CMD} "nag" "${_message}" || _warn printf "Failed to notify: %s\\n" "${_message}"
_play_sound
fi
if [[ -n "${_rule}" ]]
then

View file

@ -298,6 +298,53 @@ SCRIPT
[[ ! -s "${NAG_DIR}/alarms" ]] || [ "$(wc -l < "${NAG_DIR}/alarms")" -eq 0 ]
}
@test "check silently drops stale one-shot (older than 15 min)" {
local _stale_ts=$(( $(date +%s) - 1800 )) # 30 minutes ago
write_alarm "$(printf "1\t%s\t\tstale alarm" "${_stale_ts}")"
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 ]
# Should NOT have fired.
[ ! -f "${_fired}" ]
# Should still be removed.
[[ ! -s "${NAG_DIR}/alarms" ]] || [ "$(wc -l < "${NAG_DIR}/alarms")" -eq 0 ]
}
@test "check silently reschedules stale repeating alarm" {
local _stale_ts=$(( $(date +%s) - 1800 )) # 30 minutes ago
write_alarm "$(printf "1\t%s\tday\tstale repeater" "${_stale_ts}")"
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 ]
# Should NOT have fired.
[ ! -f "${_fired}" ]
# Should be rescheduled to a future time.
[ -s "${NAG_DIR}/alarms" ]
local _new_ts
_new_ts="$(cut -f2 "${NAG_DIR}/alarms" | head -1)"
(( _new_ts > $(date +%s) ))
}
@test "help check shows check usage" {
run_nag help check
[ "${status}" -eq 0 ]