-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon.sh
More file actions
executable file
·272 lines (244 loc) · 6.13 KB
/
daemon.sh
File metadata and controls
executable file
·272 lines (244 loc) · 6.13 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env bash
set -euo pipefail
BRIDGE_HOME="${CODEX_FEISHU_HOME:-$HOME/.codex-feishu}"
SKILL_DIR="$(cd "$(dirname "$0")/.." && pwd)"
PID_FILE="$BRIDGE_HOME/runtime/bridge.pid"
STATUS_FILE="$BRIDGE_HOME/runtime/status.json"
LOG_FILE="$BRIDGE_HOME/logs/bridge.log"
DAEMON_FILE="$SKILL_DIR/dist/daemon.mjs"
LAUNCHD_LABEL="com.codex-feishu.bridge"
LAUNCHD_PLIST="$BRIDGE_HOME/runtime/$LAUNCHD_LABEL.plist"
ensure_dirs() {
mkdir -p "$BRIDGE_HOME/data/messages" "$BRIDGE_HOME/logs" "$BRIDGE_HOME/runtime"
}
read_pid() {
[ -f "$PID_FILE" ] && cat "$PID_FILE" 2>/dev/null || true
}
pid_alive() {
local pid="${1:-}"
[ -n "$pid" ] && kill -0 "$pid" 2>/dev/null
}
status_running() {
[ -f "$STATUS_FILE" ] && grep -q '"running"[[:space:]]*:[[:space:]]*true' "$STATUS_FILE" 2>/dev/null
}
ensure_built() {
local need_build=0
if [ ! -f "$DAEMON_FILE" ]; then
need_build=1
else
local newest_src
newest_src=$(find "$SKILL_DIR/src" -name '*.ts' -newer "$DAEMON_FILE" 2>/dev/null | head -1 || true)
if [ -n "$newest_src" ]; then
need_build=1
fi
fi
if [ "$need_build" = "1" ]; then
echo "Building daemon bundle..."
(cd "$SKILL_DIR" && npm run build)
fi
}
load_config_env() {
if [ -f "$BRIDGE_HOME/config.env" ]; then
set -a
# shellcheck disable=SC1090
source "$BRIDGE_HOME/config.env"
set +a
fi
}
show_last_exit_reason() {
if [ -f "$STATUS_FILE" ]; then
local reason
reason=$(grep -o '"lastExitReason"[[:space:]]*:[[:space:]]*"[^"]*"' "$STATUS_FILE" 2>/dev/null | head -1 | sed 's/.*: *"//;s/"$//')
[ -n "$reason" ] && echo "Last exit reason: $reason"
fi
}
is_macos() {
[ "$(uname -s)" = "Darwin" ]
}
launchd_target() {
echo "gui/$(id -u)/$LAUNCHD_LABEL"
}
launchd_write_plist() {
cat >"$LAUNCHD_PLIST" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>$LAUNCHD_LABEL</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>$SKILL_DIR/scripts/run-bridge.sh</string>
</array>
<key>EnvironmentVariables</key>
<dict>
<key>CODEX_FEISHU_HOME</key>
<string>$BRIDGE_HOME</string>
</dict>
<key>WorkingDirectory</key>
<string>$SKILL_DIR</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>$LOG_FILE</string>
<key>StandardErrorPath</key>
<string>$LOG_FILE</string>
</dict>
</plist>
EOF
}
launchd_is_loaded() {
launchctl print "$(launchd_target)" >/dev/null 2>&1
}
launchd_pid() {
launchctl print "$(launchd_target)" 2>/dev/null | awk '/pid = / { print $3; exit }'
}
start_bridge() {
ensure_dirs
ensure_built
local pid
pid=$(read_pid)
if is_macos && launchd_is_loaded; then
local existing_pid
existing_pid=$(launchd_pid || true)
echo "Bridge already running${existing_pid:+ (PID: $existing_pid)}"
[ -f "$STATUS_FILE" ] && cat "$STATUS_FILE"
exit 1
fi
if ! is_macos && pid_alive "$pid"; then
echo "Bridge already running (PID: $pid)"
[ -f "$STATUS_FILE" ] && cat "$STATUS_FILE"
exit 1
fi
rm -f "$PID_FILE"
rm -f "$STATUS_FILE"
load_config_env
echo "Starting bridge..."
local new_pid=""
if is_macos; then
launchd_write_plist
launchctl bootout "gui/$(id -u)" "$LAUNCHD_PLIST" >/dev/null 2>&1 || true
launchctl bootstrap "gui/$(id -u)" "$LAUNCHD_PLIST"
launchctl kickstart -k "$(launchd_target)"
else
nohup node "$DAEMON_FILE" >>"$LOG_FILE" 2>&1 &
new_pid=$!
echo "$new_pid" >"$PID_FILE"
fi
local started=0
for _ in $(seq 1 12); do
sleep 1
if status_running; then
started=1
break
fi
if is_macos; then
if ! launchd_is_loaded; then
break
fi
elif ! pid_alive "$new_pid"; then
break
fi
done
if [ "$started" = "1" ]; then
if is_macos; then
new_pid=$(read_pid)
if [ -z "$new_pid" ]; then
new_pid=$(launchd_pid || true)
fi
[ -n "$new_pid" ] && echo "$new_pid" >"$PID_FILE"
fi
echo "Bridge started (PID: $new_pid)"
cat "$STATUS_FILE"
exit 0
fi
echo "Failed to start bridge."
if is_macos; then
if ! launchd_is_loaded; then
echo "launchd service failed to stay loaded."
fi
elif ! pid_alive "$new_pid"; then
echo "Process exited during startup."
fi
show_last_exit_reason
echo ""
echo "Recent logs:"
tail -20 "$LOG_FILE" 2>/dev/null || true
exit 1
}
stop_bridge() {
local pid
pid=$(read_pid)
if [ -z "$pid" ]; then
if is_macos && launchd_is_loaded; then
pid="$(launchd_pid || true)"
else
echo "No bridge running"
exit 0
fi
fi
if is_macos; then
echo "Stopping bridge${pid:+ (PID: $pid)}..."
launchctl bootout "gui/$(id -u)" "$LAUNCHD_PLIST" >/dev/null 2>&1 || launchctl bootout "$(launchd_target)" >/dev/null 2>&1 || true
elif pid_alive "$pid"; then
echo "Stopping bridge (PID: $pid)..."
kill "$pid"
for _ in $(seq 1 10); do
if ! pid_alive "$pid"; then
break
fi
sleep 1
done
if pid_alive "$pid"; then
kill -9 "$pid"
fi
else
echo "Bridge was not running (stale PID file)"
fi
rm -f "$PID_FILE"
echo "Bridge stopped"
}
show_status() {
local pid
pid=$(read_pid)
if is_macos && launchd_is_loaded; then
pid=$(launchd_pid || true)
echo "Bridge is registered with launchd ($LAUNCHD_LABEL)"
[ -n "$pid" ] && echo "Bridge process is running (PID: $pid)"
elif pid_alive "$pid"; then
echo "Bridge process is running (PID: $pid)"
else
echo "Bridge is not running"
[ -f "$PID_FILE" ] && rm -f "$PID_FILE"
fi
if [ -f "$STATUS_FILE" ]; then
cat "$STATUS_FILE"
else
show_last_exit_reason
fi
}
show_logs() {
local lines="${1:-50}"
tail -n "$lines" "$LOG_FILE" 2>/dev/null | sed -E 's/(token|secret|password)(["'"'"']?\s*[:=]\s*["'"'"']?)[^ "]+/\1\2*****/gi'
}
case "${1:-help}" in
start)
start_bridge
;;
stop)
stop_bridge
;;
status)
show_status
;;
logs)
show_logs "${2:-50}"
;;
*)
echo "Usage: daemon.sh {start|stop|status|logs [N]}"
exit 1
;;
esac