lobstakit-cloud 1.3.8 → 1.3.9
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/lib/gateway.js +40 -24
- package/package.json +1 -1
package/lib/gateway.js
CHANGED
|
@@ -40,40 +40,56 @@ function isSystemdAvailable() {
|
|
|
40
40
|
return _systemdAvailable;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
//
|
|
44
|
-
const
|
|
45
|
-
|
|
43
|
+
// Detached-spawn fallback for non-systemd/container environments (no PM2 needed)
|
|
44
|
+
const { spawn } = require('child_process');
|
|
45
|
+
const GATEWAY_PID_FILE = path.join(os.homedir(), '.openclaw', 'gateway.pid');
|
|
46
|
+
const GATEWAY_LOG_FILE = path.join(os.homedir(), '.openclaw', 'gateway-spawn.log');
|
|
47
|
+
|
|
48
|
+
function spawnedStart() {
|
|
46
49
|
try {
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
// Kill any previously spawned gateway
|
|
51
|
+
spawnedStop();
|
|
52
|
+
// Resolve openclaw binary
|
|
53
|
+
let bin = 'openclaw';
|
|
54
|
+
try { bin = execSync('which openclaw', { encoding: 'utf8', timeout: 3000 }).trim(); } catch {}
|
|
55
|
+
const logFd = require('fs').openSync(GATEWAY_LOG_FILE, 'a');
|
|
56
|
+
const child = spawn(bin, ['gateway'], {
|
|
57
|
+
detached: true,
|
|
58
|
+
stdio: ['ignore', logFd, logFd],
|
|
59
|
+
env: { ...process.env },
|
|
60
|
+
});
|
|
61
|
+
child.unref();
|
|
62
|
+
fs.writeFileSync(GATEWAY_PID_FILE, String(child.pid));
|
|
63
|
+
console.log(`[gateway] Spawned gateway PID ${child.pid} (no-systemd mode)`);
|
|
51
64
|
return { success: true };
|
|
52
65
|
} catch (err) {
|
|
53
66
|
return { success: false, error: err.message };
|
|
54
67
|
}
|
|
55
68
|
}
|
|
56
|
-
function
|
|
69
|
+
function spawnedStop() {
|
|
57
70
|
try {
|
|
58
|
-
|
|
71
|
+
if (!fs.existsSync(GATEWAY_PID_FILE)) return { success: true };
|
|
72
|
+
const pid = parseInt(fs.readFileSync(GATEWAY_PID_FILE, 'utf8').trim(), 10);
|
|
73
|
+
if (pid) {
|
|
74
|
+
try { process.kill(pid, 'SIGTERM'); } catch {}
|
|
75
|
+
}
|
|
76
|
+
fs.unlinkSync(GATEWAY_PID_FILE);
|
|
59
77
|
return { success: true };
|
|
60
78
|
} catch (err) {
|
|
61
79
|
return { success: false, error: err.message };
|
|
62
80
|
}
|
|
63
81
|
}
|
|
64
|
-
function
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
catch { pm2Start(); } // restart fails if not running — start fresh
|
|
68
|
-
return { success: true };
|
|
69
|
-
} catch (err) {
|
|
70
|
-
return { success: false, error: err.message };
|
|
71
|
-
}
|
|
82
|
+
function spawnedRestart() {
|
|
83
|
+
spawnedStop();
|
|
84
|
+
return spawnedStart();
|
|
72
85
|
}
|
|
73
|
-
function
|
|
86
|
+
function spawnedIsRunning() {
|
|
74
87
|
try {
|
|
75
|
-
|
|
76
|
-
|
|
88
|
+
if (!fs.existsSync(GATEWAY_PID_FILE)) return false;
|
|
89
|
+
const pid = parseInt(fs.readFileSync(GATEWAY_PID_FILE, 'utf8').trim(), 10);
|
|
90
|
+
if (!pid) return false;
|
|
91
|
+
process.kill(pid, 0); // throws if process doesn't exist
|
|
92
|
+
return true;
|
|
77
93
|
} catch { return false; }
|
|
78
94
|
}
|
|
79
95
|
|
|
@@ -168,7 +184,7 @@ function pingGateway(token) {
|
|
|
168
184
|
function isGatewayRunning() {
|
|
169
185
|
// Primary: systemctl --user (accurate for user services) OR PM2 in container envs
|
|
170
186
|
if (!isSystemdAvailable()) {
|
|
171
|
-
if (
|
|
187
|
+
if (spawnedIsRunning()) return true;
|
|
172
188
|
// Fall through to HTTP check
|
|
173
189
|
} else {
|
|
174
190
|
try {
|
|
@@ -247,7 +263,7 @@ function startGateway() {
|
|
|
247
263
|
ensureUserSession();
|
|
248
264
|
if (!isSystemdAvailable()) {
|
|
249
265
|
console.log('[gateway] systemd unavailable — using PM2 fallback');
|
|
250
|
-
const result =
|
|
266
|
+
const result = spawnedStart();
|
|
251
267
|
if (result.success) setTimeout(() => syncGatewayToken(), 4000);
|
|
252
268
|
return result;
|
|
253
269
|
}
|
|
@@ -272,7 +288,7 @@ function startGateway() {
|
|
|
272
288
|
function stopGateway() {
|
|
273
289
|
if (!isSystemdAvailable()) {
|
|
274
290
|
console.log('[gateway] systemd unavailable — using PM2 fallback');
|
|
275
|
-
return
|
|
291
|
+
return spawnedStop();
|
|
276
292
|
}
|
|
277
293
|
try {
|
|
278
294
|
execSync(`systemctl --user stop ${SERVICE_NAME}`, { encoding: 'utf8', timeout: 15000, env: userEnv() });
|
|
@@ -288,7 +304,7 @@ function restartGateway() {
|
|
|
288
304
|
ensureUserSession();
|
|
289
305
|
if (!isSystemdAvailable()) {
|
|
290
306
|
console.log('[gateway] systemd unavailable — using PM2 fallback');
|
|
291
|
-
const result =
|
|
307
|
+
const result = spawnedRestart();
|
|
292
308
|
if (result.success) setTimeout(() => syncGatewayToken(), 4000);
|
|
293
309
|
return result;
|
|
294
310
|
}
|