fluxy-bot 0.5.41 → 0.5.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/supervisor/backend.ts +12 -1
- package/supervisor/worker.ts +31 -10
package/package.json
CHANGED
package/supervisor/backend.ts
CHANGED
|
@@ -25,7 +25,18 @@ export function spawnBackend(port: number): ChildProcess {
|
|
|
25
25
|
// Clear log file on each restart — only keeps current run
|
|
26
26
|
try { fs.writeFileSync(LOG_FILE, ''); } catch {}
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
// Wrap the backend in an inline loader that:
|
|
29
|
+
// 1. Dynamically imports the user's backend (tsx handles .ts compilation)
|
|
30
|
+
// 2. Adds a keepalive timer so the event loop never drains (fixes exit code 0 under systemd)
|
|
31
|
+
// 3. Catches and logs import errors instead of silently exiting
|
|
32
|
+
const backendUrl = 'file://' + backendPath.replace(/\\/g, '/');
|
|
33
|
+
const wrapper = [
|
|
34
|
+
`import('${backendUrl}')`,
|
|
35
|
+
` .catch(e => { console.error('[backend] Fatal:', e); process.exit(1); });`,
|
|
36
|
+
`setInterval(() => {}, 60000);`,
|
|
37
|
+
].join('\n');
|
|
38
|
+
|
|
39
|
+
child = spawn(process.execPath, ['--import', 'tsx/esm', '--input-type=module', '-e', wrapper], {
|
|
29
40
|
cwd: path.join(PKG_DIR, 'workspace'),
|
|
30
41
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
31
42
|
env: { ...process.env, BACKEND_PORT: String(port) },
|
package/supervisor/worker.ts
CHANGED
|
@@ -11,10 +11,27 @@ export function getWorkerPort(basePort: number): number {
|
|
|
11
11
|
return basePort + 1;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
let lastSpawnTime = 0;
|
|
15
|
+
let intentionallyStopped = false;
|
|
16
|
+
const STABLE_THRESHOLD = 30_000;
|
|
17
|
+
|
|
14
18
|
export function spawnWorker(port: number): ChildProcess {
|
|
15
19
|
const workerPath = path.join(PKG_DIR, 'worker', 'index.ts');
|
|
20
|
+
lastSpawnTime = Date.now();
|
|
21
|
+
intentionallyStopped = false;
|
|
22
|
+
|
|
23
|
+
// Wrap the worker in an inline loader that:
|
|
24
|
+
// 1. Dynamically imports the worker (tsx handles .ts compilation)
|
|
25
|
+
// 2. Adds a keepalive timer so the event loop never drains (fixes exit code 0 under systemd)
|
|
26
|
+
// 3. Catches and logs import errors instead of silently exiting
|
|
27
|
+
const workerUrl = 'file://' + workerPath.replace(/\\/g, '/');
|
|
28
|
+
const wrapper = [
|
|
29
|
+
`import('${workerUrl}')`,
|
|
30
|
+
` .catch(e => { console.error('[worker] Fatal:', e); process.exit(1); });`,
|
|
31
|
+
`setInterval(() => {}, 60000);`,
|
|
32
|
+
].join('\n');
|
|
16
33
|
|
|
17
|
-
child = spawn(process.execPath, ['--import', 'tsx/esm',
|
|
34
|
+
child = spawn(process.execPath, ['--import', 'tsx/esm', '--input-type=module', '-e', wrapper], {
|
|
18
35
|
cwd: PKG_DIR,
|
|
19
36
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
20
37
|
env: { ...process.env, WORKER_PORT: String(port) },
|
|
@@ -29,15 +46,18 @@ export function spawnWorker(port: number): ChildProcess {
|
|
|
29
46
|
});
|
|
30
47
|
|
|
31
48
|
child.on('exit', (code) => {
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
49
|
+
if (intentionallyStopped) return;
|
|
50
|
+
|
|
51
|
+
log.warn(`Worker exited unexpectedly (code ${code})`);
|
|
52
|
+
if (Date.now() - lastSpawnTime > STABLE_THRESHOLD) {
|
|
53
|
+
restarts = 0;
|
|
54
|
+
}
|
|
55
|
+
if (restarts < MAX_RESTARTS) {
|
|
56
|
+
restarts++;
|
|
57
|
+
log.info(`Restarting worker (${restarts}/${MAX_RESTARTS})...`);
|
|
58
|
+
setTimeout(() => spawnWorker(port), 1000 * restarts);
|
|
59
|
+
} else {
|
|
60
|
+
log.error('Worker failed too many times. Use Fluxy chat to debug.');
|
|
41
61
|
}
|
|
42
62
|
});
|
|
43
63
|
|
|
@@ -46,6 +66,7 @@ export function spawnWorker(port: number): ChildProcess {
|
|
|
46
66
|
}
|
|
47
67
|
|
|
48
68
|
export function stopWorker(): void {
|
|
69
|
+
intentionallyStopped = true;
|
|
49
70
|
child?.kill();
|
|
50
71
|
child = null;
|
|
51
72
|
}
|