-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestart.ts
More file actions
53 lines (44 loc) · 1.37 KB
/
restart.ts
File metadata and controls
53 lines (44 loc) · 1.37 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
import { $ } from "bun";
const main = async () => {
try {
// Step 1: Stop PM2 process 0
console.log("Stopping PM2 process 0 (MCP-PTY HTTP)...");
await $`pm2 stop 0`.quiet();
console.log("✓ PM2 process stopped");
// Step 2: Wait for port 1234 to be released
console.log("Waiting for port 1234 to be released...");
let portFree = false;
let attempts = 0;
const maxAttempts = 30; // 30 seconds max wait
while (!portFree && attempts < maxAttempts) {
const { exitCode } = await $`lsof -i :1234`.nothrow().quiet();
portFree = exitCode !== 0;
if (!portFree) {
await new Promise((resolve) => setTimeout(resolve, 1000));
attempts++;
process.stdout.write(".");
}
}
if (!portFree) {
throw new Error("Port 1234 did not become free within 30 seconds");
}
console.log("\n✓ Port 1234 is now free");
// Step 3: Start PM2 process again
console.log("Starting PM2 process 0...");
await $`pm2 start 0`.quiet();
console.log("✓ PM2 process started");
console.log("\n✅ Restart completed successfully");
} catch (err) {
console.error(
"❌ Error during restart:",
err instanceof Error ? err.message : err,
);
process.exit(1);
}
};
if (import.meta.main) {
main().catch((err) => {
console.error("Fatal error:", err);
process.exit(1);
});
}