@dxos/phoenix 0.1.58-main.60c63b5 → 0.1.58-main.6b4aa57
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/dist/lib/node/index.cjs +98 -259
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/defs.d.ts +3 -1
- package/dist/types/src/defs.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/phoenix.d.ts +16 -0
- package/dist/types/src/phoenix.d.ts.map +1 -0
- package/dist/types/src/phoenix.test.d.ts +2 -0
- package/dist/types/src/phoenix.test.d.ts.map +1 -0
- package/dist/types/src/utils.d.ts +3 -3
- package/dist/types/src/utils.d.ts.map +1 -1
- package/dist/types/src/watchdog.d.ts +5 -24
- package/dist/types/src/watchdog.d.ts.map +1 -1
- package/package.json +9 -12
- package/src/defs.ts +3 -1
- package/src/index.ts +1 -1
- package/src/{daemon-manager.test.ts → phoenix.test.ts} +17 -20
- package/src/phoenix.ts +94 -0
- package/src/utils.ts +15 -18
- package/src/watchdog.test.ts +8 -10
- package/src/watchdog.ts +29 -109
- package/dist/types/src/daemon-manager.d.ts +0 -17
- package/dist/types/src/daemon-manager.d.ts.map +0 -1
- package/dist/types/src/daemon-manager.test.d.ts +0 -2
- package/dist/types/src/daemon-manager.test.d.ts.map +0 -1
- package/src/daemon-manager.ts +0 -151
package/dist/lib/node/index.cjs
CHANGED
|
@@ -30,115 +30,78 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// packages/common/phoenix/src/index.ts
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
|
-
|
|
33
|
+
Phoenix: () => Phoenix,
|
|
34
34
|
WatchDog: () => WatchDog
|
|
35
35
|
});
|
|
36
36
|
module.exports = __toCommonJS(src_exports);
|
|
37
37
|
|
|
38
|
-
// packages/common/phoenix/src/
|
|
38
|
+
// packages/common/phoenix/src/phoenix.ts
|
|
39
39
|
var import_node_child_process = require("node:child_process");
|
|
40
40
|
var import_node_fs2 = require("node:fs");
|
|
41
|
-
var import_promises = require("node:fs/promises");
|
|
42
41
|
var import_node_path = require("node:path");
|
|
43
42
|
var import_pkg_up = __toESM(require("pkg-up"));
|
|
44
|
-
var import_async2 = require("@dxos/async");
|
|
45
43
|
var import_invariant = require("@dxos/invariant");
|
|
46
|
-
var import_keys = require("@dxos/keys");
|
|
47
|
-
var import_lock_file2 = require("@dxos/lock-file");
|
|
48
44
|
var import_log = require("@dxos/log");
|
|
49
45
|
|
|
50
|
-
// packages/common/phoenix/src/defs.ts
|
|
51
|
-
var LOCK_TIMEOUT = 1e3;
|
|
52
|
-
var LOCK_CHECK_INTERVAL = 50;
|
|
53
|
-
var DAEMON_START_TIMEOUT = 1e4;
|
|
54
|
-
|
|
55
46
|
// packages/common/phoenix/src/utils.ts
|
|
56
47
|
var import_node_fs = require("node:fs");
|
|
57
48
|
var import_async = require("@dxos/async");
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
49
|
+
|
|
50
|
+
// packages/common/phoenix/src/defs.ts
|
|
51
|
+
var WATCHDOG_START_TIMEOUT = 1e4;
|
|
52
|
+
var WATCHDOG_STOP_TIMEOUT = 1e3;
|
|
53
|
+
var WATCHDOG_CHECK_INTERVAL = 50;
|
|
54
|
+
|
|
55
|
+
// packages/common/phoenix/src/utils.ts
|
|
56
|
+
var waitForPidDeletion = async (pidFile) => (0, import_async.waitForCondition)({
|
|
57
|
+
condition: () => !(0, import_node_fs.existsSync)(pidFile),
|
|
58
|
+
timeout: WATCHDOG_STOP_TIMEOUT,
|
|
59
|
+
interval: WATCHDOG_CHECK_INTERVAL
|
|
64
60
|
});
|
|
65
|
-
var
|
|
66
|
-
condition: () => (0, import_node_fs.readFileSync)(
|
|
61
|
+
var waitForPidFileBeingFilledWithInfo = async (pidFile) => (0, import_async.waitForCondition)({
|
|
62
|
+
condition: () => (0, import_node_fs.readFileSync)(pidFile, {
|
|
67
63
|
encoding: "utf-8"
|
|
68
64
|
}).includes("pid"),
|
|
69
|
-
timeout:
|
|
70
|
-
interval:
|
|
65
|
+
timeout: WATCHDOG_START_TIMEOUT,
|
|
66
|
+
interval: WATCHDOG_CHECK_INTERVAL,
|
|
71
67
|
error: new Error("Lock file is not being propagated with info.")
|
|
72
68
|
});
|
|
73
|
-
var waitForLockRelease = async (lockFile) => (0, import_async.waitForCondition)({
|
|
74
|
-
condition: async () => !await import_lock_file.LockFile.isLocked(lockFile),
|
|
75
|
-
timeout: LOCK_TIMEOUT,
|
|
76
|
-
interval: LOCK_CHECK_INTERVAL,
|
|
77
|
-
error: new Error("Lock file is not being released.")
|
|
78
|
-
});
|
|
79
69
|
|
|
80
|
-
// packages/common/phoenix/src/
|
|
81
|
-
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/phoenix/src/
|
|
82
|
-
var
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
(0, import_node_fs2.mkdirSync)((0, import_node_path.join)(_rootPath, "profile"), {
|
|
88
|
-
recursive: true
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
_getConfigFiles(uid) {
|
|
93
|
-
const defaultConfigDir = (0, import_node_path.join)(this._rootPath, "profile", uid);
|
|
94
|
-
if (!(0, import_node_fs2.existsSync)(defaultConfigDir)) {
|
|
95
|
-
(0, import_node_fs2.mkdirSync)(defaultConfigDir, {
|
|
96
|
-
recursive: true
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
return {
|
|
100
|
-
lockFile: (0, import_node_path.join)(defaultConfigDir, LOCK_FILE_NAME),
|
|
101
|
-
logFile: (0, import_node_path.join)(defaultConfigDir, "file.log"),
|
|
102
|
-
errFile: (0, import_node_path.join)(defaultConfigDir, "err.log")
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
async start(params) {
|
|
106
|
-
(0, import_invariant.invariant)(params.command, "command is required", {
|
|
107
|
-
F: __dxlog_file,
|
|
108
|
-
L: 49,
|
|
109
|
-
S: this,
|
|
110
|
-
A: [
|
|
111
|
-
"params.command",
|
|
112
|
-
"'command is required'"
|
|
113
|
-
]
|
|
114
|
-
});
|
|
115
|
-
const watchdogId = import_keys.PublicKey.random().toHex();
|
|
116
|
-
const watchDogParams = {
|
|
117
|
-
watchdogId,
|
|
118
|
-
...this._getConfigFiles(params.uid),
|
|
119
|
-
...params
|
|
120
|
-
};
|
|
70
|
+
// packages/common/phoenix/src/phoenix.ts
|
|
71
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/phoenix/src/phoenix.ts";
|
|
72
|
+
var Phoenix = class _Phoenix {
|
|
73
|
+
/**
|
|
74
|
+
* Starts detached watchdog process which starts and monitors selected command.
|
|
75
|
+
*/
|
|
76
|
+
static async start(params) {
|
|
121
77
|
{
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
recursive: true
|
|
127
|
-
});
|
|
78
|
+
if ((0, import_node_fs2.existsSync)(params.pidFile)) {
|
|
79
|
+
await _Phoenix.stop(params.pidFile);
|
|
80
|
+
}
|
|
81
|
+
await waitForPidDeletion(params.pidFile);
|
|
128
82
|
}
|
|
129
83
|
{
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
84
|
+
[
|
|
85
|
+
params.logFile,
|
|
86
|
+
params.errFile,
|
|
87
|
+
params.pidFile
|
|
88
|
+
].forEach((filename) => {
|
|
89
|
+
if (!(0, import_node_fs2.existsSync)(filename)) {
|
|
90
|
+
(0, import_node_fs2.mkdirSync)((0, import_node_path.dirname)(filename), {
|
|
91
|
+
recursive: true
|
|
92
|
+
});
|
|
93
|
+
(0, import_node_fs2.writeFileSync)(filename, "", {
|
|
94
|
+
encoding: "utf-8"
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
});
|
|
134
98
|
}
|
|
135
99
|
const watchdogPath = (0, import_node_path.join)((0, import_node_path.dirname)(import_pkg_up.default.sync({
|
|
136
100
|
cwd: __dirname
|
|
137
101
|
})), "bin", "watchdog");
|
|
138
102
|
const watchDog = (0, import_node_child_process.fork)(watchdogPath, [
|
|
139
|
-
JSON.stringify(
|
|
103
|
+
JSON.stringify(params)
|
|
140
104
|
], {
|
|
141
|
-
stdio: "pipe",
|
|
142
105
|
detached: true,
|
|
143
106
|
cwd: __dirname
|
|
144
107
|
});
|
|
@@ -149,94 +112,63 @@ var DaemonManager = class {
|
|
|
149
112
|
signal
|
|
150
113
|
}, {
|
|
151
114
|
F: __dxlog_file,
|
|
152
|
-
L:
|
|
115
|
+
L: 52,
|
|
153
116
|
S: this,
|
|
154
117
|
C: (f, a) => f(...a)
|
|
155
118
|
});
|
|
156
119
|
}
|
|
157
120
|
});
|
|
158
|
-
|
|
159
|
-
watchDog.stdout.on("data", (data) => {
|
|
160
|
-
if (String(data).includes(watchdogId)) {
|
|
161
|
-
started.wake();
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
await (0, import_async2.asyncTimeout)(started.wait(), DAEMON_START_TIMEOUT);
|
|
165
|
-
await waitForLockAcquisition(watchDogParams.lockFile);
|
|
166
|
-
await waitForLockFileBeingFilledWithInfo(watchDogParams.lockFile);
|
|
121
|
+
await waitForPidFileBeingFilledWithInfo(params.pidFile);
|
|
167
122
|
watchDog.disconnect();
|
|
168
123
|
watchDog.unref();
|
|
169
|
-
return
|
|
124
|
+
return _Phoenix.info(params.pidFile);
|
|
170
125
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
126
|
+
/**
|
|
127
|
+
* Stops detached watchdog process by PID info written down in PID file.
|
|
128
|
+
*/
|
|
129
|
+
static async stop(pidFile, force = false) {
|
|
130
|
+
if (!(0, import_node_fs2.existsSync)(pidFile)) {
|
|
131
|
+
throw new Error("PID file does not exist");
|
|
132
|
+
}
|
|
133
|
+
const fileContent = (0, import_node_fs2.readFileSync)(pidFile, {
|
|
174
134
|
encoding: "utf-8"
|
|
175
|
-
})
|
|
135
|
+
});
|
|
136
|
+
if (!fileContent.includes("pid")) {
|
|
137
|
+
throw new Error("Invalid PID file content");
|
|
138
|
+
}
|
|
139
|
+
const { pid } = JSON.parse(fileContent);
|
|
140
|
+
const signal = force ? "SIGKILL" : "SIGINT";
|
|
176
141
|
try {
|
|
177
|
-
|
|
178
|
-
process.kill(processInfo.pid, "SIGKILL");
|
|
179
|
-
} else {
|
|
180
|
-
process.kill(processInfo.pid, "SIGINT");
|
|
181
|
-
}
|
|
142
|
+
process.kill(pid, signal);
|
|
182
143
|
} catch (err) {
|
|
183
|
-
(0, import_invariant.invariant)(err instanceof Error, "Invalid error type
|
|
144
|
+
(0, import_invariant.invariant)(err instanceof Error, "Invalid error type", {
|
|
184
145
|
F: __dxlog_file,
|
|
185
|
-
L:
|
|
146
|
+
L: 81,
|
|
186
147
|
S: this,
|
|
187
148
|
A: [
|
|
188
149
|
"err instanceof Error",
|
|
189
|
-
"'Invalid error type
|
|
150
|
+
"'Invalid error type'"
|
|
190
151
|
]
|
|
191
152
|
});
|
|
192
|
-
if (
|
|
153
|
+
if (err.message.includes("ESRCH") || err.name.includes("ESRCH")) {
|
|
154
|
+
(0, import_node_fs2.unlinkSync)(pidFile);
|
|
155
|
+
} else {
|
|
193
156
|
throw err;
|
|
194
157
|
}
|
|
195
158
|
}
|
|
196
|
-
await waitForLockRelease(lockFile);
|
|
197
|
-
return this.getInfo(uid);
|
|
198
159
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
return this.getInfo(uid);
|
|
160
|
+
static info(pidFile) {
|
|
161
|
+
return JSON.parse((0, import_node_fs2.readFileSync)(pidFile, {
|
|
162
|
+
encoding: "utf-8"
|
|
203
163
|
}));
|
|
204
164
|
}
|
|
205
|
-
async getInfo(uid) {
|
|
206
|
-
const files = this._getConfigFiles(uid);
|
|
207
|
-
let info = {
|
|
208
|
-
running: await import_lock_file2.LockFile.isLocked(files.lockFile),
|
|
209
|
-
uid,
|
|
210
|
-
...files
|
|
211
|
-
};
|
|
212
|
-
if ((0, import_node_fs2.existsSync)(files.lockFile)) {
|
|
213
|
-
try {
|
|
214
|
-
info = {
|
|
215
|
-
...info,
|
|
216
|
-
...JSON.parse((0, import_node_fs2.readFileSync)(files.lockFile, {
|
|
217
|
-
encoding: "utf-8"
|
|
218
|
-
}))
|
|
219
|
-
};
|
|
220
|
-
} catch (err) {
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
return info;
|
|
224
|
-
}
|
|
225
|
-
async isRunning(uid) {
|
|
226
|
-
const lockFile = this._getConfigFiles(uid).lockFile;
|
|
227
|
-
return import_lock_file2.LockFile.isLocked(lockFile);
|
|
228
|
-
}
|
|
229
165
|
};
|
|
230
166
|
|
|
231
167
|
// packages/common/phoenix/src/watchdog.ts
|
|
232
168
|
var import_node_child_process2 = require("node:child_process");
|
|
233
169
|
var import_node_fs3 = require("node:fs");
|
|
234
|
-
var
|
|
235
|
-
var import_ps_tree = __toESM(require("ps-tree"));
|
|
236
|
-
var import_async3 = require("@dxos/async");
|
|
237
|
-
var import_context = require("@dxos/context");
|
|
170
|
+
var import_async2 = require("@dxos/async");
|
|
238
171
|
var import_invariant2 = require("@dxos/invariant");
|
|
239
|
-
var import_lock_file3 = require("@dxos/lock-file");
|
|
240
172
|
var import_log2 = require("@dxos/log");
|
|
241
173
|
function _ts_decorate(decorators, target, key, desc) {
|
|
242
174
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
@@ -255,79 +187,43 @@ var WatchDog = class {
|
|
|
255
187
|
this._restarts = 0;
|
|
256
188
|
}
|
|
257
189
|
async start() {
|
|
258
|
-
await this._acquireLock();
|
|
259
|
-
this._log("Lock acquired.");
|
|
260
190
|
const { cwd, shell, env, command, args } = {
|
|
261
191
|
cwd: process.cwd(),
|
|
262
192
|
...this._params
|
|
263
193
|
};
|
|
264
|
-
(
|
|
265
|
-
F: __dxlog_file2,
|
|
266
|
-
L: 86,
|
|
267
|
-
S: this,
|
|
268
|
-
A: [
|
|
269
|
-
"command",
|
|
270
|
-
"'Command is not defined.'"
|
|
271
|
-
]
|
|
272
|
-
});
|
|
273
|
-
this._log(`Spawning process ${command} ${args?.join(" ")}`);
|
|
194
|
+
this._log(`Spawning process \`\`\`${command} ${args?.join(" ")}\`\`\``);
|
|
274
195
|
this._child = (0, import_node_child_process2.spawn)(command, args, {
|
|
275
196
|
cwd,
|
|
276
197
|
shell,
|
|
277
198
|
env,
|
|
278
199
|
stdio: "pipe"
|
|
279
200
|
});
|
|
280
|
-
this._processCtx = new import_context.Context();
|
|
281
|
-
const childInfo = {
|
|
282
|
-
pid: process.pid,
|
|
283
|
-
timestamp: Date.now(),
|
|
284
|
-
restarts: this._restarts,
|
|
285
|
-
...this._params
|
|
286
|
-
};
|
|
287
|
-
(0, import_node_fs3.writeFileSync)(this._params.lockFile, JSON.stringify(childInfo, void 0, 2), {
|
|
288
|
-
encoding: "utf-8"
|
|
289
|
-
});
|
|
290
201
|
this._child.stdout.on("data", (data) => {
|
|
291
202
|
this._log(String(data));
|
|
292
203
|
});
|
|
293
204
|
this._child.stderr.on("data", (data) => {
|
|
294
205
|
this._err(data);
|
|
295
206
|
});
|
|
296
|
-
{
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
}
|
|
302
|
-
};
|
|
303
|
-
this._child.on("close", restartHandler);
|
|
304
|
-
this._processCtx.onDispose(() => {
|
|
305
|
-
this._child.off("close", restartHandler);
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
this._child.on("close", (code, signal) => {
|
|
207
|
+
this._child.on("close", async (code, signal) => {
|
|
208
|
+
if (code && code !== 0 && signal !== "SIGINT" && signal !== "SIGKILL") {
|
|
209
|
+
this._err(`Died unexpectedly with exit code ${code} (signal: ${signal}).`);
|
|
210
|
+
await this.restart();
|
|
211
|
+
}
|
|
309
212
|
this._log(`Stopped with exit code ${code} (signal: ${signal}).`);
|
|
213
|
+
if ((0, import_node_fs3.existsSync)(this._params.pidFile)) {
|
|
214
|
+
(0, import_node_fs3.unlinkSync)(this._params.pidFile);
|
|
215
|
+
}
|
|
310
216
|
});
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
217
|
+
const childInfo = {
|
|
218
|
+
pid: this._child.pid,
|
|
219
|
+
started: Date.now(),
|
|
220
|
+
restarts: this._restarts,
|
|
221
|
+
...this._params
|
|
222
|
+
};
|
|
223
|
+
(0, import_node_fs3.writeFileSync)(this._params.pidFile, JSON.stringify(childInfo, void 0, 2), {
|
|
224
|
+
encoding: "utf-8"
|
|
319
225
|
});
|
|
320
|
-
|
|
321
|
-
}
|
|
322
|
-
/**
|
|
323
|
-
* Sends SIGINT to the child process and the tree it spawned (if `killTree` param is `true`).
|
|
324
|
-
*/
|
|
325
|
-
async stop() {
|
|
326
|
-
if (!this._child) {
|
|
327
|
-
return;
|
|
328
|
-
}
|
|
329
|
-
await this._killWithSignal("SIGKILL");
|
|
330
|
-
await this._releaseLock();
|
|
226
|
+
await waitForPidFileBeingFilledWithInfo(this._params.pidFile);
|
|
331
227
|
}
|
|
332
228
|
/**
|
|
333
229
|
* Sends SIGKILL to the child process and the tree it spawned (if `killTree` param is `true`).
|
|
@@ -337,7 +233,10 @@ var WatchDog = class {
|
|
|
337
233
|
return;
|
|
338
234
|
}
|
|
339
235
|
await this._killWithSignal("SIGKILL");
|
|
340
|
-
|
|
236
|
+
if ((0, import_node_fs3.existsSync)(this._params.pidFile)) {
|
|
237
|
+
(0, import_node_fs3.unlinkSync)(this._params.pidFile);
|
|
238
|
+
}
|
|
239
|
+
await waitForPidDeletion(this._params.pidFile);
|
|
341
240
|
}
|
|
342
241
|
async restart() {
|
|
343
242
|
await this.kill();
|
|
@@ -346,7 +245,7 @@ var WatchDog = class {
|
|
|
346
245
|
} else {
|
|
347
246
|
(0, import_log2.log)("Restarting...", void 0, {
|
|
348
247
|
F: __dxlog_file2,
|
|
349
|
-
L:
|
|
248
|
+
L: 120,
|
|
350
249
|
S: this,
|
|
351
250
|
C: (f, a) => f(...a)
|
|
352
251
|
});
|
|
@@ -355,45 +254,9 @@ var WatchDog = class {
|
|
|
355
254
|
}
|
|
356
255
|
}
|
|
357
256
|
async _killWithSignal(signal) {
|
|
358
|
-
(0, import_invariant2.invariant)(this._processCtx, "Process context is not defined.", {
|
|
359
|
-
F: __dxlog_file2,
|
|
360
|
-
L: 173,
|
|
361
|
-
S: this,
|
|
362
|
-
A: [
|
|
363
|
-
"this._processCtx",
|
|
364
|
-
"'Process context is not defined.'"
|
|
365
|
-
]
|
|
366
|
-
});
|
|
367
|
-
await this._processCtx.dispose();
|
|
368
|
-
if (this._params.killTree) {
|
|
369
|
-
if (process.platform !== "win32") {
|
|
370
|
-
(0, import_invariant2.invariant)(this._child?.pid, "Child process has no pid.", {
|
|
371
|
-
F: __dxlog_file2,
|
|
372
|
-
L: 179,
|
|
373
|
-
S: this,
|
|
374
|
-
A: [
|
|
375
|
-
"this._child?.pid",
|
|
376
|
-
"'Child process has no pid.'"
|
|
377
|
-
]
|
|
378
|
-
});
|
|
379
|
-
const children = await (0, import_node_util.promisify)(import_ps_tree.default)(this._child.pid);
|
|
380
|
-
children.map((p) => p.PID).forEach((tpid) => {
|
|
381
|
-
(0, import_invariant2.invariant)(tpid, "Process id is not defined.", {
|
|
382
|
-
F: __dxlog_file2,
|
|
383
|
-
L: 184,
|
|
384
|
-
S: this,
|
|
385
|
-
A: [
|
|
386
|
-
"tpid",
|
|
387
|
-
"'Process id is not defined.'"
|
|
388
|
-
]
|
|
389
|
-
});
|
|
390
|
-
process.kill(Number(tpid), signal);
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
257
|
(0, import_invariant2.invariant)(this._child?.pid, "Child process has no pid.", {
|
|
395
258
|
F: __dxlog_file2,
|
|
396
|
-
L:
|
|
259
|
+
L: 127,
|
|
397
260
|
S: this,
|
|
398
261
|
A: [
|
|
399
262
|
"this._child?.pid",
|
|
@@ -403,27 +266,6 @@ var WatchDog = class {
|
|
|
403
266
|
this._child.kill(signal);
|
|
404
267
|
this._child = void 0;
|
|
405
268
|
}
|
|
406
|
-
async _acquireLock() {
|
|
407
|
-
if (await import_lock_file3.LockFile.isLocked(this._params.lockFile)) {
|
|
408
|
-
throw new Error("Lock file is already locked.");
|
|
409
|
-
}
|
|
410
|
-
this._lock = await import_lock_file3.LockFile.acquire(this._params.lockFile);
|
|
411
|
-
await waitForLockAcquisition(this._params.lockFile);
|
|
412
|
-
}
|
|
413
|
-
async _releaseLock() {
|
|
414
|
-
(0, import_invariant2.invariant)(this._lock, "Lock is not defined.", {
|
|
415
|
-
F: __dxlog_file2,
|
|
416
|
-
L: 204,
|
|
417
|
-
S: this,
|
|
418
|
-
A: [
|
|
419
|
-
"this._lock",
|
|
420
|
-
"'Lock is not defined.'"
|
|
421
|
-
]
|
|
422
|
-
});
|
|
423
|
-
await import_lock_file3.LockFile.release(this._lock);
|
|
424
|
-
await waitForLockRelease(this._params.lockFile);
|
|
425
|
-
this._lock = void 0;
|
|
426
|
-
}
|
|
427
269
|
_log(message) {
|
|
428
270
|
(0, import_node_fs3.writeFileSync)(this._params.logFile, message + "\n", {
|
|
429
271
|
flag: "a+",
|
|
@@ -439,17 +281,14 @@ var WatchDog = class {
|
|
|
439
281
|
}
|
|
440
282
|
};
|
|
441
283
|
_ts_decorate([
|
|
442
|
-
|
|
284
|
+
import_async2.synchronized
|
|
443
285
|
], WatchDog.prototype, "start", null);
|
|
444
286
|
_ts_decorate([
|
|
445
|
-
|
|
446
|
-
], WatchDog.prototype, "stop", null);
|
|
447
|
-
_ts_decorate([
|
|
448
|
-
import_async3.synchronized
|
|
287
|
+
import_async2.synchronized
|
|
449
288
|
], WatchDog.prototype, "kill", null);
|
|
450
289
|
// Annotate the CommonJS export names for ESM import in node:
|
|
451
290
|
0 && (module.exports = {
|
|
452
|
-
|
|
291
|
+
Phoenix,
|
|
453
292
|
WatchDog
|
|
454
293
|
});
|
|
455
294
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/index.ts", "../../../src/
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n// Inspired by https://github.com/foreversd/forever\n// Their copyright notice is included below.\n// Copyright (C) 2010 Charlie Robbins & the Contributors\n//\n\nexport * from './daemon-manager';\nexport * from './watchdog';\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { fork } from 'node:child_process';\nimport { existsSync, mkdirSync, readFileSync, unlinkSync } from 'node:fs';\nimport { readdir } from 'node:fs/promises';\nimport { dirname, join } from 'node:path';\nimport pkgUp from 'pkg-up';\n\nimport { Trigger, asyncTimeout } from '@dxos/async';\nimport { invariant } from '@dxos/invariant';\nimport { PublicKey } from '@dxos/keys';\nimport { LockFile } from '@dxos/lock-file';\nimport { log } from '@dxos/log';\n\nimport { DAEMON_START_TIMEOUT } from './defs';\nimport { waitForLockAcquisition, waitForLockFileBeingFilledWithInfo, waitForLockRelease } from './utils';\nimport { ChildParams, Logs, Lock, ProcessInfo, WatchDogParams } from './watchdog';\n\nconst LOCK_FILE_NAME = 'lockfile';\n\n/**\n * Params to start a daemon.\n * User have no control over the lock file.\n */\nexport type StartParams = ChildParams & Partial<Logs>;\n\nexport class DaemonManager {\n constructor(private readonly _rootPath: string) {\n if (!existsSync(join(_rootPath, 'profile'))) {\n mkdirSync(join(_rootPath, 'profile'), { recursive: true });\n }\n }\n\n private _getConfigFiles(uid: string): Logs & Lock {\n const defaultConfigDir = join(this._rootPath, 'profile', uid);\n if (!existsSync(defaultConfigDir)) {\n mkdirSync(defaultConfigDir, { recursive: true });\n }\n return {\n lockFile: join(defaultConfigDir, LOCK_FILE_NAME),\n logFile: join(defaultConfigDir, 'file.log'),\n errFile: join(defaultConfigDir, 'err.log'),\n };\n }\n\n async start(params: StartParams) {\n invariant(params.command, 'command is required');\n const watchdogId = PublicKey.random().toHex();\n const watchDogParams: WatchDogParams & { watchdogId: string } = {\n watchdogId,\n ...this._getConfigFiles(params.uid),\n ...params,\n };\n\n {\n // Create log folders.\n mkdirSync(dirname(watchDogParams.logFile), { recursive: true });\n mkdirSync(dirname(watchDogParams.errFile), { recursive: true });\n }\n\n {\n // Clear stale lock file if process is not running.\n if (await LockFile.isLocked(watchDogParams.lockFile)) {\n throw new Error('Lock file is already locked.');\n }\n unlinkSync(watchDogParams.lockFile);\n }\n\n const watchdogPath = join(dirname(pkgUp.sync({ cwd: __dirname })!), 'bin', 'watchdog');\n\n const watchDog = fork(watchdogPath, [JSON.stringify(watchDogParams)], {\n stdio: 'pipe',\n detached: true,\n cwd: __dirname,\n });\n\n watchDog.on('exit', (code, signal) => {\n if (code && code !== 0) {\n log.error('Monitor died unexpectedly', { code, signal });\n }\n });\n\n const started = new Trigger();\n watchDog.stdout!.on('data', (data) => {\n if (String(data).includes(watchdogId)) {\n started.wake();\n }\n });\n\n await asyncTimeout(started.wait(), DAEMON_START_TIMEOUT);\n await waitForLockAcquisition(watchDogParams.lockFile);\n await waitForLockFileBeingFilledWithInfo(watchDogParams.lockFile);\n\n watchDog.disconnect();\n watchDog.unref();\n\n return this.getInfo(params.uid);\n }\n\n async stop(uid: string, force?: boolean) {\n const lockFile = this._getConfigFiles(uid).lockFile;\n const processInfo = JSON.parse(readFileSync(lockFile, { encoding: 'utf-8' }));\n try {\n if (force) {\n process.kill(processInfo.pid, 'SIGKILL');\n } else {\n process.kill(processInfo.pid, 'SIGINT');\n }\n } catch (err) {\n invariant(err instanceof Error, 'Invalid error type.');\n if (!err.name.includes('ESRCH') && !err.message.includes('ESRCH')) {\n throw err;\n }\n }\n await waitForLockRelease(lockFile);\n\n return this.getInfo(uid);\n }\n\n async list(): Promise<ProcessInfo[]> {\n const uids = await readdir(join(this._rootPath, 'profile'));\n return Promise.all(\n uids.map(async (uid) => {\n return this.getInfo(uid);\n }),\n );\n }\n\n async getInfo(uid: string): Promise<ProcessInfo> {\n const files = this._getConfigFiles(uid);\n\n let info: ProcessInfo = { running: await LockFile.isLocked(files.lockFile), uid, ...files };\n if (existsSync(files.lockFile)) {\n try {\n info = {\n ...info,\n ...JSON.parse(readFileSync(files.lockFile, { encoding: 'utf-8' })),\n };\n } catch (err) {}\n }\n\n return info;\n }\n\n async isRunning(uid: string): Promise<boolean> {\n const lockFile = this._getConfigFiles(uid).lockFile;\n return LockFile.isLocked(lockFile);\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nexport const LOCK_TIMEOUT = 1_000;\nexport const LOCK_CHECK_INTERVAL = 50;\nexport const DAEMON_START_TIMEOUT = 10_000;\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { readFileSync } from 'node:fs';\n\nimport { waitForCondition } from '@dxos/async';\nimport { LockFile } from '@dxos/lock-file';\n\nimport { LOCK_CHECK_INTERVAL, LOCK_TIMEOUT } from './defs';\n\nexport const waitForLockAcquisition = async (lockFile: string) =>\n waitForCondition({\n condition: async () => await LockFile.isLocked(lockFile),\n timeout: LOCK_TIMEOUT,\n interval: LOCK_CHECK_INTERVAL,\n error: new Error('Lock file is not being acquired.'),\n });\n\nexport const waitForLockFileBeingFilledWithInfo = async (lockFile: string) =>\n waitForCondition({\n condition: () => readFileSync(lockFile, { encoding: 'utf-8' }).includes('pid'),\n timeout: LOCK_TIMEOUT,\n interval: LOCK_CHECK_INTERVAL,\n error: new Error('Lock file is not being propagated with info.'),\n });\n\nexport const waitForLockRelease = async (lockFile: string) =>\n waitForCondition({\n condition: async () => !(await LockFile.isLocked(lockFile)),\n timeout: LOCK_TIMEOUT,\n interval: LOCK_CHECK_INTERVAL,\n error: new Error('Lock file is not being released.'),\n });\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { ChildProcessWithoutNullStreams, spawn } from 'node:child_process';\nimport { writeFileSync } from 'node:fs';\nimport { FileHandle } from 'node:fs/promises';\nimport { promisify } from 'node:util';\nimport psTree from 'ps-tree';\n\nimport { synchronized } from '@dxos/async';\nimport { Context } from '@dxos/context';\nimport { invariant } from '@dxos/invariant';\nimport { LockFile } from '@dxos/lock-file';\nimport { log } from '@dxos/log';\n\nimport { waitForLockAcquisition, waitForLockRelease } from './utils';\n\nexport type DaemonInfo = {\n pid: number;\n command: string;\n args: string[];\n cwd: string;\n timestamp: number;\n};\n\nexport type ProcessInfo = WatchDogParams & {\n pid?: number;\n timestamp?: number;\n restarts?: number;\n running?: boolean;\n};\n\nexport type Lock = {\n lockFile: string; // Path to lock file\n};\n\nexport type Logs = {\n //\n // Log files and associated logging options for this instance\n //\n logFile: string; // Path to log output all logs\n errFile: string; // Path to log output from child stderr\n};\n\nexport type ChildParams = {\n uid: string; // Unique identifier for this instance\n\n //\n // Basic configuration options\n //\n maxRestarts?: number | undefined; // Sets the maximum number of times a given script should run\n killTree?: boolean | undefined; // Kills the entire child process tree on `exit`\n\n //\n // Command to spawn as well as options and other vars\n // (env, cwd, etc) to pass along\n //\n command?: string; // Binary to run (default: 'node')\n args?: string[] | undefined; // Additional arguments to pass to the script,\n\n //\n // More specific options to pass along to `child_process.spawn` which\n // will override anything passed to the `spawnWith` option\n //\n env?: NodeJS.ProcessEnv | undefined;\n cwd?: string | undefined;\n shell?: boolean | undefined;\n};\n\nexport type WatchDogParams = ChildParams & Lock & Logs;\n\nexport class WatchDog {\n private _lock?: FileHandle;\n private _child?: ChildProcessWithoutNullStreams;\n private _restarts = 0;\n private _processCtx?: Context;\n\n constructor(private readonly _params: WatchDogParams) {}\n\n @synchronized\n async start() {\n await this._acquireLock();\n this._log('Lock acquired.');\n const { cwd, shell, env, command, args } = { cwd: process.cwd(), ...this._params };\n invariant(command, 'Command is not defined.');\n\n this._log(`Spawning process ${command} ${args?.join(' ')}`);\n this._child = spawn(command, args, { cwd, shell, env, stdio: 'pipe' });\n this._processCtx = new Context();\n\n const childInfo: ProcessInfo = {\n pid: process.pid,\n timestamp: Date.now(),\n restarts: this._restarts,\n ...this._params,\n };\n\n writeFileSync(this._params.lockFile, JSON.stringify(childInfo, undefined, 2), { encoding: 'utf-8' });\n\n this._child.stdout.on('data', (data: Uint8Array) => {\n this._log(String(data));\n });\n this._child.stderr.on('data', (data: Uint8Array) => {\n this._err(data);\n });\n\n // Setup restart handler.\n {\n const restartHandler = async (code: number, signal: number | NodeJS.Signals) => {\n if (code && code !== 0) {\n this._err(`Died unexpectedly with exit code ${code} (signal: ${signal}).`);\n await this.restart();\n }\n };\n\n this._child.on('close', restartHandler);\n\n // We should unsubscribe from the event when the process is killed by us to not try to restart it.\n this._processCtx.onDispose(() => {\n this._child!.off('close', restartHandler);\n });\n }\n\n this._child.on('close', (code: number, signal: number | NodeJS.Signals) => {\n this._log(`Stopped with exit code ${code} (signal: ${signal}).`);\n });\n\n invariant(this._child.pid, 'Child process has no pid.');\n this._log(`Started with pid ${this._child.pid}.`);\n }\n\n /**\n * Sends SIGINT to the child process and the tree it spawned (if `killTree` param is `true`).\n */\n @synchronized\n async stop() {\n if (!this._child) {\n return;\n }\n\n await this._killWithSignal('SIGKILL');\n\n await this._releaseLock();\n }\n\n /**\n * Sends SIGKILL to the child process and the tree it spawned (if `killTree` param is `true`).\n */\n @synchronized\n async kill() {\n if (!this._child) {\n return;\n }\n\n await this._killWithSignal('SIGKILL');\n\n await this._releaseLock();\n }\n\n async restart() {\n await this.kill();\n if (this._params.maxRestarts !== undefined && this._restarts >= this._params.maxRestarts) {\n this._err('Max restarts number is reached');\n } else {\n log('Restarting...');\n this._restarts++;\n await this.start();\n }\n }\n\n async _killWithSignal(signal: number | NodeJS.Signals) {\n invariant(this._processCtx, 'Process context is not defined.');\n await this._processCtx.dispose();\n\n // Kill child process tree.\n if (this._params.killTree) {\n if (process.platform !== 'win32') {\n invariant(this._child?.pid, 'Child process has no pid.');\n const children = await promisify(psTree)(this._child.pid);\n children\n .map((p) => p.PID)\n .forEach((tpid) => {\n invariant(tpid, 'Process id is not defined.');\n process.kill(Number(tpid), signal);\n });\n }\n }\n\n invariant(this._child?.pid, 'Child process has no pid.');\n this._child.kill(signal);\n this._child = undefined;\n }\n\n private async _acquireLock() {\n if (await LockFile.isLocked(this._params.lockFile)) {\n throw new Error('Lock file is already locked.');\n }\n this._lock = await LockFile.acquire(this._params.lockFile);\n await waitForLockAcquisition(this._params.lockFile);\n }\n\n private async _releaseLock() {\n invariant(this._lock, 'Lock is not defined.');\n\n await LockFile.release(this._lock);\n await waitForLockRelease(this._params.lockFile);\n\n this._lock = undefined;\n }\n\n private _log(message: string | Uint8Array) {\n writeFileSync(this._params.logFile, message + '\\n', {\n flag: 'a+',\n encoding: 'utf-8',\n });\n }\n\n private _err(message: string | Uint8Array) {\n this._log(message);\n writeFileSync(this._params.errFile, message + '\\n', {\n flag: 'a+',\n encoding: 'utf-8',\n });\n }\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;ACIA,gCAAqB;AACrB,IAAAA,
|
|
6
|
-
"names": ["import_node_fs", "
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/phoenix.ts", "../../../src/utils.ts", "../../../src/defs.ts", "../../../src/watchdog.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n// Inspired by https://github.com/foreversd/forever\n// Their copyright notice is included below.\n// Copyright (C) 2010 Charlie Robbins & the Contributors\n//\n\nexport * from './phoenix';\nexport * from './watchdog';\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { fork } from 'node:child_process';\nimport { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs';\nimport { dirname, join } from 'node:path';\nimport pkgUp from 'pkg-up';\n\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\n\nimport { waitForPidDeletion, waitForPidFileBeingFilledWithInfo } from './utils';\nimport { ProcessInfo, WatchDogParams } from './watchdog';\n\n/**\n * Utils to start/stop detached process with errors and logs handling.\n */\nexport class Phoenix {\n /**\n * Starts detached watchdog process which starts and monitors selected command.\n */\n static async start(params: WatchDogParams) {\n {\n // Clear stale pid file.\n if (existsSync(params.pidFile)) {\n await Phoenix.stop(params.pidFile);\n }\n\n await waitForPidDeletion(params.pidFile);\n }\n\n {\n // Create log folders.\n [params.logFile, params.errFile, params.pidFile].forEach((filename) => {\n if (!existsSync(filename)) {\n mkdirSync(dirname(filename), { recursive: true });\n writeFileSync(filename, '', { encoding: 'utf-8' });\n }\n });\n }\n\n const watchdogPath = join(dirname(pkgUp.sync({ cwd: __dirname })!), 'bin', 'watchdog');\n\n const watchDog = fork(watchdogPath, [JSON.stringify(params)], {\n detached: true,\n cwd: __dirname,\n });\n\n watchDog.on('exit', (code, signal) => {\n if (code && code !== 0) {\n log.error('Monitor died unexpectedly', { code, signal });\n }\n });\n\n await waitForPidFileBeingFilledWithInfo(params.pidFile);\n\n watchDog.disconnect();\n watchDog.unref();\n\n return Phoenix.info(params.pidFile);\n }\n\n /**\n * Stops detached watchdog process by PID info written down in PID file.\n */\n static async stop(pidFile: string, force = false) {\n if (!existsSync(pidFile)) {\n throw new Error('PID file does not exist');\n }\n const fileContent = readFileSync(pidFile, { encoding: 'utf-8' });\n if (!fileContent.includes('pid')) {\n throw new Error('Invalid PID file content');\n }\n\n const { pid } = JSON.parse(fileContent);\n const signal: NodeJS.Signals = force ? 'SIGKILL' : 'SIGINT';\n try {\n process.kill(pid, signal);\n } catch (err) {\n invariant(err instanceof Error, 'Invalid error type');\n if (err.message.includes('ESRCH') || err.name.includes('ESRCH')) {\n // Process is already dead.\n unlinkSync(pidFile);\n } else {\n throw err;\n }\n }\n }\n\n static info(pidFile: string): ProcessInfo {\n return JSON.parse(readFileSync(pidFile, { encoding: 'utf-8' }));\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { existsSync, readFileSync } from 'node:fs';\n\nimport { waitForCondition } from '@dxos/async';\n\nimport { WATCHDOG_CHECK_INTERVAL, WATCHDOG_START_TIMEOUT, WATCHDOG_STOP_TIMEOUT } from './defs';\n\nexport const waitForPidCreation = async (pidFile: string) =>\n waitForCondition({\n condition: () => existsSync(pidFile),\n timeout: WATCHDOG_START_TIMEOUT,\n interval: WATCHDOG_CHECK_INTERVAL,\n });\n\nexport const waitForPidDeletion = async (pidFile: string) =>\n waitForCondition({\n condition: () => !existsSync(pidFile),\n timeout: WATCHDOG_STOP_TIMEOUT,\n interval: WATCHDOG_CHECK_INTERVAL,\n });\n\nexport const waitForPidFileBeingFilledWithInfo = async (pidFile: string) =>\n waitForCondition({\n condition: () => readFileSync(pidFile, { encoding: 'utf-8' }).includes('pid'),\n timeout: WATCHDOG_START_TIMEOUT,\n interval: WATCHDOG_CHECK_INTERVAL,\n error: new Error('Lock file is not being propagated with info.'),\n });\n", "//\n// Copyright 2023 DXOS.org\n//\n\nexport const LOCK_TIMEOUT = 1_000;\nexport const LOCK_CHECK_INTERVAL = 50;\nexport const WATCHDOG_START_TIMEOUT = 10_000;\nexport const WATCHDOG_STOP_TIMEOUT = 1_000;\nexport const WATCHDOG_CHECK_INTERVAL = 50;\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { ChildProcessWithoutNullStreams, spawn } from 'node:child_process';\nimport { existsSync, unlinkSync, writeFileSync } from 'node:fs';\nimport { FileHandle } from 'node:fs/promises';\n\nimport { synchronized } from '@dxos/async';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\n\nimport { waitForPidDeletion, waitForPidFileBeingFilledWithInfo } from './utils';\n\nexport type ProcessInfo = WatchDogParams & {\n pid?: number;\n started?: number;\n restarts?: number;\n running?: boolean;\n};\n\nexport type WatchDogParams = {\n profile?: string; // Human readable process identifier\n pidFile: string; // Path to PID file\n\n //\n // Log files and associated logging options for this instance\n //\n logFile: string; // Path to log output all logs\n errFile: string; // Path to log output from child stderr\n\n //\n // Basic configuration options\n //\n maxRestarts?: number | undefined; // Sets the maximum number of times a given script should run\n killTree?: boolean | undefined; // Kills the entire child process tree on `exit`\n\n //\n // Command to spawn as well as options and other vars\n // (env, cwd, etc) to pass along\n //\n command: string; // Binary to run (default: 'node')\n args?: string[] | undefined; // Additional arguments to pass to the script,\n\n //\n // More specific options to pass along to `child_process.spawn` which\n // will override anything passed to the `spawnWith` option\n //\n env?: NodeJS.ProcessEnv | undefined;\n cwd?: string | undefined;\n shell?: boolean | undefined;\n};\n\nexport class WatchDog {\n private _lock?: FileHandle;\n private _child?: ChildProcessWithoutNullStreams;\n private _restarts = 0;\n\n constructor(private readonly _params: WatchDogParams) {}\n\n @synchronized\n async start() {\n const { cwd, shell, env, command, args } = { cwd: process.cwd(), ...this._params };\n\n this._log(`Spawning process \\`\\`\\`${command} ${args?.join(' ')}\\`\\`\\``);\n this._child = spawn(command, args, { cwd, shell, env, stdio: 'pipe' });\n\n this._child.stdout.on('data', (data: Uint8Array) => {\n this._log(String(data));\n });\n this._child.stderr.on('data', (data: Uint8Array) => {\n this._err(data);\n });\n this._child.on('close', async (code: number, signal: number | NodeJS.Signals) => {\n if (code && code !== 0 && signal !== 'SIGINT' && signal !== 'SIGKILL') {\n this._err(`Died unexpectedly with exit code ${code} (signal: ${signal}).`);\n await this.restart();\n }\n this._log(`Stopped with exit code ${code} (signal: ${signal}).`);\n if (existsSync(this._params.pidFile)) {\n unlinkSync(this._params.pidFile);\n }\n });\n\n const childInfo: ProcessInfo = {\n pid: this._child.pid,\n started: Date.now(),\n restarts: this._restarts,\n ...this._params,\n };\n\n writeFileSync(this._params.pidFile, JSON.stringify(childInfo, undefined, 2), { encoding: 'utf-8' });\n\n await waitForPidFileBeingFilledWithInfo(this._params.pidFile);\n }\n\n /**\n * Sends SIGKILL to the child process and the tree it spawned (if `killTree` param is `true`).\n */\n @synchronized\n async kill() {\n if (!this._child) {\n return;\n }\n\n await this._killWithSignal('SIGKILL');\n\n if (existsSync(this._params.pidFile)) {\n unlinkSync(this._params.pidFile);\n }\n\n await waitForPidDeletion(this._params.pidFile);\n }\n\n async restart() {\n await this.kill();\n if (this._params.maxRestarts !== undefined && this._restarts >= this._params.maxRestarts) {\n this._err('Max restarts number is reached');\n } else {\n log('Restarting...');\n this._restarts++;\n await this.start();\n }\n }\n\n async _killWithSignal(signal: number | NodeJS.Signals) {\n invariant(this._child?.pid, 'Child process has no pid.');\n this._child.kill(signal);\n this._child = undefined;\n }\n\n private _log(message: string | Uint8Array) {\n writeFileSync(this._params.logFile, message + '\\n', {\n flag: 'a+',\n encoding: 'utf-8',\n });\n }\n\n private _err(message: string | Uint8Array) {\n this._log(message);\n writeFileSync(this._params.errFile, message + '\\n', {\n flag: 'a+',\n encoding: 'utf-8',\n });\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;ACIA,gCAAqB;AACrB,IAAAA,kBAA+E;AAC/E,uBAA8B;AAC9B,oBAAkB;AAElB,uBAA0B;AAC1B,iBAAoB;;;ACNpB,qBAAyC;AAEzC,mBAAiC;;;ACA1B,IAAMC,yBAAyB;AAC/B,IAAMC,wBAAwB;AAC9B,IAAMC,0BAA0B;;;ADShC,IAAMC,qBAAqB,OAAOC,gBACvCC,+BAAiB;EACfC,WAAW,MAAM,KAACC,2BAAWH,OAAAA;EAC7BI,SAASC;EACTC,UAAUC;AACZ,CAAA;AAEK,IAAMC,oCAAoC,OAAOR,gBACtDC,+BAAiB;EACfC,WAAW,UAAMO,6BAAaT,SAAS;IAAEU,UAAU;EAAQ,CAAA,EAAGC,SAAS,KAAA;EACvEP,SAASQ;EACTN,UAAUC;EACVM,OAAO,IAAIC,MAAM,8CAAA;AACnB,CAAA;;;;ADZK,IAAMC,UAAN,MAAMA,SAAAA;;;;EAIX,aAAaC,MAAMC,QAAwB;AACzC;AAEE,cAAIC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AASD,UAAU,MAAM;MAClDE,MAAM;MACNV,UAAU;IACZ,CAAA;EACF;EAEQjB,KAAKyB,SAA8B;AACzC,SAAKnC,KAAKmC,OAAAA;AACVZ,uCAAc,KAAKhC,QAAQ+C,SAASH,UAAU,MAAM;MAClDE,MAAM;MACNV,UAAU;IACZ,CAAA;EACF;AACF;;EArFGY;GAPUlD,SAAAA,WAAAA,SAAAA,IAAAA;;EA8CVkD;GA9CUlD,SAAAA,WAAAA,QAAAA,IAAAA;",
|
|
6
|
+
"names": ["import_node_fs", "WATCHDOG_START_TIMEOUT", "WATCHDOG_STOP_TIMEOUT", "WATCHDOG_CHECK_INTERVAL", "waitForPidDeletion", "pidFile", "waitForCondition", "condition", "existsSync", "timeout", "WATCHDOG_STOP_TIMEOUT", "interval", "WATCHDOG_CHECK_INTERVAL", "waitForPidFileBeingFilledWithInfo", "readFileSync", "encoding", "includes", "WATCHDOG_START_TIMEOUT", "error", "Error", "Phoenix", "start", "params", "existsSync", "pidFile", "stop", "waitForPidDeletion", "logFile", "errFile", "forEach", "filename", "mkdirSync", "dirname", "recursive", "writeFileSync", "encoding", "watchdogPath", "join", "pkgUp", "sync", "cwd", "__dirname", "watchDog", "fork", "JSON", "stringify", "detached", "on", "code", "signal", "log", "error", "waitForPidFileBeingFilledWithInfo", "disconnect", "unref", "info", "force", "Error", "fileContent", "readFileSync", "includes", "pid", "parse", "process", "kill", "err", "invariant", "message", "name", "unlinkSync", "import_node_child_process", "import_node_fs", "import_async", "import_invariant", "import_log", "WatchDog", "constructor", "_params", "_restarts", "start", "cwd", "shell", "env", "command", "args", "process", "_log", "join", "_child", "spawn", "stdio", "stdout", "on", "data", "String", "stderr", "_err", "code", "signal", "restart", "existsSync", "pidFile", "unlinkSync", "childInfo", "pid", "started", "Date", "now", "restarts", "writeFileSync", "JSON", "stringify", "undefined", "encoding", "waitForPidFileBeingFilledWithInfo", "kill", "_killWithSignal", "waitForPidDeletion", "maxRestarts", "log", "invariant", "message", "logFile", "flag", "errFile", "synchronized"]
|
|
7
7
|
}
|
package/dist/lib/node/meta.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/common/phoenix/src/defs.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/common/phoenix/src/defs.ts":{"bytes":1201,"imports":[],"format":"esm"},"packages/common/phoenix/src/utils.ts":{"bytes":3673,"imports":[{"path":"node:fs","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"packages/common/phoenix/src/defs.ts","kind":"import-statement","original":"./defs"}],"format":"esm"},"packages/common/phoenix/src/phoenix.ts":{"bytes":10474,"imports":[{"path":"node:child_process","kind":"import-statement","external":true},{"path":"node:fs","kind":"import-statement","external":true},{"path":"node:path","kind":"import-statement","external":true},{"path":"pkg-up","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"packages/common/phoenix/src/utils.ts","kind":"import-statement","original":"./utils"}],"format":"esm"},"packages/common/phoenix/src/watchdog.ts":{"bytes":14340,"imports":[{"path":"node:child_process","kind":"import-statement","external":true},{"path":"node:fs","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"packages/common/phoenix/src/utils.ts","kind":"import-statement","original":"./utils"}],"format":"esm"},"packages/common/phoenix/src/index.ts":{"bytes":987,"imports":[{"path":"packages/common/phoenix/src/phoenix.ts","kind":"import-statement","original":"./phoenix"},{"path":"packages/common/phoenix/src/watchdog.ts","kind":"import-statement","original":"./watchdog"}],"format":"esm"}},"outputs":{"packages/common/phoenix/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":14468},"packages/common/phoenix/dist/lib/node/index.cjs":{"imports":[{"path":"node:child_process","kind":"require-call","external":true},{"path":"node:fs","kind":"require-call","external":true},{"path":"node:path","kind":"require-call","external":true},{"path":"pkg-up","kind":"require-call","external":true},{"path":"@dxos/invariant","kind":"require-call","external":true},{"path":"@dxos/log","kind":"require-call","external":true},{"path":"node:fs","kind":"require-call","external":true},{"path":"@dxos/async","kind":"require-call","external":true},{"path":"node:child_process","kind":"require-call","external":true},{"path":"node:fs","kind":"require-call","external":true},{"path":"@dxos/async","kind":"require-call","external":true},{"path":"@dxos/invariant","kind":"require-call","external":true},{"path":"@dxos/log","kind":"require-call","external":true}],"exports":[],"entryPoint":"packages/common/phoenix/src/index.ts","inputs":{"packages/common/phoenix/src/index.ts":{"bytesInOutput":147},"packages/common/phoenix/src/phoenix.ts":{"bytesInOutput":3063},"packages/common/phoenix/src/utils.ts":{"bytesInOutput":646},"packages/common/phoenix/src/defs.ts":{"bytesInOutput":101},"packages/common/phoenix/src/watchdog.ts":{"bytesInOutput":3933}},"bytes":9727}}}
|