blob: 6c3d5c901cc103f9a56930f2cb1f8e0fbdb2567b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#!/bin/sh
# gn-indexer-run — the cron wrapper: acquire the per-index single-instance
# flock, record the hold start, then hand off to the indexer (which does
# all the work and decides nothing about schedule or mutex).
#
# SUPERSEDES DESIGN-CRON §1's crontab-level `flock`: a single flock inside
# this wrapper is the authoritative mutex. Do NOT also wrap the crontab
# line in flock — two locks would defeat the held-since bookkeeping (and
# the wrapper's own skip log).
#
# usage: gn-indexer-run <delta|sweep|full> --index PATH [indexer args…]
# * `--index PATH` (or GN4_INDEXER_INDEX) is REQUIRED: the wrapper needs
# it both for the lock path and to pass it through to the indexer.
# * `sweep` maps to the indexer's `incremental` (the verification sweep).
# * lock file: $GN4_INDEXER_LOCK else <index>.lock
# * hold-start file: $GN4_INDEXER_HELD else <index>.lock.held
# * hung-holder ERROR threshold: GN4_INDEXER_WORST_CASE_MINUTES (99)
#
# The kernel releases the flock when the holder exits — including kill -9 —
# so there is no pidfile, no operator unlock, and no queue.
#
# crontab (install is the operator's job):
# */10 * * * * /srv/gn4/bin/gn-indexer-run delta --index /srv/gn4/index
# 0 */3 * * * /srv/gn4/bin/gn-indexer-run sweep --index /srv/gn4/index
set -u
log() {
echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) gn-indexer-run[$mode_arg]: $*"
}
usage() {
echo "usage: gn-indexer-run <delta|sweep|full> --index PATH [indexer args…]" >&2
exit 2
}
[ "$#" -ge 1 ] || usage
mode=$1
shift
case "$mode" in
delta) mode_arg=delta ;;
sweep) mode_arg=incremental ;;
full) mode_arg=full ;;
*) usage ;;
esac
# Resolve --index from the remaining args (or GN4_INDEXER_INDEX).
# Both `--index PATH` and `--index=PATH` are honoured.
index=${GN4_INDEXER_INDEX:-}
prev=
for arg in "$@"; do
case "$arg" in
--index=*) index=${arg#--index=} ;;
esac
if [ "$prev" = "--index" ]; then
index=$arg
fi
prev=$arg
done
[ -n "$index" ] || usage
bin=${GN4_INDEXER_BIN:-gn-indexer}
lock=${GN4_INDEXER_LOCK:-${index}.lock}
held=${GN4_INDEXER_HELD:-${index}.lock.held}
worst_case=${GN4_INDEXER_WORST_CASE_MINUTES:-99}
# Hold the lock on fd 9 for the wrapper's lifetime. NOTE (test seam nit): on
# dash a failed `exec 9>file` redirection exits the shell before the `||`
# branch runs, so this `|| { ... }` is a safety net for shells that do
# reach it; the failure is fatal either way (the lock is never silently
# skipped).
exec 9>"$lock" || {
log "ERROR: cannot open lock file $lock"
exit 1
}
if ! flock -n 9; then
# Skip-and-log: a busy lock is expected while a long sweep runs; rc 0
# for cron. Only a holder past the worst-case threshold is an ERROR.
if [ -f "$held" ]; then
since=$(cat "$held" 2>/dev/null || echo 0)
now=$(date -u +%s)
age_min=$(( (now - since) / 60 ))
if [ "$age_min" -ge "$worst_case" ]; then
log "ERROR: skip: lock held ${age_min}min >= worst-case ${worst_case}min (hung holder?); held-since=$since"
else
log "INFO: skip: lock busy (held since $since, ${age_min}min)"
fi
else
log "INFO: skip: lock busy"
fi
exit 0
fi
# We hold the lock: record the hold start so a later skip can state the age.
now=$(date -u +%s)
printf '%s\n' "$now" >"$held"
trap 'rm -f "$held"' EXIT HUP INT TERM
# The indexer does the work; propagate its exit code (a failed run is not
# a skip). fd 9 is closed for the child so the WRAPPER is the sole lock
# holder: a kill -9 of the wrapper releases the flock immediately (E11).
"$bin" "$mode_arg" "$@" 9>&-
rc=$?
if [ "$rc" -ne 0 ]; then
log "ERROR: run failed rc=$rc"
fi
exit "$rc"
|