@memoryblock/daemon 0.1.0-beta
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/LICENSE +21 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +54 -0
- package/dist/index.js.map +1 -0
- package/package.json +19 -0
- package/src/index.ts +57 -0
- package/tsconfig.json +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 memoryblock
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Spawns a background daemon for a memoryblock.
|
|
3
|
+
* Detaches the process and writes its PID to blockPath/daemon.pid
|
|
4
|
+
*/
|
|
5
|
+
export declare function spawnDaemon(blockName: string, _channel: string, blockPath: string): Promise<number>;
|
|
6
|
+
/**
|
|
7
|
+
* Reads daemon.pid for a block and sends SIGTERM.
|
|
8
|
+
*/
|
|
9
|
+
export declare function killDaemon(blockPath: string): Promise<boolean>;
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,wBAAsB,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAuBzG;AAED;;GAEG;AACH,wBAAsB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAoBpE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { promises as fsp } from 'node:fs';
|
|
4
|
+
/**
|
|
5
|
+
* Spawns a background daemon for a memoryblock.
|
|
6
|
+
* Detaches the process and writes its PID to blockPath/daemon.pid
|
|
7
|
+
*/
|
|
8
|
+
export async function spawnDaemon(blockName, _channel, blockPath) {
|
|
9
|
+
const scriptPath = process.argv[1];
|
|
10
|
+
const out = await fsp.open(join(blockPath, 'daemon-debug.log'), 'a');
|
|
11
|
+
// Always use 'multi' so the daemon initializes ALL available channels
|
|
12
|
+
// (web, telegram, etc.) and auto-routes messages via MultiChannelManager
|
|
13
|
+
const child = spawn(process.execPath, [
|
|
14
|
+
scriptPath, 'start', blockName, '--channel', 'multi'
|
|
15
|
+
], {
|
|
16
|
+
detached: true,
|
|
17
|
+
stdio: ['ignore', out.fd, out.fd], // Pipe to debug log
|
|
18
|
+
env: { ...process.env, MBLK_IS_DAEMON: '1' } // Preserve authentication keys and mark as daemon
|
|
19
|
+
});
|
|
20
|
+
child.unref(); // Prevent parent from waiting for child
|
|
21
|
+
await out.close();
|
|
22
|
+
if (child.pid) {
|
|
23
|
+
await fsp.writeFile(join(blockPath, 'daemon.pid'), child.pid.toString());
|
|
24
|
+
return child.pid;
|
|
25
|
+
}
|
|
26
|
+
throw new Error('Failed to spawn daemon process');
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Reads daemon.pid for a block and sends SIGTERM.
|
|
30
|
+
*/
|
|
31
|
+
export async function killDaemon(blockPath) {
|
|
32
|
+
try {
|
|
33
|
+
const pidPath = join(blockPath, 'daemon.pid');
|
|
34
|
+
const pidStr = await fsp.readFile(pidPath, 'utf8');
|
|
35
|
+
const pid = parseInt(pidStr.trim(), 10);
|
|
36
|
+
if (pid && !isNaN(pid)) {
|
|
37
|
+
try {
|
|
38
|
+
process.kill(pid); // Send SIGTERM
|
|
39
|
+
}
|
|
40
|
+
catch (kErr) {
|
|
41
|
+
// If ESRCH (No such process), process mapping is dead but file exists.
|
|
42
|
+
if (kErr.code !== 'ESRCH')
|
|
43
|
+
throw kErr;
|
|
44
|
+
}
|
|
45
|
+
await fsp.unlink(pidPath).catch(() => { });
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
// PID file doesn't exist
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,QAAQ,IAAI,GAAG,EAAE,MAA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GAAG,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QACnD,MAAM,GAAG,GAAG,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;QAExC,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;YACrB,IAAI,CAAC;gBACD,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,eAAe;YACtC,CAAC;YAAC,OAAO,IAAS,EAAE,CAAC;gBACjB,uEAAuE;gBACvE,IAAI,IAAI,CAAC,IAAI,KAAK,OAAO;oBAAE,MAAM,IAAI,CAAC;YAC1C,CAAC;YACD,MAAM,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;YAC1C,OAAO,IAAI,CAAC;QAChB,CAAC;IACL,CAAC;IAAC,MAAM,CAAC;QACL,yBAAyB;IAC7B,CAAC;IACD,OAAO,KAAK,CAAC;AACjB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@memoryblock/daemon",
|
|
3
|
+
"version": "0.1.0-beta",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"memoryblock": "0.1.0-beta"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsc -p tsconfig.json"
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { promises as fsp } from 'node:fs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Spawns a background daemon for a memoryblock.
|
|
7
|
+
* Detaches the process and writes its PID to blockPath/daemon.pid
|
|
8
|
+
*/
|
|
9
|
+
export async function spawnDaemon(blockName: string, _channel: string, blockPath: string): Promise<number> {
|
|
10
|
+
const scriptPath = process.argv[1];
|
|
11
|
+
|
|
12
|
+
const out = await fsp.open(join(blockPath, 'daemon-debug.log'), 'a');
|
|
13
|
+
|
|
14
|
+
// Always use 'multi' so the daemon initializes ALL available channels
|
|
15
|
+
// (web, telegram, etc.) and auto-routes messages via MultiChannelManager
|
|
16
|
+
const child = spawn(process.execPath, [
|
|
17
|
+
scriptPath, 'start', blockName, '--channel', 'multi'
|
|
18
|
+
], {
|
|
19
|
+
detached: true,
|
|
20
|
+
stdio: ['ignore', out.fd, out.fd], // Pipe to debug log
|
|
21
|
+
env: { ...process.env, MBLK_IS_DAEMON: '1' } // Preserve authentication keys and mark as daemon
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
child.unref(); // Prevent parent from waiting for child
|
|
25
|
+
await out.close();
|
|
26
|
+
|
|
27
|
+
if (child.pid) {
|
|
28
|
+
await fsp.writeFile(join(blockPath, 'daemon.pid'), child.pid.toString());
|
|
29
|
+
return child.pid;
|
|
30
|
+
}
|
|
31
|
+
throw new Error('Failed to spawn daemon process');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Reads daemon.pid for a block and sends SIGTERM.
|
|
36
|
+
*/
|
|
37
|
+
export async function killDaemon(blockPath: string): Promise<boolean> {
|
|
38
|
+
try {
|
|
39
|
+
const pidPath = join(blockPath, 'daemon.pid');
|
|
40
|
+
const pidStr = await fsp.readFile(pidPath, 'utf8');
|
|
41
|
+
const pid = parseInt(pidStr.trim(), 10);
|
|
42
|
+
|
|
43
|
+
if (pid && !isNaN(pid)) {
|
|
44
|
+
try {
|
|
45
|
+
process.kill(pid); // Send SIGTERM
|
|
46
|
+
} catch (kErr: any) {
|
|
47
|
+
// If ESRCH (No such process), process mapping is dead but file exists.
|
|
48
|
+
if (kErr.code !== 'ESRCH') throw kErr;
|
|
49
|
+
}
|
|
50
|
+
await fsp.unlink(pidPath).catch(() => {});
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
} catch {
|
|
54
|
+
// PID file doesn't exist
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|