Repository navigation
Expand file tree
/
Copy pathsystem.sh
More file actions
executable file
·312 lines (287 loc) · 10.2 KB
/
Copy pathsystem.sh
File metadata and controls
executable file
·312 lines (287 loc) · 10.2 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env bash
#
# ShortsGenerator backend manager.
#
# Always runs `python ./Backend/main.py` inside the `shortsgenerator` conda
# env, from the repo root, no matter which shell/env you launched it from.
#
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
ENV_NAME="shortsgenerator"
PORT="${API_PORT:-8080}"
LOG_FILE="$SCRIPT_DIR/backend.log"
REQ_FILE="$SCRIPT_DIR/requirements.txt"
ENV_FILE="$SCRIPT_DIR/environment.yml"
note() { printf '\033[1;36m[system.sh]\033[0m %s\n' "$*"; }
warn() { printf '\033[1;33m[system.sh]\033[0m %s\n' "$*" >&2; }
die() { printf '\033[1;31m[system.sh]\033[0m %s\n' "$*" >&2; exit 1; }
# ------------------------------------------------------------------ help ----
cmd_help() {
cat <<EOF
Usage: ./system.sh <command> [options]
Runs the ShortsGenerator backend (Flask, port ${PORT}) inside the
'${ENV_NAME}' conda env. With no command, this help is shown.
Commands:
start [options] Start the backend in this terminal (foreground).
Verifies the conda env + dependencies first, then stops
any stale backend still holding the port (Flask's debug
reloader leaves a parent + child behind — that is how
"fixed" code can appear not to take effect).
stop Stop the backend listening on port ${PORT} (TERM, then
KILL if it lingers). Refuses to kill a foreign process
that merely happens to hold the port.
restart [options] stop, then start again. Same options as start.
status Show whether the backend is running on port ${PORT}
(pids + uptime), without touching anything.
help Show this help.
Options for start / restart:
-d, --background Detach instead of staying in the foreground: the backend
keeps running after you close the terminal. Output goes
to backend.log. A health check waits until the API
answers before this command returns.
--no-kill Do NOT stop an already running backend. If the port is
busy, start fails with a hint to run stop/restart
instead of killing anything silently.
Environment:
API_PORT Port to serve/stop/check (default: 8080).
Files:
backend.log Backend output (every start appends here).
Examples:
./system.sh Show this help.
./system.sh start Start in this terminal (Ctrl+C stops it).
./system.sh start -d Start detached (keeps running).
./system.sh stop Stop the backend.
./system.sh restart -d Restart detached.
./system.sh status Is it running?
API_PORT=8081 ./system.sh start -d
EOF
}
# ------------------------------------------------------------ port helpers ----
# Pids currently LISTENING on $PORT (any process).
port_pids() {
ss -ltnpH "sport = :$PORT" 2>/dev/null | grep -oP 'pid=\K[0-9]+' | sort -u || true
}
# Full command line of a pid (empty when already gone).
cmd_of() {
tr '\0' ' ' < "/proc/$1/cmdline" 2>/dev/null || true
}
# Pids on $PORT that are our backend (Flask reloader parent + child).
backend_pids() {
for pid in $(port_pids); do
case "$(cmd_of "$pid")" in
*Backend/main.py*) printf '%s\n' "$pid" ;;
esac
done
}
# ------------------------------------------------------------------ stop ----
cmd_stop() {
local pids
pids="$(backend_pids)"
if [ -z "$pids" ]; then
if [ -n "$(port_pids)" ]; then
die "Port $PORT is held by another process (not the backend) — not touching it."
fi
note "Backend is not running on port $PORT."
return 0
fi
for pid in $pids; do
note "Stopping backend (pid $pid)…"
kill "$pid" 2>/dev/null || true
found=1
done
# Give them a moment, then escalate leftovers to KILL.
for _ in $(seq 1 10); do
local alive=""
for pid in $pids; do
kill -0 "$pid" 2>/dev/null && alive="$alive $pid"
done
[ -z "$alive" ] && break
sleep 0.5
done
for pid in $pids; do
if kill -0 "$pid" 2>/dev/null; then
warn "pid $pid lingers — killing."
kill -9 "$pid" 2>/dev/null || true
fi
done
sleep 0.5
if [ -z "$(backend_pids)" ]; then
note "Backend stopped."
else
die "Could not stop all backend processes: $(backend_pids | tr '\n' ' ')"
fi
}
# ---------------------------------------------------------------- status ----
cmd_status() {
local pids
pids="$(backend_pids)"
if [ -z "$pids" ]; then
if [ -n "$(port_pids)" ]; then
echo "Port $PORT is busy, but NOT with the backend (pids: $(port_pids | tr '\n' ' '))."
else
echo "Backend is not running (port $PORT is free)."
fi
return 0
fi
echo "Backend is running on port $PORT:"
for pid in $pids; do
local etime
etime="$(ps -o etime= -p "$pid" 2>/dev/null | tr -d ' ' || echo '?')"
echo " pid $pid uptime $etime"
done
}
# ------------------------------------------------------ conda + dependencies -
# Needed only for start/restart (stop/status stay instant and conda-free).
ensure_env() {
# Source conda non-interactively so this works from cron, sh, or a fresh
# terminal that never ran `conda init`. Always source it: a conda executable
# on PATH without the shell functions makes `conda activate` fail.
for conda_sh in \
"$HOME/miniconda3/etc/profile.d/conda.sh" \
"$HOME/anaconda3/etc/profile.d/conda.sh" \
"$HOME/mambaforge/etc/profile.d/conda.sh" \
"$HOME/miniforge3/etc/profile.d/conda.sh" \
"/opt/conda/etc/profile.d/conda.sh"; do
if [ -f "$conda_sh" ]; then
# shellcheck disable=SC1090
. "$conda_sh"
break
fi
done
command -v conda >/dev/null 2>&1 || die "conda not found — install Miniconda/Anaconda first."
if ! conda env list | awk '{print $1}' | grep -qx "$ENV_NAME"; then
[ -f "$ENV_FILE" ] || die "conda env '$ENV_NAME' missing and $ENV_FILE not found."
note "Creating conda env '$ENV_NAME' from environment.yml (this can take a few minutes)…"
conda env create -f "$ENV_FILE"
fi
# Activate the env exactly like `conda activate shortsgenerator` would.
conda activate "$ENV_NAME"
PYTHON="$(command -v python)"
note "Python: $PYTHON"
case "$PYTHON" in
*"/envs/$ENV_NAME/"*) ;;
*) die "Wrong environment activated ($PYTHON) — expected $ENV_NAME." ;;
esac
# Missing modules (secretstorage = yt-dlp Chrome cookies, google-genai =
# Gemini API, g4f = free providers) are the usual cause of
# "it broke after a restart".
local missing
missing="$("$PYTHON" - <<'PY'
import importlib
required = [
"flask", "flask_cors", "dotenv", "termcolor", "requests",
"yt_dlp", "secretstorage", "google.genai", "g4f",
"moviepy", "cv2", "numpy",
]
missing = []
for name in required:
try:
importlib.import_module(name)
except Exception:
missing.append(name)
print(",".join(missing))
PY
)"
if [ -n "$missing" ]; then
[ -f "$REQ_FILE" ] || die "Missing modules: $missing (and no requirements.txt found)."
warn "Missing modules: $missing — installing from requirements.txt…"
"$PYTHON" -m pip install --quiet --upgrade pip
"$PYTHON" -m pip install -r "$REQ_FILE"
fi
}
# Stop stale backend processes so the new one can bind the port. With
# KILL_STALE=0 (--no-kill) a busy port is a fatal error instead.
kill_stale() {
if [ "$KILL_STALE" = "0" ]; then
if [ -n "$(port_pids)" ]; then
die "Port $PORT is already busy (pids: $(port_pids | tr '\n' ' ')). Use './system.sh stop' or 'restart' first."
fi
return 0
fi
for _attempt in $(seq 1 20); do
local pids
pids="$(port_pids)"
[ -z "$pids" ] && break
local killed=0
for pid in $pids; do
local cmd
cmd="$(cmd_of "$pid")"
[ -z "$cmd" ] && continue # vanished between ss and read
case "$cmd" in
*Backend/main.py*)
note "Stopping stale backend (pid $pid) on port $PORT…"
kill -9 "$pid" 2>/dev/null || true
killed=1
;;
*)
die "Port $PORT is used by another process (pid $pid) — stop it first, or run with API_PORT=<other>."
;;
esac
done
[ "$killed" = "1" ] || sleep 0.5
sleep 0.5
done
if ss -ltnH "sport = :$PORT" 2>/dev/null | grep -q .; then
die "Port $PORT still busy after cleanup."
fi
}
start_foreground() {
note "Starting backend on port $PORT (log: $LOG_FILE)"
note "cwd=$SCRIPT_DIR env=$ENV_NAME"
echo "------------------------------------------------------------------" >>"$LOG_FILE"
date '+%F %T starting backend (foreground)' >>"$LOG_FILE"
"$PYTHON" -u ./Backend/main.py 2>&1 | tee -a "$LOG_FILE"
}
start_background() {
note "Starting backend on port $PORT detached (log: $LOG_FILE)"
echo "------------------------------------------------------------------" >>"$LOG_FILE"
date '+%F %T starting backend (background)' >>"$LOG_FILE"
setsid nohup "$PYTHON" -u ./Backend/main.py >>"$LOG_FILE" 2>&1 </dev/null &
# Wait until the API actually answers (or give up loudly).
local ok=0
for _ in $(seq 1 45); do
if curl -sf -m 3 "http://localhost:$PORT/api/clipper/llm/settings" >/dev/null 2>&1; then
ok=1
break
fi
sleep 1
done
if [ "$ok" = "1" ]; then
note "Backend is up on port $PORT (pids: $(backend_pids | tr '\n' ' ')). Tail the log with: tail -f $LOG_FILE"
else
warn "Backend did not answer within 45s — check $LOG_FILE for the traceback."
return 1
fi
}
# ------------------------------------------------------------------ start ---
cmd_start() {
ensure_env
kill_stale
if [ "$BACKGROUND" = "1" ]; then
start_background
else
start_foreground
fi
}
# ------------------------------------------------------------------- main ---
COMMAND="${1:-help}"
shift || true
BACKGROUND=0
KILL_STALE=1
for arg in "$@"; do
case "$arg" in
-d|--background) BACKGROUND=1 ;;
--no-kill) KILL_STALE=0 ;;
-h|--help) cmd_help; exit 0 ;;
*) die "Unknown option: $arg (see './system.sh help')" ;;
esac
done
case "$COMMAND" in
start) cmd_start ;;
stop) cmd_stop ;;
restart) cmd_stop && cmd_start ;;
status) cmd_status ;;
help|-h|--help) cmd_help ;;
*) die "Unknown command: $COMMAND (see './system.sh help')" ;;
esac