-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupervisor.js
More file actions
executable file
·62 lines (51 loc) · 1.44 KB
/
Copy pathsupervisor.js
File metadata and controls
executable file
·62 lines (51 loc) · 1.44 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
const { spawn, execSync } = require('child_process');
const path = require('path');
let child = null;
let restartCount = 0;
const MAX_RESTARTS = 1000;
function killExisting() {
try {
execSync('pkill -f "server.js" 2>/dev/null || true', { timeout: 2000 });
} catch {}
try {
execSync('fuser -k 3000/tcp 2>/dev/null || true', { timeout: 2000 });
} catch {}
}
function startServer() {
if (restartCount >= MAX_RESTARTS) {
console.error('Max restarts reached');
process.exit(1);
}
restartCount++;
killExisting();
console.log(`[${new Date().toISOString()}] Starting server (attempt ${restartCount})...`);
child = spawn('bun', ['.next/standalone/server.js'], {
cwd: '/home/z/my-project',
env: {
...process.env,
HOSTNAME: '0.0.0.0',
PORT: '3000'
},
stdio: 'inherit'
});
child.on('exit', (code, signal) => {
console.log(`[${new Date().toISOString()}] Server exited (code=${code}, signal=${signal}). Restarting in 1s...`);
child = null;
setTimeout(startServer, 1000);
});
child.on('error', (err) => {
console.error(`[${new Date().toISOString()}] Server error: ${err.message}. Restarting in 1s...`);
child = null;
setTimeout(startServer, 1000);
});
}
// Handle supervisor shutdown
process.on('SIGTERM', () => {
if (child) child.kill();
process.exit(0);
});
process.on('SIGINT', () => {
if (child) child.kill();
process.exit(0);
});
startServer();