@camstack/kernel 0.1.7 → 0.1.10

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.
@@ -1,154 +0,0 @@
1
- import {
2
- __commonJS,
3
- __require
4
- } from "./chunk-LZOMFHX3.mjs";
5
-
6
- // src/worker/worker-process-manager.js
7
- var require_worker_process_manager = __commonJS({
8
- "src/worker/worker-process-manager.js"(exports) {
9
- "use strict";
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.WorkerProcessManager = void 0;
12
- var node_child_process_1 = __require("child_process");
13
- var WorkerProcessManager = class {
14
- sendToMain;
15
- processes = /* @__PURE__ */ new Map();
16
- constructor(sendToMain) {
17
- this.sendToMain = sendToMain;
18
- }
19
- async spawn(config) {
20
- const child = (0, node_child_process_1.spawn)(config.command, [...config.args ?? []], {
21
- cwd: config.cwd,
22
- env: config.env ? { ...process.env, ...config.env } : void 0,
23
- stdio: ["pipe", "pipe", "pipe"]
24
- });
25
- const managed = new ManagedProcess(child, config, this.sendToMain);
26
- this.processes.set(child.pid, managed);
27
- this.sendToMain({
28
- type: "SUB_PROCESS_SPAWNED",
29
- pid: child.pid,
30
- name: config.name,
31
- command: config.command
32
- });
33
- child.on("exit", (code) => {
34
- this.sendToMain({
35
- type: "SUB_PROCESS_EXITED",
36
- pid: child.pid,
37
- code
38
- });
39
- if (config.autoRestart && managed.restartCount < (config.maxRestarts ?? 3)) {
40
- managed.restartCount++;
41
- setTimeout(() => {
42
- this.spawn(config).catch(() => {
43
- });
44
- }, 2e3);
45
- }
46
- });
47
- return managed;
48
- }
49
- listProcesses() {
50
- return [...this.processes.values()].map((p) => p.toInfo());
51
- }
52
- getWorkerStats() {
53
- const mem = process.memoryUsage();
54
- const cpu = process.cpuUsage();
55
- return {
56
- pid: process.pid,
57
- cpuPercent: (cpu.user + cpu.system) / 1e6,
58
- memoryRss: mem.rss,
59
- heapUsed: mem.heapUsed,
60
- uptimeSeconds: Math.round(process.uptime()),
61
- restartCount: 0,
62
- state: "running"
63
- };
64
- }
65
- async killAll() {
66
- for (const [, proc] of this.processes) {
67
- proc.kill("SIGTERM");
68
- }
69
- this.processes.clear();
70
- }
71
- };
72
- exports.WorkerProcessManager = WorkerProcessManager;
73
- var ManagedProcess = class {
74
- child;
75
- config;
76
- sendToMain;
77
- pid;
78
- name;
79
- restartCount = 0;
80
- startedAt = Date.now();
81
- exitHandlers = [];
82
- errorHandlers = [];
83
- constructor(child, config, sendToMain) {
84
- this.child = child;
85
- this.config = config;
86
- this.sendToMain = sendToMain;
87
- this.pid = child.pid;
88
- this.name = config.name;
89
- child.on("exit", (code) => {
90
- for (const handler of this.exitHandlers)
91
- handler(code);
92
- });
93
- child.on("error", (err) => {
94
- for (const handler of this.errorHandlers)
95
- handler(err);
96
- });
97
- }
98
- getStats() {
99
- return {
100
- pid: this.pid,
101
- cpuPercent: 0,
102
- memoryRss: 0,
103
- uptimeSeconds: Math.round((Date.now() - this.startedAt) / 1e3),
104
- restartCount: this.restartCount,
105
- state: this.child.exitCode !== null ? "stopped" : "running"
106
- };
107
- }
108
- write(data) {
109
- this.child.stdin?.write(data);
110
- }
111
- get stdout() {
112
- return this.child.stdout;
113
- }
114
- get stderr() {
115
- return this.child.stderr;
116
- }
117
- kill(signal = "SIGTERM") {
118
- this.child.kill(signal);
119
- }
120
- wait() {
121
- if (this.child.exitCode !== null) {
122
- return Promise.resolve({ code: this.child.exitCode, signal: null });
123
- }
124
- return new Promise((resolve) => {
125
- this.child.on("exit", (code, signal) => {
126
- resolve({ code, signal });
127
- });
128
- });
129
- }
130
- onExit(handler) {
131
- this.exitHandlers.push(handler);
132
- }
133
- onError(handler) {
134
- this.errorHandlers.push(handler);
135
- }
136
- toInfo() {
137
- return {
138
- pid: this.pid,
139
- name: this.name,
140
- command: this.config.command,
141
- state: this.child.exitCode !== null ? "stopped" : "running",
142
- cpuPercent: 0,
143
- memoryRss: 0,
144
- uptimeSeconds: Math.round((Date.now() - this.startedAt) / 1e3)
145
- };
146
- }
147
- };
148
- }
149
- });
150
-
151
- export {
152
- require_worker_process_manager
153
- };
154
- //# sourceMappingURL=chunk-RHK5CCAL.mjs.map