Skip to content

Commit 6dad794

Browse files
Add scripts/ocl.sh: one command to start/stop/restart the whole stack
Wraps PM2 so the full stack (project-local PostgreSQL, API and web) can be managed from a single entry point, without remembering individual process names. Stop order drains web/API before the database; start is idempotent. Documents the commands in DEPLOYMENT.md.
1 parent 45a8fcb commit 6dad794

2 files changed

Lines changed: 149 additions & 0 deletions

File tree

‎docs/DEPLOYMENT.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,22 @@ pm2 logs opencoperlock-api
219219
pm2 reload ecosystem.config.cjs # zero-downtime reload after a redeploy
220220
```
221221

222+
#### One command for the whole stack
223+
224+
`scripts/ocl.sh` wraps the above so you never have to remember individual process names —
225+
it drives the entire stack (project-local PostgreSQL, API and web) at once:
226+
227+
```bash
228+
./scripts/ocl.sh start # start everything
229+
./scripts/ocl.sh stop # stop everything (web/API first, then the database)
230+
./scripts/ocl.sh restart # restart everything
231+
./scripts/ocl.sh reload # zero-downtime reload (after a redeploy)
232+
./scripts/ocl.sh status # process table
233+
./scripts/ocl.sh logs # tail all logs (or: logs api | logs web | logs postgres)
234+
./scripts/ocl.sh update # git pull + deploy.sh + reload
235+
./scripts/ocl.sh persist # pm2 save (then run the printed `pm2 startup` once)
236+
```
237+
222238
> The API loads the repo-root `.env` itself, so no secrets are stored in the PM2 config.
223239
> Keep `opencoperlock-api` at a single instance — the Remote-Upload worker must not run in
224240
> parallel copies.

‎scripts/ocl.sh‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/env bash
2+
#
3+
# OpenCoperLock lifecycle control — one entry point to start / stop / restart the whole
4+
# stack (project-local PostgreSQL, API and web) through PM2.
5+
#
6+
# ./scripts/ocl.sh start # start every process (or bring them back if stopped)
7+
# ./scripts/ocl.sh stop # stop every process (cluster, API, web)
8+
# ./scripts/ocl.sh restart # restart every process
9+
# ./scripts/ocl.sh reload # zero-downtime reload (use after a redeploy)
10+
# ./scripts/ocl.sh status # show the process table
11+
# ./scripts/ocl.sh logs [app] # tail logs (all, or one of: postgres|api|web)
12+
# ./scripts/ocl.sh update # git pull + ./scripts/deploy.sh + reload
13+
# ./scripts/ocl.sh persist # save the PM2 process list + enable boot startup
14+
#
15+
# Everything is driven by ecosystem.config.cjs, so the process set (whether a project-local
16+
# PostgreSQL is supervised or an external database is used) is detected automatically.
17+
set -euo pipefail
18+
19+
cd "$(dirname "$0")/.."
20+
ROOT="$(pwd)"
21+
ECOSYSTEM="$ROOT/ecosystem.config.cjs"
22+
23+
# Process names as declared in ecosystem.config.cjs.
24+
APPS=(opencoperlock-postgres opencoperlock-api opencoperlock-web)
25+
26+
need_pm2() {
27+
command -v pm2 >/dev/null 2>&1 || {
28+
echo "ERROR: pm2 is not installed or not on PATH. Install it with: npm i -g pm2" >&2
29+
exit 1
30+
}
31+
}
32+
33+
# Resolve a short app name (postgres|api|web) to its full PM2 process name.
34+
full_name() {
35+
case "$1" in
36+
postgres|pg|db) echo "opencoperlock-postgres" ;;
37+
api) echo "opencoperlock-api" ;;
38+
web|ui) echo "opencoperlock-web" ;;
39+
*) echo "$1" ;;
40+
esac
41+
}
42+
43+
cmd="${1:-}"
44+
shift || true
45+
46+
case "$cmd" in
47+
start)
48+
need_pm2
49+
# `pm2 start <ecosystem>` is idempotent: it launches missing apps and is a no-op for
50+
# ones already online. If they exist but are stopped, bring them back up too.
51+
echo "==> Starting OpenCoperLock"
52+
pm2 start "$ECOSYSTEM"
53+
pm2 start "${APPS[@]}" 2>/dev/null || true
54+
pm2 status
55+
;;
56+
57+
stop)
58+
need_pm2
59+
echo "==> Stopping OpenCoperLock"
60+
# Stop the API/web first, the database last, so in-flight requests aren't cut off from
61+
# their storage backend before they drain.
62+
pm2 stop opencoperlock-web opencoperlock-api 2>/dev/null || true
63+
pm2 stop opencoperlock-postgres 2>/dev/null || true
64+
pm2 status
65+
;;
66+
67+
restart)
68+
need_pm2
69+
echo "==> Restarting OpenCoperLock"
70+
# Ensure everything is defined first (handles a fresh boot where nothing is loaded yet),
71+
# then hard-restart the lot.
72+
pm2 start "$ECOSYSTEM" >/dev/null 2>&1 || true
73+
pm2 restart "${APPS[@]}"
74+
pm2 status
75+
;;
76+
77+
reload)
78+
need_pm2
79+
echo "==> Reloading OpenCoperLock (zero-downtime where possible)"
80+
pm2 reload "$ECOSYSTEM"
81+
pm2 status
82+
;;
83+
84+
status|st)
85+
need_pm2
86+
pm2 status
87+
;;
88+
89+
logs|log)
90+
need_pm2
91+
if [[ -n "${1:-}" ]]; then
92+
pm2 logs "$(full_name "$1")"
93+
else
94+
pm2 logs opencoperlock-postgres opencoperlock-api opencoperlock-web
95+
fi
96+
;;
97+
98+
update|upgrade)
99+
need_pm2
100+
echo "==> Pulling latest changes"
101+
git pull --ff-only
102+
echo "==> Rebuilding and migrating"
103+
./scripts/deploy.sh
104+
echo "==> Reloading processes"
105+
pm2 reload "$ECOSYSTEM"
106+
pm2 status
107+
;;
108+
109+
persist|save)
110+
need_pm2
111+
pm2 save
112+
echo "==> To start OpenCoperLock automatically on boot, run the command printed by:"
113+
echo " pm2 startup"
114+
;;
115+
116+
*)
117+
cat >&2 <<USAGE
118+
OpenCoperLock control
119+
120+
Usage: ./scripts/ocl.sh <command> [arg]
121+
122+
start Start the whole stack (PostgreSQL, API, web)
123+
stop Stop the whole stack
124+
restart Restart the whole stack
125+
reload Zero-downtime reload (after a redeploy)
126+
status Show the PM2 process table
127+
logs [postgres|api|web] Tail logs (all by default)
128+
update git pull + rebuild/migrate + reload
129+
persist Save the process list for reboot survival
130+
USAGE
131+
exit 1
132+
;;
133+
esac

0 commit comments

Comments
 (0)