@dxos/phoenix 0.1.58-main.4cbb52d → 0.1.58-main.4e2a73a
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 +91 -241
- 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 -21
- 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 -138
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { ChildParams, Logs, ProcessInfo } from './watchdog';
|
|
2
|
-
/**
|
|
3
|
-
* Params to start a daemon.
|
|
4
|
-
* User have no control over the lock file.
|
|
5
|
-
*/
|
|
6
|
-
export type StartParams = ChildParams & Partial<Logs>;
|
|
7
|
-
export declare class DaemonManager {
|
|
8
|
-
private readonly _rootPath;
|
|
9
|
-
constructor(_rootPath: string);
|
|
10
|
-
private _getConfigFiles;
|
|
11
|
-
start(params: StartParams): Promise<ProcessInfo>;
|
|
12
|
-
stop(uid: string, force?: boolean): Promise<ProcessInfo>;
|
|
13
|
-
list(): Promise<ProcessInfo[]>;
|
|
14
|
-
getInfo(uid: string): Promise<ProcessInfo>;
|
|
15
|
-
isRunning(uid: string): Promise<boolean>;
|
|
16
|
-
}
|
|
17
|
-
//# sourceMappingURL=daemon-manager.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"daemon-manager.d.ts","sourceRoot":"","sources":["../../../src/daemon-manager.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,WAAW,EAAE,IAAI,EAAQ,WAAW,EAAkB,MAAM,YAAY,CAAC;AAIlF;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAEtD,qBAAa,aAAa;IACZ,OAAO,CAAC,QAAQ,CAAC,SAAS;gBAAT,SAAS,EAAE,MAAM;IAM9C,OAAO,CAAC,eAAe;IAYjB,KAAK,CAAC,MAAM,EAAE,WAAW;IA2CzB,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;IAoBjC,IAAI,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;IAU9B,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAgB1C,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CAI/C"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"daemon-manager.test.d.ts","sourceRoot":"","sources":["../../../src/daemon-manager.test.ts"],"names":[],"mappings":""}
|
package/src/daemon-manager.ts
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { fork } from 'node:child_process';
|
|
6
|
-
import { existsSync, mkdirSync, readFileSync, unlinkSync } from 'node:fs';
|
|
7
|
-
import { readdir } from 'node:fs/promises';
|
|
8
|
-
import { dirname, join } from 'node:path';
|
|
9
|
-
import pkgUp from 'pkg-up';
|
|
10
|
-
|
|
11
|
-
import { invariant } from '@dxos/invariant';
|
|
12
|
-
import { LockFile } from '@dxos/lock-file';
|
|
13
|
-
import { log } from '@dxos/log';
|
|
14
|
-
|
|
15
|
-
import { waitForLockAcquisition, waitForLockFileBeingFilledWithInfo, waitForLockRelease } from './utils';
|
|
16
|
-
import { ChildParams, Logs, Lock, ProcessInfo, WatchDogParams } from './watchdog';
|
|
17
|
-
|
|
18
|
-
const LOCK_FILE_NAME = 'lockfile';
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Params to start a daemon.
|
|
22
|
-
* User have no control over the lock file.
|
|
23
|
-
*/
|
|
24
|
-
export type StartParams = ChildParams & Partial<Logs>;
|
|
25
|
-
|
|
26
|
-
export class DaemonManager {
|
|
27
|
-
constructor(private readonly _rootPath: string) {
|
|
28
|
-
if (!existsSync(join(_rootPath, 'profile'))) {
|
|
29
|
-
mkdirSync(join(_rootPath, 'profile'), { recursive: true });
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
private _getConfigFiles(uid: string): Logs & Lock {
|
|
34
|
-
const defaultConfigDir = join(this._rootPath, 'profile', uid);
|
|
35
|
-
if (!existsSync(defaultConfigDir)) {
|
|
36
|
-
mkdirSync(defaultConfigDir, { recursive: true });
|
|
37
|
-
}
|
|
38
|
-
return {
|
|
39
|
-
lockFile: join(defaultConfigDir, LOCK_FILE_NAME),
|
|
40
|
-
logFile: join(defaultConfigDir, 'file.log'),
|
|
41
|
-
errFile: join(defaultConfigDir, 'err.log'),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async start(params: StartParams) {
|
|
46
|
-
invariant(params.command, 'command is required');
|
|
47
|
-
const watchDogParams: WatchDogParams = {
|
|
48
|
-
...this._getConfigFiles(params.uid),
|
|
49
|
-
...params,
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
{
|
|
53
|
-
// Create log folders.
|
|
54
|
-
mkdirSync(dirname(watchDogParams.logFile), { recursive: true });
|
|
55
|
-
mkdirSync(dirname(watchDogParams.errFile), { recursive: true });
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
{
|
|
59
|
-
// Clear stale lock file if process is not running.
|
|
60
|
-
if (await LockFile.isLocked(watchDogParams.lockFile)) {
|
|
61
|
-
throw new Error('Lock file is already locked.');
|
|
62
|
-
}
|
|
63
|
-
unlinkSync(watchDogParams.lockFile);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const watchdogPath = join(dirname(pkgUp.sync({ cwd: __dirname })!), 'bin', 'watchdog');
|
|
67
|
-
|
|
68
|
-
const watchDog = fork(watchdogPath, [JSON.stringify(watchDogParams)], {
|
|
69
|
-
detached: true,
|
|
70
|
-
cwd: __dirname,
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
watchDog.on('exit', (code, signal) => {
|
|
74
|
-
if (code && code !== 0) {
|
|
75
|
-
log.error('Monitor died unexpectedly', { code, signal });
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
await waitForLockAcquisition(watchDogParams.lockFile);
|
|
80
|
-
await waitForLockFileBeingFilledWithInfo(watchDogParams.lockFile);
|
|
81
|
-
|
|
82
|
-
watchDog.disconnect();
|
|
83
|
-
watchDog.unref();
|
|
84
|
-
|
|
85
|
-
return this.getInfo(params.uid);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async stop(uid: string, force?: boolean) {
|
|
89
|
-
const lockFile = this._getConfigFiles(uid).lockFile;
|
|
90
|
-
const processInfo = JSON.parse(readFileSync(lockFile, { encoding: 'utf-8' }));
|
|
91
|
-
try {
|
|
92
|
-
if (force) {
|
|
93
|
-
process.kill(processInfo.pid, 'SIGKILL');
|
|
94
|
-
} else {
|
|
95
|
-
process.kill(processInfo.pid, 'SIGINT');
|
|
96
|
-
}
|
|
97
|
-
} catch (err) {
|
|
98
|
-
invariant(err instanceof Error, 'Invalid error type.');
|
|
99
|
-
if (!err.name.includes('ESRCH') && !err.message.includes('ESRCH')) {
|
|
100
|
-
throw err;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
await waitForLockRelease(lockFile);
|
|
104
|
-
|
|
105
|
-
return this.getInfo(uid);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async list(): Promise<ProcessInfo[]> {
|
|
109
|
-
const uids = (await readdir(join(this._rootPath, 'profile'))).filter((uid) => !uid.startsWith('.'));
|
|
110
|
-
|
|
111
|
-
return Promise.all(
|
|
112
|
-
uids.map(async (uid) => {
|
|
113
|
-
return this.getInfo(uid);
|
|
114
|
-
}),
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async getInfo(uid: string): Promise<ProcessInfo> {
|
|
119
|
-
const files = this._getConfigFiles(uid);
|
|
120
|
-
|
|
121
|
-
let info: ProcessInfo = { running: await LockFile.isLocked(files.lockFile), uid, ...files };
|
|
122
|
-
if (existsSync(files.lockFile)) {
|
|
123
|
-
try {
|
|
124
|
-
info = {
|
|
125
|
-
...info,
|
|
126
|
-
...JSON.parse(readFileSync(files.lockFile, { encoding: 'utf-8' })),
|
|
127
|
-
};
|
|
128
|
-
} catch (err) {}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return info;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
async isRunning(uid: string): Promise<boolean> {
|
|
135
|
-
const lockFile = this._getConfigFiles(uid).lockFile;
|
|
136
|
-
return LockFile.isLocked(lockFile);
|
|
137
|
-
}
|
|
138
|
-
}
|