@@ -16,6 +16,7 @@ progress_extensions="${IB_GATEWAY_RECOVERY_PROGRESS_EXTENSIONS:-2}"
1616progress_window_seconds=" ${IB_GATEWAY_RECOVERY_PROGRESS_WINDOW_SECONDS:- 420} "
1717progress_regex=" ${IB_GATEWAY_RECOVERY_PROGRESS_REGEX:- IBC: (Starting Gateway|Login attempt|Second Factor Authentication|Login has completed|Configuration tasks completed|Found Gateway main window|Getting config dialog|Getting main window)|Authentication window found|Auto-fill submitted|Passed token authentication|Authentication completed|Security code: } "
1818terminal_regex=" ${IB_GATEWAY_RECOVERY_TERMINAL_REGEX:- Connection reset by peer|Server disconnected|IBC: .* (Authentication|Login).* (timed out|timeout|failed)|IBC: .* (timed out|timeout).* (Authentication|Login)} "
19+ activity_classifier=" ${script_dir} /classify_ib_gateway_activity.awk"
1920lock_file=" ${IB_GATEWAY_RECOVERY_LOCK_FILE:-/ var/ lock/ ib_gateway_recovery.lock} "
2021lock_wait_seconds=" ${IB_GATEWAY_RECOVERY_LOCK_WAIT_SECONDS:- 900} "
2122
@@ -42,98 +43,54 @@ wait_for_ready() {
4243 bash " ${script_dir} /wait_for_ib_gateway_ready.sh" " ${gateway_mode} "
4344}
4445
45- gateway_recently_progressing_from_docker_logs () {
46- docker logs --since " ${progress_window_seconds} s" " ${container_name} " 2>&1 \
47- | grep -Eiq " ${progress_regex} "
46+ gateway_recent_activity_from_docker_logs () {
47+ docker logs --timestamps --since " ${progress_window_seconds} s" " ${container_name} " 2>&1 \
48+ | awk -v progress_regex=" ${progress_regex} " -v terminal_regex=" ${terminal_regex} " \
49+ -f " ${activity_classifier} "
4850}
4951
50- gateway_recently_progressing_from_file_logs () {
51- docker exec " ${container_name} " sh -s -- " ${progress_window_seconds} " " ${progress_regex} " << 'SH '
52- set -eu
53-
54- progress_window_seconds="$1"
55- progress_regex="$2"
56- now="$(date +%s)"
57- cutoff_timestamp="$(date -u -d "@$((now - progress_window_seconds))" "+%Y-%m-%d %H:%M:%S")"
58-
59- for log_path in /home/ibgateway/Jts/launcher.log /home/ibgateway/2fa.log; do
60- if [ ! -f "${log_path}" ]; then
61- continue
62- fi
63-
64- log_mtime="$(stat -c %Y "${log_path}" 2>/dev/null || echo 0)"
65- if [ $((now - log_mtime)) -le "${progress_window_seconds}" ]; then
66- tail -n 400 "${log_path}" 2>/dev/null \
67- | awk -v cutoff_timestamp="${cutoff_timestamp}" -v progress_regex="${progress_regex}" '
68- substr($0, 1, 19) >= cutoff_timestamp && $0 ~ progress_regex { found = 1 }
69- END { exit found ? 0 : 1 }
70- ' && exit 0
71- fi
72- done
73-
74- exit 1
75- SH
76- }
77-
78- gateway_recently_progressing () {
79- gateway_recently_progressing_from_docker_logs || gateway_recently_progressing_from_file_logs
80- }
81-
82- gateway_recently_terminal_from_docker_logs () {
83- docker logs --since " ${progress_window_seconds} s" " ${container_name} " 2>&1 \
84- | grep -Eiq " ${terminal_regex} "
85- }
86-
87- gateway_recently_terminal_from_file_logs () {
88- docker exec " ${container_name} " sh -s -- " ${progress_window_seconds} " " ${terminal_regex} " << 'SH '
89- set -eu
90-
91- progress_window_seconds="$1"
92- terminal_regex="$2"
93- now="$(date +%s)"
94- cutoff_timestamp="$(date -u -d "@$((now - progress_window_seconds))" "+%Y-%m-%d %H:%M:%S")"
95-
96- for log_path in /home/ibgateway/Jts/launcher.log /home/ibgateway/2fa.log; do
97- if [ ! -f "${log_path}" ]; then
98- continue
99- fi
100-
101- log_mtime="$(stat -c %Y "${log_path}" 2>/dev/null || echo 0)"
102- if [ $((now - log_mtime)) -le "${progress_window_seconds}" ]; then
103- tail -n 400 "${log_path}" 2>/dev/null \
104- | awk -v cutoff_timestamp="${cutoff_timestamp}" -v terminal_regex="${terminal_regex}" '
105- substr($0, 1, 19) >= cutoff_timestamp && $0 ~ terminal_regex { found = 1 }
106- END { exit found ? 0 : 1 }
107- ' && exit 0
108- fi
109- done
110-
111- exit 1
112- SH
52+ gateway_recent_activity_from_file_logs () {
53+ local cutoff_timestamp
54+ local log_path
55+ local now
56+ now=" $( date +%s) "
57+ cutoff_timestamp=" $( date -u -d " @$(( now - progress_window_seconds)) " " +%Y-%m-%d %H:%M:%S" ) "
58+
59+ for log_path in /home/ibgateway/Jts/launcher.log /home/ibgateway/2fa.log; do
60+ { docker exec " ${container_name} " tail -n 400 " ${log_path} " 2> /dev/null || true ; } \
61+ | awk -v cutoff_timestamp=" ${cutoff_timestamp} " \
62+ -v progress_regex=" ${progress_regex} " -v terminal_regex=" ${terminal_regex} " \
63+ -f " ${activity_classifier} "
64+ done
11365}
11466
115- gateway_recently_terminal () {
116- gateway_recently_terminal_from_docker_logs || gateway_recently_terminal_from_file_logs
67+ gateway_recent_activity () {
68+ {
69+ gateway_recent_activity_from_docker_logs || true
70+ gateway_recent_activity_from_file_logs || true
71+ } | LC_ALL=C sort | tail -n 1 | cut -f 2
11772}
11873
11974wait_for_ready_with_progress () {
12075 local timeout_seconds=" $1 "
12176 local stage=" $2 "
12277 local extension=0
78+ local activity
12379
12480 if wait_for_ready " ${timeout_seconds} " ; then
12581 return 0
12682 fi
12783
12884 while [ " ${extension} " -lt " ${progress_extensions} " ]; do
129- if gateway_recently_terminal; then
130- echo " Recent terminal IB gateway authentication failure detected after ${stage} wait; skipping progress extension." >&2
131- return 1
132- fi
133-
134- if ! gateway_recently_progressing; then
135- return 1
136- fi
85+ activity=" $( gateway_recent_activity) "
86+ case " ${activity} " in
87+ terminal)
88+ echo " Recent terminal IB gateway authentication failure detected after ${stage} wait; skipping progress extension." >&2
89+ return 1
90+ ;;
91+ progress) ;;
92+ * ) return 1 ;;
93+ esac
13794
13895 extension=$(( extension + 1 ))
13996 echo " Recent IB gateway login/config progress detected after ${stage} wait; extending readiness wait (${extension} /${progress_extensions} ) by ${progress_wait_seconds} s before external recovery." >&2
0 commit comments