@livekit/agents 0.4.6 → 0.5.1
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/README.md +17 -0
- package/dist/audio.cjs +77 -0
- package/dist/audio.cjs.map +1 -0
- package/dist/audio.js +48 -37
- package/dist/audio.js.map +1 -1
- package/dist/cli.cjs +131 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.js +96 -122
- package/dist/cli.js.map +1 -1
- package/dist/generator.cjs +36 -0
- package/dist/generator.cjs.map +1 -0
- package/dist/generator.js +8 -22
- package/dist/generator.js.map +1 -1
- package/dist/http_server.cjs +72 -0
- package/dist/http_server.cjs.map +1 -0
- package/dist/http_server.d.ts +1 -1
- package/dist/http_server.js +44 -47
- package/dist/http_server.js.map +1 -1
- package/dist/index.cjs +78 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +26 -28
- package/dist/index.js.map +1 -1
- package/dist/ipc/job_executor.cjs +33 -0
- package/dist/ipc/job_executor.cjs.map +1 -0
- package/dist/ipc/job_executor.js +7 -4
- package/dist/ipc/job_executor.js.map +1 -1
- package/dist/ipc/job_main.cjs +147 -0
- package/dist/ipc/job_main.cjs.map +1 -0
- package/dist/ipc/job_main.d.ts +1 -1
- package/dist/ipc/job_main.js +103 -103
- package/dist/ipc/job_main.js.map +1 -1
- package/dist/ipc/message.cjs +17 -0
- package/dist/ipc/message.cjs.map +1 -0
- package/dist/ipc/message.js +0 -1
- package/dist/ipc/message.js.map +1 -1
- package/dist/ipc/proc_job_executor.cjs +174 -0
- package/dist/ipc/proc_job_executor.cjs.map +1 -0
- package/dist/ipc/proc_job_executor.js +130 -126
- package/dist/ipc/proc_job_executor.js.map +1 -1
- package/dist/ipc/proc_pool.cjs +126 -0
- package/dist/ipc/proc_pool.cjs.map +1 -0
- package/dist/ipc/proc_pool.js +93 -96
- package/dist/ipc/proc_pool.js.map +1 -1
- package/dist/job.cjs +230 -0
- package/dist/job.cjs.map +1 -0
- package/dist/job.d.ts +6 -1
- package/dist/job.d.ts.map +1 -1
- package/dist/job.js +195 -198
- package/dist/job.js.map +1 -1
- package/dist/llm/chat_context.cjs +131 -0
- package/dist/llm/chat_context.cjs.map +1 -0
- package/dist/llm/chat_context.js +98 -86
- package/dist/llm/chat_context.js.map +1 -1
- package/dist/llm/function_context.cjs +103 -0
- package/dist/llm/function_context.cjs.map +1 -0
- package/dist/llm/function_context.js +72 -81
- package/dist/llm/function_context.js.map +1 -1
- package/dist/llm/function_context.test.cjs +218 -0
- package/dist/llm/function_context.test.cjs.map +1 -0
- package/dist/llm/function_context.test.js +209 -210
- package/dist/llm/function_context.test.js.map +1 -1
- package/dist/llm/index.cjs +43 -0
- package/dist/llm/index.cjs.map +1 -0
- package/dist/llm/index.js +22 -6
- package/dist/llm/index.js.map +1 -1
- package/dist/llm/llm.cjs +76 -0
- package/dist/llm/llm.cjs.map +1 -0
- package/dist/llm/llm.js +48 -42
- package/dist/llm/llm.js.map +1 -1
- package/dist/log.cjs +57 -0
- package/dist/log.cjs.map +1 -0
- package/dist/log.js +27 -26
- package/dist/log.js.map +1 -1
- package/dist/multimodal/agent_playout.cjs +228 -0
- package/dist/multimodal/agent_playout.cjs.map +1 -0
- package/dist/multimodal/agent_playout.d.ts +1 -1
- package/dist/multimodal/agent_playout.js +193 -180
- package/dist/multimodal/agent_playout.js.map +1 -1
- package/dist/multimodal/index.cjs +25 -0
- package/dist/multimodal/index.cjs.map +1 -0
- package/dist/multimodal/index.js +2 -5
- package/dist/multimodal/index.js.map +1 -1
- package/dist/multimodal/multimodal_agent.cjs +404 -0
- package/dist/multimodal/multimodal_agent.cjs.map +1 -0
- package/dist/multimodal/multimodal_agent.d.ts +1 -1
- package/dist/multimodal/multimodal_agent.js +351 -330
- package/dist/multimodal/multimodal_agent.js.map +1 -1
- package/dist/pipeline/agent_output.cjs +172 -0
- package/dist/pipeline/agent_output.cjs.map +1 -0
- package/dist/pipeline/agent_output.js +136 -138
- package/dist/pipeline/agent_output.js.map +1 -1
- package/dist/pipeline/agent_playout.cjs +169 -0
- package/dist/pipeline/agent_playout.cjs.map +1 -0
- package/dist/pipeline/agent_playout.js +126 -136
- package/dist/pipeline/agent_playout.js.map +1 -1
- package/dist/pipeline/human_input.cjs +158 -0
- package/dist/pipeline/human_input.cjs.map +1 -0
- package/dist/pipeline/human_input.js +124 -125
- package/dist/pipeline/human_input.js.map +1 -1
- package/dist/pipeline/index.cjs +31 -0
- package/dist/pipeline/index.cjs.map +1 -0
- package/dist/pipeline/index.js +8 -4
- package/dist/pipeline/index.js.map +1 -1
- package/dist/pipeline/pipeline_agent.cjs +642 -0
- package/dist/pipeline/pipeline_agent.cjs.map +1 -0
- package/dist/pipeline/pipeline_agent.js +595 -651
- package/dist/pipeline/pipeline_agent.js.map +1 -1
- package/dist/pipeline/speech_handle.cjs +128 -0
- package/dist/pipeline/speech_handle.cjs.map +1 -0
- package/dist/pipeline/speech_handle.js +102 -100
- package/dist/pipeline/speech_handle.js.map +1 -1
- package/dist/plugin.cjs +46 -0
- package/dist/plugin.cjs.map +1 -0
- package/dist/plugin.js +20 -20
- package/dist/plugin.js.map +1 -1
- package/dist/stt/index.cjs +38 -0
- package/dist/stt/index.cjs.map +1 -0
- package/dist/stt/index.js +13 -5
- package/dist/stt/index.js.map +1 -1
- package/dist/stt/stream_adapter.cjs +87 -0
- package/dist/stt/stream_adapter.cjs.map +1 -0
- package/dist/stt/stream_adapter.js +58 -55
- package/dist/stt/stream_adapter.js.map +1 -1
- package/dist/stt/stt.cjs +98 -0
- package/dist/stt/stt.cjs.map +1 -0
- package/dist/stt/stt.js +63 -98
- package/dist/stt/stt.js.map +1 -1
- package/dist/tokenize/basic/basic.cjs +98 -0
- package/dist/tokenize/basic/basic.cjs.map +1 -0
- package/dist/tokenize/basic/basic.d.ts +1 -1
- package/dist/tokenize/basic/basic.d.ts.map +1 -1
- package/dist/tokenize/basic/basic.js +56 -45
- package/dist/tokenize/basic/basic.js.map +1 -1
- package/dist/tokenize/basic/hyphenator.cjs +425 -0
- package/dist/tokenize/basic/hyphenator.cjs.map +1 -0
- package/dist/tokenize/basic/hyphenator.js +66 -82
- package/dist/tokenize/basic/hyphenator.js.map +1 -1
- package/dist/tokenize/basic/index.cjs +35 -0
- package/dist/tokenize/basic/index.cjs.map +1 -0
- package/dist/tokenize/basic/index.js +7 -4
- package/dist/tokenize/basic/index.js.map +1 -1
- package/dist/tokenize/basic/paragraph.cjs +57 -0
- package/dist/tokenize/basic/paragraph.cjs.map +1 -0
- package/dist/tokenize/basic/paragraph.js +30 -35
- package/dist/tokenize/basic/paragraph.js.map +1 -1
- package/dist/tokenize/basic/sentence.cjs +89 -0
- package/dist/tokenize/basic/sentence.cjs.map +1 -0
- package/dist/tokenize/basic/sentence.d.ts.map +1 -1
- package/dist/tokenize/basic/sentence.js +62 -57
- package/dist/tokenize/basic/sentence.js.map +1 -1
- package/dist/tokenize/basic/word.cjs +44 -0
- package/dist/tokenize/basic/word.cjs.map +1 -0
- package/dist/tokenize/basic/word.js +17 -20
- package/dist/tokenize/basic/word.js.map +1 -1
- package/dist/tokenize/index.cjs +55 -0
- package/dist/tokenize/index.cjs.map +1 -0
- package/dist/tokenize/index.js +18 -7
- package/dist/tokenize/index.js.map +1 -1
- package/dist/tokenize/token_stream.cjs +164 -0
- package/dist/tokenize/token_stream.cjs.map +1 -0
- package/dist/tokenize/token_stream.js +133 -139
- package/dist/tokenize/token_stream.js.map +1 -1
- package/dist/tokenize/tokenizer.cjs +184 -0
- package/dist/tokenize/tokenizer.cjs.map +1 -0
- package/dist/tokenize/tokenizer.js +138 -99
- package/dist/tokenize/tokenizer.js.map +1 -1
- package/dist/tokenize/tokenizer.test.cjs +220 -0
- package/dist/tokenize/tokenizer.test.cjs.map +1 -0
- package/dist/tokenize/tokenizer.test.d.ts +2 -0
- package/dist/tokenize/tokenizer.test.d.ts.map +1 -0
- package/dist/tokenize/tokenizer.test.js +219 -0
- package/dist/tokenize/tokenizer.test.js.map +1 -0
- package/dist/transcription.cjs +131 -0
- package/dist/transcription.cjs.map +1 -0
- package/dist/transcription.js +99 -96
- package/dist/transcription.js.map +1 -1
- package/dist/tts/index.cjs +38 -0
- package/dist/tts/index.cjs.map +1 -0
- package/dist/tts/index.js +13 -5
- package/dist/tts/index.js.map +1 -1
- package/dist/tts/stream_adapter.cjs +78 -0
- package/dist/tts/stream_adapter.cjs.map +1 -0
- package/dist/tts/stream_adapter.js +50 -47
- package/dist/tts/stream_adapter.js.map +1 -1
- package/dist/tts/tts.cjs +127 -0
- package/dist/tts/tts.cjs.map +1 -0
- package/dist/tts/tts.js +90 -120
- package/dist/tts/tts.js.map +1 -1
- package/dist/utils.cjs +284 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.js +242 -247
- package/dist/utils.js.map +1 -1
- package/dist/vad.cjs +92 -0
- package/dist/vad.cjs.map +1 -0
- package/dist/vad.js +57 -52
- package/dist/vad.js.map +1 -1
- package/dist/version.cjs +29 -0
- package/dist/version.cjs.map +1 -0
- package/dist/version.js +4 -4
- package/dist/version.js.map +1 -1
- package/dist/worker.cjs +577 -0
- package/dist/worker.cjs.map +1 -0
- package/dist/worker.d.ts +1 -1
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +512 -484
- package/dist/worker.js.map +1 -1
- package/package.json +18 -8
- package/src/ipc/job_main.ts +66 -64
- package/src/job.ts +3 -2
- package/src/pipeline/pipeline_agent.ts +23 -23
- package/src/tokenize/basic/basic.ts +1 -1
- package/src/tokenize/basic/sentence.ts +14 -8
- package/src/tokenize/tokenizer.test.ts +255 -0
- package/src/worker.ts +1 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var proc_job_executor_exports = {};
|
|
30
|
+
__export(proc_job_executor_exports, {
|
|
31
|
+
ProcJobExecutor: () => ProcJobExecutor
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(proc_job_executor_exports);
|
|
34
|
+
var import_node_events = require("node:events");
|
|
35
|
+
var import_log = require("../log.cjs");
|
|
36
|
+
var import_utils = require("../utils.cjs");
|
|
37
|
+
var import_job_executor = require("./job_executor.cjs");
|
|
38
|
+
class ProcJobExecutor extends import_job_executor.JobExecutor {
|
|
39
|
+
#opts;
|
|
40
|
+
#started = false;
|
|
41
|
+
#closing = false;
|
|
42
|
+
#runningJob = void 0;
|
|
43
|
+
#proc;
|
|
44
|
+
#pingInterval;
|
|
45
|
+
#pongTimeout;
|
|
46
|
+
#init = new import_utils.Future();
|
|
47
|
+
#join = new import_utils.Future();
|
|
48
|
+
#logger = (0, import_log.log)().child({ runningJob: this.#runningJob });
|
|
49
|
+
constructor(agent, initializeTimeout, closeTimeout) {
|
|
50
|
+
super();
|
|
51
|
+
this.#opts = {
|
|
52
|
+
agent,
|
|
53
|
+
initializeTimeout,
|
|
54
|
+
closeTimeout
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
get started() {
|
|
58
|
+
return this.#started;
|
|
59
|
+
}
|
|
60
|
+
get runningJob() {
|
|
61
|
+
return this.#runningJob;
|
|
62
|
+
}
|
|
63
|
+
async start() {
|
|
64
|
+
if (this.#started) {
|
|
65
|
+
throw new Error("runner already started");
|
|
66
|
+
} else if (this.#closing) {
|
|
67
|
+
throw new Error("runner is closed");
|
|
68
|
+
}
|
|
69
|
+
this.#proc = await import("./job_main.cjs").then(
|
|
70
|
+
(m) => m.runProcess({
|
|
71
|
+
agentFile: this.#opts.agent
|
|
72
|
+
})
|
|
73
|
+
);
|
|
74
|
+
this.#started = true;
|
|
75
|
+
this.run();
|
|
76
|
+
}
|
|
77
|
+
async run() {
|
|
78
|
+
await this.#init.await;
|
|
79
|
+
this.#pingInterval = setInterval(() => {
|
|
80
|
+
this.#proc.send({ case: "pingRequest", value: { timestamp: Date.now() } });
|
|
81
|
+
}, this.PING_INTERVAL);
|
|
82
|
+
this.#pongTimeout = setTimeout(() => {
|
|
83
|
+
this.#logger.warn("job is unresponsive");
|
|
84
|
+
clearTimeout(this.#pongTimeout);
|
|
85
|
+
clearInterval(this.#pingInterval);
|
|
86
|
+
this.#proc.kill();
|
|
87
|
+
this.#join.resolve();
|
|
88
|
+
}, this.PING_TIMEOUT);
|
|
89
|
+
const listener = (msg) => {
|
|
90
|
+
var _a;
|
|
91
|
+
switch (msg.case) {
|
|
92
|
+
case "pongResponse": {
|
|
93
|
+
const delay = Date.now() - msg.value.timestamp;
|
|
94
|
+
if (delay > this.HIGH_PING_THRESHOLD) {
|
|
95
|
+
this.#logger.child({ delay }).warn("job executor is unresponsive");
|
|
96
|
+
}
|
|
97
|
+
(_a = this.#pongTimeout) == null ? void 0 : _a.refresh();
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
case "exiting": {
|
|
101
|
+
this.#logger.child({ reason: msg.value.reason }).debug("job exiting");
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
case "done": {
|
|
105
|
+
this.#closing = true;
|
|
106
|
+
this.#proc.off("message", listener);
|
|
107
|
+
this.#join.resolve();
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
this.#proc.on("message", listener);
|
|
113
|
+
this.#proc.on("error", (err) => {
|
|
114
|
+
if (this.#closing) return;
|
|
115
|
+
this.#logger.child({ err }).warn("job process exited unexpectedly");
|
|
116
|
+
clearTimeout(this.#pongTimeout);
|
|
117
|
+
clearInterval(this.#pingInterval);
|
|
118
|
+
this.#join.resolve();
|
|
119
|
+
});
|
|
120
|
+
await this.#join.await;
|
|
121
|
+
}
|
|
122
|
+
async join() {
|
|
123
|
+
if (!this.#started) {
|
|
124
|
+
throw new Error("runner not started");
|
|
125
|
+
}
|
|
126
|
+
await this.#join.await;
|
|
127
|
+
}
|
|
128
|
+
async initialize() {
|
|
129
|
+
const timer = setTimeout(() => {
|
|
130
|
+
const err = new Error("runner initialization timed out");
|
|
131
|
+
this.#init.reject(err);
|
|
132
|
+
throw err;
|
|
133
|
+
}, this.#opts.initializeTimeout);
|
|
134
|
+
this.#proc.send({ case: "initializeRequest", value: { loggerOptions: import_log.loggerOptions } });
|
|
135
|
+
await (0, import_node_events.once)(this.#proc, "message").then(([msg]) => {
|
|
136
|
+
clearTimeout(timer);
|
|
137
|
+
if (msg.case !== "initializeResponse") {
|
|
138
|
+
throw new Error("first message must be InitializeResponse");
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
this.#init.resolve();
|
|
142
|
+
}
|
|
143
|
+
async close() {
|
|
144
|
+
if (!this.#started) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
this.#closing = true;
|
|
148
|
+
if (!this.#runningJob) {
|
|
149
|
+
this.#proc.kill();
|
|
150
|
+
this.#join.resolve();
|
|
151
|
+
}
|
|
152
|
+
this.#proc.send({ case: "shutdownRequest" });
|
|
153
|
+
const timer = setTimeout(() => {
|
|
154
|
+
this.#logger.error("job shutdown is taking too much time");
|
|
155
|
+
}, this.#opts.closeTimeout);
|
|
156
|
+
await this.#join.await.then(() => {
|
|
157
|
+
clearTimeout(timer);
|
|
158
|
+
clearTimeout(this.#pongTimeout);
|
|
159
|
+
clearInterval(this.#pingInterval);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
async launchJob(info) {
|
|
163
|
+
if (this.#runningJob) {
|
|
164
|
+
throw new Error("executor already has a running job");
|
|
165
|
+
}
|
|
166
|
+
this.#runningJob = info;
|
|
167
|
+
this.#proc.send({ case: "startJobRequest", value: { runningJob: info } });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
171
|
+
0 && (module.exports = {
|
|
172
|
+
ProcJobExecutor
|
|
173
|
+
});
|
|
174
|
+
//# sourceMappingURL=proc_job_executor.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/ipc/proc_job_executor.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChildProcess } from 'node:child_process';\nimport { once } from 'node:events';\nimport type { RunningJobInfo } from '../job.js';\nimport { log, loggerOptions } from '../log.js';\nimport { Future } from '../utils.js';\nimport type { ProcOpts } from './job_executor.js';\nimport { JobExecutor } from './job_executor.js';\nimport type { IPCMessage } from './message.js';\n\nexport class ProcJobExecutor extends JobExecutor {\n #opts: ProcOpts;\n #started = false;\n #closing = false;\n #runningJob?: RunningJobInfo = undefined;\n #proc?: ChildProcess;\n #pingInterval?: ReturnType<typeof setInterval>;\n #pongTimeout?: ReturnType<typeof setTimeout>;\n #init = new Future();\n #join = new Future();\n #logger = log().child({ runningJob: this.#runningJob });\n\n constructor(agent: string, initializeTimeout: number, closeTimeout: number) {\n super();\n this.#opts = {\n agent,\n initializeTimeout,\n closeTimeout,\n };\n }\n\n get started(): boolean {\n return this.#started;\n }\n\n get runningJob(): RunningJobInfo | undefined {\n return this.#runningJob;\n }\n\n async start() {\n if (this.#started) {\n throw new Error('runner already started');\n } else if (this.#closing) {\n throw new Error('runner is closed');\n }\n\n this.#proc = await import('./job_main.js').then((m) =>\n m.runProcess({\n agentFile: this.#opts.agent,\n }),\n );\n\n this.#started = true;\n this.run();\n }\n\n async run() {\n await this.#init.await;\n\n this.#pingInterval = setInterval(() => {\n this.#proc!.send({ case: 'pingRequest', value: { timestamp: Date.now() } });\n }, this.PING_INTERVAL);\n\n this.#pongTimeout = setTimeout(() => {\n this.#logger.warn('job is unresponsive');\n clearTimeout(this.#pongTimeout);\n clearInterval(this.#pingInterval);\n this.#proc!.kill();\n this.#join.resolve();\n }, this.PING_TIMEOUT);\n\n const listener = (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pongResponse': {\n const delay = Date.now() - msg.value.timestamp;\n if (delay > this.HIGH_PING_THRESHOLD) {\n this.#logger.child({ delay }).warn('job executor is unresponsive');\n }\n this.#pongTimeout?.refresh();\n break;\n }\n case 'exiting': {\n this.#logger.child({ reason: msg.value.reason }).debug('job exiting');\n break;\n }\n case 'done': {\n this.#closing = true;\n this.#proc!.off('message', listener);\n this.#join.resolve();\n break;\n }\n }\n };\n this.#proc!.on('message', listener);\n this.#proc!.on('error', (err) => {\n if (this.#closing) return;\n this.#logger.child({ err }).warn('job process exited unexpectedly');\n clearTimeout(this.#pongTimeout);\n clearInterval(this.#pingInterval);\n this.#join.resolve();\n });\n\n await this.#join.await;\n }\n\n async join() {\n if (!this.#started) {\n throw new Error('runner not started');\n }\n\n await this.#join.await;\n }\n\n async initialize() {\n const timer = setTimeout(() => {\n const err = new Error('runner initialization timed out');\n this.#init.reject(err);\n throw err;\n }, this.#opts.initializeTimeout);\n this.#proc!.send({ case: 'initializeRequest', value: { loggerOptions } });\n await once(this.#proc!, 'message').then(([msg]: IPCMessage[]) => {\n clearTimeout(timer);\n if (msg!.case !== 'initializeResponse') {\n throw new Error('first message must be InitializeResponse');\n }\n });\n this.#init.resolve();\n }\n\n async close() {\n if (!this.#started) {\n return;\n }\n this.#closing = true;\n\n if (!this.#runningJob) {\n this.#proc!.kill();\n this.#join.resolve();\n }\n\n this.#proc!.send({ case: 'shutdownRequest' });\n\n const timer = setTimeout(() => {\n this.#logger.error('job shutdown is taking too much time');\n }, this.#opts.closeTimeout);\n await this.#join.await.then(() => {\n clearTimeout(timer);\n clearTimeout(this.#pongTimeout);\n clearInterval(this.#pingInterval);\n });\n }\n\n async launchJob(info: RunningJobInfo) {\n if (this.#runningJob) {\n throw new Error('executor already has a running job');\n }\n this.#runningJob = info;\n this.#proc!.send({ case: 'startJobRequest', value: { runningJob: info } });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,yBAAqB;AAErB,iBAAmC;AACnC,mBAAuB;AAEvB,0BAA4B;AAGrB,MAAM,wBAAwB,gCAAY;AAAA,EAC/C;AAAA,EACA,WAAW;AAAA,EACX,WAAW;AAAA,EACX,cAA+B;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA,QAAQ,IAAI,oBAAO;AAAA,EACnB,QAAQ,IAAI,oBAAO;AAAA,EACnB,cAAU,gBAAI,EAAE,MAAM,EAAE,YAAY,KAAK,YAAY,CAAC;AAAA,EAEtD,YAAY,OAAe,mBAA2B,cAAsB;AAC1E,UAAM;AACN,SAAK,QAAQ;AAAA,MACX;AAAA,MACA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AAK,aAAa;AACrB,WAAK,MAAO,KAAK;AACjB,WAAK,MAAM,QAAQ;AAAA,IACrB;AAEA,SAAK,MAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAE5C,UAAM,QAAQ,WAAW,MAAM;AAC7B,WAAK,QAAQ,MAAM,sCAAsC;AAAA,IAC3D,GAAG,KAAK,MAAM,YAAY;AAC1B,UAAM,KAAK,MAAM,MAAM,KAAK,MAAM;AAChC,mBAAa,KAAK;AAClB,mBAAa,KAAK,YAAY;AAC9B,oBAAc,KAAK,aAAa;AAAA,IAClC,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,UAAU,MAAsB;AACpC,QAAI,KAAK,aAAa;AACpB,YAAM,IAAI,MAAM,oCAAoC;AAAA,IACtD;AACA,SAAK,cAAc;AACnB,SAAK,MAAO,KAAK,EAAE,MAAM,mBAAmB,OAAO,EAAE,YAAY,KAAK,EAAE,CAAC;AAAA,EAC3E;AACF;","names":[]}
|
|
@@ -1,136 +1,140 @@
|
|
|
1
|
-
import { once } from
|
|
2
|
-
import { log, loggerOptions } from
|
|
3
|
-
import { Future } from
|
|
4
|
-
import { JobExecutor } from
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
import { once } from "node:events";
|
|
2
|
+
import { log, loggerOptions } from "../log.js";
|
|
3
|
+
import { Future } from "../utils.js";
|
|
4
|
+
import { JobExecutor } from "./job_executor.js";
|
|
5
|
+
class ProcJobExecutor extends JobExecutor {
|
|
6
|
+
#opts;
|
|
7
|
+
#started = false;
|
|
8
|
+
#closing = false;
|
|
9
|
+
#runningJob = void 0;
|
|
10
|
+
#proc;
|
|
11
|
+
#pingInterval;
|
|
12
|
+
#pongTimeout;
|
|
13
|
+
#init = new Future();
|
|
14
|
+
#join = new Future();
|
|
15
|
+
#logger = log().child({ runningJob: this.#runningJob });
|
|
16
|
+
constructor(agent, initializeTimeout, closeTimeout) {
|
|
17
|
+
super();
|
|
18
|
+
this.#opts = {
|
|
19
|
+
agent,
|
|
20
|
+
initializeTimeout,
|
|
21
|
+
closeTimeout
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
get started() {
|
|
25
|
+
return this.#started;
|
|
26
|
+
}
|
|
27
|
+
get runningJob() {
|
|
28
|
+
return this.#runningJob;
|
|
29
|
+
}
|
|
30
|
+
async start() {
|
|
31
|
+
if (this.#started) {
|
|
32
|
+
throw new Error("runner already started");
|
|
33
|
+
} else if (this.#closing) {
|
|
34
|
+
throw new Error("runner is closed");
|
|
23
35
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
36
|
+
this.#proc = await import("./job_main.js").then(
|
|
37
|
+
(m) => m.runProcess({
|
|
38
|
+
agentFile: this.#opts.agent
|
|
39
|
+
})
|
|
40
|
+
);
|
|
41
|
+
this.#started = true;
|
|
42
|
+
this.run();
|
|
43
|
+
}
|
|
44
|
+
async run() {
|
|
45
|
+
await this.#init.await;
|
|
46
|
+
this.#pingInterval = setInterval(() => {
|
|
47
|
+
this.#proc.send({ case: "pingRequest", value: { timestamp: Date.now() } });
|
|
48
|
+
}, this.PING_INTERVAL);
|
|
49
|
+
this.#pongTimeout = setTimeout(() => {
|
|
50
|
+
this.#logger.warn("job is unresponsive");
|
|
51
|
+
clearTimeout(this.#pongTimeout);
|
|
52
|
+
clearInterval(this.#pingInterval);
|
|
53
|
+
this.#proc.kill();
|
|
54
|
+
this.#join.resolve();
|
|
55
|
+
}, this.PING_TIMEOUT);
|
|
56
|
+
const listener = (msg) => {
|
|
57
|
+
var _a;
|
|
58
|
+
switch (msg.case) {
|
|
59
|
+
case "pongResponse": {
|
|
60
|
+
const delay = Date.now() - msg.value.timestamp;
|
|
61
|
+
if (delay > this.HIGH_PING_THRESHOLD) {
|
|
62
|
+
this.#logger.child({ delay }).warn("job executor is unresponsive");
|
|
63
|
+
}
|
|
64
|
+
(_a = this.#pongTimeout) == null ? void 0 : _a.refresh();
|
|
65
|
+
break;
|
|
33
66
|
}
|
|
34
|
-
|
|
35
|
-
|
|
67
|
+
case "exiting": {
|
|
68
|
+
this.#logger.child({ reason: msg.value.reason }).debug("job exiting");
|
|
69
|
+
break;
|
|
36
70
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
async run() {
|
|
44
|
-
await this.#init.await;
|
|
45
|
-
this.#pingInterval = setInterval(() => {
|
|
46
|
-
this.#proc.send({ case: 'pingRequest', value: { timestamp: Date.now() } });
|
|
47
|
-
}, this.PING_INTERVAL);
|
|
48
|
-
this.#pongTimeout = setTimeout(() => {
|
|
49
|
-
this.#logger.warn('job is unresponsive');
|
|
50
|
-
clearTimeout(this.#pongTimeout);
|
|
51
|
-
clearInterval(this.#pingInterval);
|
|
52
|
-
this.#proc.kill();
|
|
53
|
-
this.#join.resolve();
|
|
54
|
-
}, this.PING_TIMEOUT);
|
|
55
|
-
const listener = (msg) => {
|
|
56
|
-
switch (msg.case) {
|
|
57
|
-
case 'pongResponse': {
|
|
58
|
-
const delay = Date.now() - msg.value.timestamp;
|
|
59
|
-
if (delay > this.HIGH_PING_THRESHOLD) {
|
|
60
|
-
this.#logger.child({ delay }).warn('job executor is unresponsive');
|
|
61
|
-
}
|
|
62
|
-
this.#pongTimeout?.refresh();
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
case 'exiting': {
|
|
66
|
-
this.#logger.child({ reason: msg.value.reason }).debug('job exiting');
|
|
67
|
-
break;
|
|
68
|
-
}
|
|
69
|
-
case 'done': {
|
|
70
|
-
this.#closing = true;
|
|
71
|
-
this.#proc.off('message', listener);
|
|
72
|
-
this.#join.resolve();
|
|
73
|
-
break;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
this.#proc.on('message', listener);
|
|
78
|
-
this.#proc.on('error', (err) => {
|
|
79
|
-
if (this.#closing)
|
|
80
|
-
return;
|
|
81
|
-
this.#logger.child({ err }).warn('job process exited unexpectedly');
|
|
82
|
-
clearTimeout(this.#pongTimeout);
|
|
83
|
-
clearInterval(this.#pingInterval);
|
|
84
|
-
this.#join.resolve();
|
|
85
|
-
});
|
|
86
|
-
await this.#join.await;
|
|
87
|
-
}
|
|
88
|
-
async join() {
|
|
89
|
-
if (!this.#started) {
|
|
90
|
-
throw new Error('runner not started');
|
|
71
|
+
case "done": {
|
|
72
|
+
this.#closing = true;
|
|
73
|
+
this.#proc.off("message", listener);
|
|
74
|
+
this.#join.resolve();
|
|
75
|
+
break;
|
|
91
76
|
}
|
|
92
|
-
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
this.#proc.on("message", listener);
|
|
80
|
+
this.#proc.on("error", (err) => {
|
|
81
|
+
if (this.#closing) return;
|
|
82
|
+
this.#logger.child({ err }).warn("job process exited unexpectedly");
|
|
83
|
+
clearTimeout(this.#pongTimeout);
|
|
84
|
+
clearInterval(this.#pingInterval);
|
|
85
|
+
this.#join.resolve();
|
|
86
|
+
});
|
|
87
|
+
await this.#join.await;
|
|
88
|
+
}
|
|
89
|
+
async join() {
|
|
90
|
+
if (!this.#started) {
|
|
91
|
+
throw new Error("runner not started");
|
|
93
92
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
93
|
+
await this.#join.await;
|
|
94
|
+
}
|
|
95
|
+
async initialize() {
|
|
96
|
+
const timer = setTimeout(() => {
|
|
97
|
+
const err = new Error("runner initialization timed out");
|
|
98
|
+
this.#init.reject(err);
|
|
99
|
+
throw err;
|
|
100
|
+
}, this.#opts.initializeTimeout);
|
|
101
|
+
this.#proc.send({ case: "initializeRequest", value: { loggerOptions } });
|
|
102
|
+
await once(this.#proc, "message").then(([msg]) => {
|
|
103
|
+
clearTimeout(timer);
|
|
104
|
+
if (msg.case !== "initializeResponse") {
|
|
105
|
+
throw new Error("first message must be InitializeResponse");
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
this.#init.resolve();
|
|
109
|
+
}
|
|
110
|
+
async close() {
|
|
111
|
+
if (!this.#started) {
|
|
112
|
+
return;
|
|
108
113
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
this.#closing = true;
|
|
114
|
-
if (!this.#runningJob) {
|
|
115
|
-
this.#proc.kill();
|
|
116
|
-
this.#join.resolve();
|
|
117
|
-
}
|
|
118
|
-
this.#proc.send({ case: 'shutdownRequest' });
|
|
119
|
-
const timer = setTimeout(() => {
|
|
120
|
-
this.#logger.error('job shutdown is taking too much time');
|
|
121
|
-
}, this.#opts.closeTimeout);
|
|
122
|
-
await this.#join.await.then(() => {
|
|
123
|
-
clearTimeout(timer);
|
|
124
|
-
clearTimeout(this.#pongTimeout);
|
|
125
|
-
clearInterval(this.#pingInterval);
|
|
126
|
-
});
|
|
114
|
+
this.#closing = true;
|
|
115
|
+
if (!this.#runningJob) {
|
|
116
|
+
this.#proc.kill();
|
|
117
|
+
this.#join.resolve();
|
|
127
118
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
119
|
+
this.#proc.send({ case: "shutdownRequest" });
|
|
120
|
+
const timer = setTimeout(() => {
|
|
121
|
+
this.#logger.error("job shutdown is taking too much time");
|
|
122
|
+
}, this.#opts.closeTimeout);
|
|
123
|
+
await this.#join.await.then(() => {
|
|
124
|
+
clearTimeout(timer);
|
|
125
|
+
clearTimeout(this.#pongTimeout);
|
|
126
|
+
clearInterval(this.#pingInterval);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
async launchJob(info) {
|
|
130
|
+
if (this.#runningJob) {
|
|
131
|
+
throw new Error("executor already has a running job");
|
|
134
132
|
}
|
|
133
|
+
this.#runningJob = info;
|
|
134
|
+
this.#proc.send({ case: "startJobRequest", value: { runningJob: info } });
|
|
135
|
+
}
|
|
135
136
|
}
|
|
137
|
+
export {
|
|
138
|
+
ProcJobExecutor
|
|
139
|
+
};
|
|
136
140
|
//# sourceMappingURL=proc_job_executor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../../src/ipc/proc_job_executor.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChildProcess } from 'node:child_process';\nimport { once } from 'node:events';\nimport type { RunningJobInfo } from '../job.js';\nimport { log, loggerOptions } from '../log.js';\nimport { Future } from '../utils.js';\nimport type { ProcOpts } from './job_executor.js';\nimport { JobExecutor } from './job_executor.js';\nimport type { IPCMessage } from './message.js';\n\nexport class ProcJobExecutor extends JobExecutor {\n #opts: ProcOpts;\n #started = false;\n #closing = false;\n #runningJob?: RunningJobInfo = undefined;\n #proc?: ChildProcess;\n #pingInterval?: ReturnType<typeof setInterval>;\n #pongTimeout?: ReturnType<typeof setTimeout>;\n #init = new Future();\n #join = new Future();\n #logger = log().child({ runningJob: this.#runningJob });\n\n constructor(agent: string, initializeTimeout: number, closeTimeout: number) {\n super();\n this.#opts = {\n agent,\n initializeTimeout,\n closeTimeout,\n };\n }\n\n get started(): boolean {\n return this.#started;\n }\n\n get runningJob(): RunningJobInfo | undefined {\n return this.#runningJob;\n }\n\n async start() {\n if (this.#started) {\n throw new Error('runner already started');\n } else if (this.#closing) {\n throw new Error('runner is closed');\n }\n\n this.#proc = await import('./job_main.js').then((m) =>\n m.runProcess({\n agentFile: this.#opts.agent,\n }),\n );\n\n this.#started = true;\n this.run();\n }\n\n async run() {\n await this.#init.await;\n\n this.#pingInterval = setInterval(() => {\n this.#proc!.send({ case: 'pingRequest', value: { timestamp: Date.now() } });\n }, this.PING_INTERVAL);\n\n this.#pongTimeout = setTimeout(() => {\n this.#logger.warn('job is unresponsive');\n clearTimeout(this.#pongTimeout);\n clearInterval(this.#pingInterval);\n this.#proc!.kill();\n this.#join.resolve();\n }, this.PING_TIMEOUT);\n\n const listener = (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pongResponse': {\n const delay = Date.now() - msg.value.timestamp;\n if (delay > this.HIGH_PING_THRESHOLD) {\n this.#logger.child({ delay }).warn('job executor is unresponsive');\n }\n this.#pongTimeout?.refresh();\n break;\n }\n case 'exiting': {\n this.#logger.child({ reason: msg.value.reason }).debug('job exiting');\n break;\n }\n case 'done': {\n this.#closing = true;\n this.#proc!.off('message', listener);\n this.#join.resolve();\n break;\n }\n }\n };\n this.#proc!.on('message', listener);\n this.#proc!.on('error', (err) => {\n if (this.#closing) return;\n this.#logger.child({ err }).warn('job process exited unexpectedly');\n clearTimeout(this.#pongTimeout);\n clearInterval(this.#pingInterval);\n this.#join.resolve();\n });\n\n await this.#join.await;\n }\n\n async join() {\n if (!this.#started) {\n throw new Error('runner not started');\n }\n\n await this.#join.await;\n }\n\n async initialize() {\n const timer = setTimeout(() => {\n const err = new Error('runner initialization timed out');\n this.#init.reject(err);\n throw err;\n }, this.#opts.initializeTimeout);\n this.#proc!.send({ case: 'initializeRequest', value: { loggerOptions } });\n await once(this.#proc!, 'message').then(([msg]: IPCMessage[]) => {\n clearTimeout(timer);\n if (msg!.case !== 'initializeResponse') {\n throw new Error('first message must be InitializeResponse');\n }\n });\n this.#init.resolve();\n }\n\n async close() {\n if (!this.#started) {\n return;\n }\n this.#closing = true;\n\n if (!this.#runningJob) {\n this.#proc!.kill();\n this.#join.resolve();\n }\n\n this.#proc!.send({ case: 'shutdownRequest' });\n\n const timer = setTimeout(() => {\n this.#logger.error('job shutdown is taking too much time');\n }, this.#opts.closeTimeout);\n await this.#join.await.then(() => {\n clearTimeout(timer);\n clearTimeout(this.#pongTimeout);\n clearInterval(this.#pingInterval);\n });\n }\n\n async launchJob(info: RunningJobInfo) {\n if (this.#runningJob) {\n throw new Error('executor already has a running job');\n }\n this.#runningJob = info;\n this.#proc!.send({ case: 'startJobRequest', value: { runningJob: info } });\n }\n}\n"],"mappings":"AAIA,SAAS,YAAY;AAErB,SAAS,KAAK,qBAAqB;AACnC,SAAS,cAAc;AAEvB,SAAS,mBAAmB;AAGrB,MAAM,wBAAwB,YAAY;AAAA,EAC/C;AAAA,EACA,WAAW;AAAA,EACX,WAAW;AAAA,EACX,cAA+B;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA,QAAQ,IAAI,OAAO;AAAA,EACnB,QAAQ,IAAI,OAAO;AAAA,EACnB,UAAU,IAAI,EAAE,MAAM,EAAE,YAAY,KAAK,YAAY,CAAC;AAAA,EAEtD,YAAY,OAAe,mBAA2B,cAAsB;AAC1E,UAAM;AACN,SAAK,QAAQ;AAAA,MACX;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA,EAEA,IAAI,UAAmB;AACrB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,aAAyC;AAC3C,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,MAAM,QAAQ;AACZ,QAAI,KAAK,UAAU;AACjB,YAAM,IAAI,MAAM,wBAAwB;AAAA,IAC1C,WAAW,KAAK,UAAU;AACxB,YAAM,IAAI,MAAM,kBAAkB;AAAA,IACpC;AAEA,SAAK,QAAQ,MAAM,OAAO,eAAe,EAAE;AAAA,MAAK,CAAC,MAC/C,EAAE,WAAW;AAAA,QACX,WAAW,KAAK,MAAM;AAAA,MACxB,CAAC;AAAA,IACH;AAEA,SAAK,WAAW;AAChB,SAAK,IAAI;AAAA,EACX;AAAA,EAEA,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AAO,KAAK,EAAE,MAAM,mBAAmB,OAAO,EAAE,YAAY,KAAK,EAAE,CAAC;AAAA,EAC3E;AACF;","names":[]}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var proc_pool_exports = {};
|
|
20
|
+
__export(proc_pool_exports, {
|
|
21
|
+
ProcPool: () => ProcPool
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(proc_pool_exports);
|
|
24
|
+
var import_mutex = require("@livekit/mutex");
|
|
25
|
+
var import_utils = require("../utils.cjs");
|
|
26
|
+
var import_proc_job_executor = require("./proc_job_executor.cjs");
|
|
27
|
+
class ProcPool {
|
|
28
|
+
agent;
|
|
29
|
+
initializeTimeout;
|
|
30
|
+
closeTimeout;
|
|
31
|
+
executors = [];
|
|
32
|
+
tasks = [];
|
|
33
|
+
started = false;
|
|
34
|
+
closed = false;
|
|
35
|
+
controller = new AbortController();
|
|
36
|
+
initMutex = new import_mutex.Mutex();
|
|
37
|
+
procMutex;
|
|
38
|
+
procUnlock;
|
|
39
|
+
warmedProcQueue = new import_utils.Queue();
|
|
40
|
+
constructor(agent, numIdleProcesses, initializeTimeout, closeTimeout) {
|
|
41
|
+
this.agent = agent;
|
|
42
|
+
if (numIdleProcesses > 0) {
|
|
43
|
+
this.procMutex = new import_mutex.MultiMutex(numIdleProcesses);
|
|
44
|
+
}
|
|
45
|
+
this.initializeTimeout = initializeTimeout;
|
|
46
|
+
this.closeTimeout = closeTimeout;
|
|
47
|
+
}
|
|
48
|
+
get processes() {
|
|
49
|
+
return this.executors;
|
|
50
|
+
}
|
|
51
|
+
getByJobId(id) {
|
|
52
|
+
return this.executors.find((x) => x.runningJob && x.runningJob.job.id === id) || null;
|
|
53
|
+
}
|
|
54
|
+
async launchJob(info) {
|
|
55
|
+
let proc;
|
|
56
|
+
if (this.procMutex) {
|
|
57
|
+
proc = await this.warmedProcQueue.get();
|
|
58
|
+
if (this.procUnlock) {
|
|
59
|
+
this.procUnlock();
|
|
60
|
+
this.procUnlock = void 0;
|
|
61
|
+
}
|
|
62
|
+
} else {
|
|
63
|
+
proc = new import_proc_job_executor.ProcJobExecutor(this.agent, this.initializeTimeout, this.closeTimeout);
|
|
64
|
+
this.executors.push(proc);
|
|
65
|
+
await proc.start();
|
|
66
|
+
await proc.initialize();
|
|
67
|
+
}
|
|
68
|
+
await proc.launchJob(info);
|
|
69
|
+
}
|
|
70
|
+
async procWatchTask() {
|
|
71
|
+
const proc = new import_proc_job_executor.ProcJobExecutor(this.agent, this.initializeTimeout, this.closeTimeout);
|
|
72
|
+
try {
|
|
73
|
+
this.executors.push(proc);
|
|
74
|
+
const unlock = await this.initMutex.lock();
|
|
75
|
+
if (this.closed) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
await proc.start();
|
|
79
|
+
try {
|
|
80
|
+
await proc.initialize();
|
|
81
|
+
await this.warmedProcQueue.put(proc);
|
|
82
|
+
} catch {
|
|
83
|
+
if (this.procUnlock) {
|
|
84
|
+
this.procUnlock();
|
|
85
|
+
this.procUnlock = void 0;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
unlock();
|
|
89
|
+
await proc.join();
|
|
90
|
+
} finally {
|
|
91
|
+
this.executors.splice(this.executors.indexOf(proc));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
start() {
|
|
95
|
+
if (this.started) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
this.started = true;
|
|
99
|
+
this.run(this.controller.signal);
|
|
100
|
+
}
|
|
101
|
+
async run(signal) {
|
|
102
|
+
if (this.procMutex) {
|
|
103
|
+
while (!signal.aborted) {
|
|
104
|
+
this.procUnlock = await this.procMutex.lock();
|
|
105
|
+
const task = this.procWatchTask();
|
|
106
|
+
this.tasks.push(task);
|
|
107
|
+
task.finally(() => this.tasks.splice(this.tasks.indexOf(task)));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async close() {
|
|
112
|
+
if (!this.started) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
this.closed = true;
|
|
116
|
+
this.controller.abort();
|
|
117
|
+
this.warmedProcQueue.items.forEach((e) => e.close());
|
|
118
|
+
this.executors.forEach((e) => e.close());
|
|
119
|
+
await Promise.allSettled(this.tasks);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
123
|
+
0 && (module.exports = {
|
|
124
|
+
ProcPool
|
|
125
|
+
});
|
|
126
|
+
//# sourceMappingURL=proc_pool.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/ipc/proc_pool.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { MultiMutex, Mutex } from '@livekit/mutex';\nimport type { RunningJobInfo } from '../job.js';\nimport { Queue } from '../utils.js';\nimport type { JobExecutor } from './job_executor.js';\nimport { ProcJobExecutor } from './proc_job_executor.js';\n\nexport class ProcPool {\n agent: string;\n initializeTimeout: number;\n closeTimeout: number;\n executors: JobExecutor[] = [];\n tasks: Promise<void>[] = [];\n started = false;\n closed = false;\n controller = new AbortController();\n initMutex = new Mutex();\n procMutex?: MultiMutex;\n procUnlock?: () => void;\n warmedProcQueue = new Queue<JobExecutor>();\n\n constructor(\n agent: string,\n numIdleProcesses: number,\n initializeTimeout: number,\n closeTimeout: number,\n ) {\n this.agent = agent;\n if (numIdleProcesses > 0) {\n this.procMutex = new MultiMutex(numIdleProcesses);\n }\n this.initializeTimeout = initializeTimeout;\n this.closeTimeout = closeTimeout;\n }\n\n get processes(): JobExecutor[] {\n return this.executors;\n }\n\n getByJobId(id: string): JobExecutor | null {\n return this.executors.find((x) => x.runningJob && x.runningJob.job.id === id) || null;\n }\n\n async launchJob(info: RunningJobInfo) {\n let proc: JobExecutor;\n if (this.procMutex) {\n proc = await this.warmedProcQueue.get();\n if (this.procUnlock) {\n this.procUnlock();\n this.procUnlock = undefined;\n }\n } else {\n proc = new ProcJobExecutor(this.agent, this.initializeTimeout, this.closeTimeout);\n this.executors.push(proc);\n await proc.start();\n await proc.initialize();\n }\n await proc.launchJob(info);\n }\n\n async procWatchTask() {\n const proc = new ProcJobExecutor(this.agent, this.initializeTimeout, this.closeTimeout);\n\n try {\n this.executors.push(proc);\n\n const unlock = await this.initMutex.lock();\n if (this.closed) {\n return;\n }\n\n await proc.start();\n try {\n await proc.initialize();\n await this.warmedProcQueue.put(proc);\n } catch {\n if (this.procUnlock) {\n this.procUnlock();\n this.procUnlock = undefined;\n }\n }\n\n unlock();\n await proc.join();\n } finally {\n this.executors.splice(this.executors.indexOf(proc));\n }\n }\n\n start() {\n if (this.started) {\n return;\n }\n\n this.started = true;\n this.run(this.controller.signal);\n }\n\n async run(signal: AbortSignal) {\n if (this.procMutex) {\n while (!signal.aborted) {\n this.procUnlock = await this.procMutex.lock();\n const task = this.procWatchTask();\n this.tasks.push(task);\n task.finally(() => this.tasks.splice(this.tasks.indexOf(task)));\n }\n }\n }\n\n async close() {\n if (!this.started) {\n return;\n }\n this.closed = true;\n this.controller.abort();\n this.warmedProcQueue.items.forEach((e) => e.close());\n this.executors.forEach((e) => e.close());\n await Promise.allSettled(this.tasks);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,mBAAkC;AAElC,mBAAsB;AAEtB,+BAAgC;AAEzB,MAAM,SAAS;AAAA,EACpB;AAAA,EACA;AAAA,EACA;AAAA,EACA,YAA2B,CAAC;AAAA,EAC5B,QAAyB,CAAC;AAAA,EAC1B,UAAU;AAAA,EACV,SAAS;AAAA,EACT,aAAa,IAAI,gBAAgB;AAAA,EACjC,YAAY,IAAI,mBAAM;AAAA,EACtB;AAAA,EACA;AAAA,EACA,kBAAkB,IAAI,mBAAmB;AAAA,EAEzC,YACE,OACA,kBACA,mBACA,cACA;AACA,SAAK,QAAQ;AACb,QAAI,mBAAmB,GAAG;AACxB,WAAK,YAAY,IAAI,wBAAW,gBAAgB;AAAA,IAClD;AACA,SAAK,oBAAoB;AACzB,SAAK,eAAe;AAAA,EACtB;AAAA,EAEA,IAAI,YAA2B;AAC7B,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,WAAW,IAAgC;AACzC,WAAO,KAAK,UAAU,KAAK,CAAC,MAAM,EAAE,cAAc,EAAE,WAAW,IAAI,OAAO,EAAE,KAAK;AAAA,EACnF;AAAA,EAEA,MAAM,UAAU,MAAsB;AACpC,QAAI;AACJ,QAAI,KAAK,WAAW;AAClB,aAAO,MAAM,KAAK,gBAAgB,IAAI;AACtC,UAAI,KAAK,YAAY;AACnB,aAAK,WAAW;AAChB,aAAK,aAAa;AAAA,MACpB;AAAA,IACF,OAAO;AACL,aAAO,IAAI,yCAAgB,KAAK,OAAO,KAAK,mBAAmB,KAAK,YAAY;AAChF,WAAK,UAAU,KAAK,IAAI;AACxB,YAAM,KAAK,MAAM;AACjB,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