-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathproc_pool.ts
More file actions
165 lines (151 loc) · 4.12 KB
/
Copy pathproc_pool.ts
File metadata and controls
165 lines (151 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { MultiMutex, Mutex } from '@livekit/mutex';
import type { Throws } from '@livekit/throws-transformer/throws';
import type { RunningJobInfo } from '../job.js';
import { Queue } from '../utils.js';
import type { InferenceExecutor } from './inference_executor.js';
import type { JobExecutor } from './job_executor.js';
import { JobProcExecutor } from './job_proc_executor.js';
export class ProcPool {
agent: string;
initializeTimeout: number;
closeTimeout: number;
executors: JobExecutor[] = [];
tasks: Promise<void>[] = [];
started = false;
closed = false;
controller = new AbortController();
initMutex = new Mutex();
procMutex?: MultiMutex;
procUnlock?: () => void;
warmedProcQueue = new Queue<JobExecutor>();
inferenceExecutor?: InferenceExecutor;
memoryWarnMB: number;
memoryLimitMB: number;
constructor(
agent: string,
numIdleProcesses: number,
initializeTimeout: number,
closeTimeout: number,
inferenceExecutor: InferenceExecutor | undefined,
memoryWarnMB: number,
memoryLimitMB: number,
) {
this.agent = agent;
if (numIdleProcesses > 0) {
this.procMutex = new MultiMutex(numIdleProcesses);
}
this.initializeTimeout = initializeTimeout;
this.closeTimeout = closeTimeout;
this.inferenceExecutor = inferenceExecutor;
this.memoryWarnMB = memoryWarnMB;
this.memoryLimitMB = memoryLimitMB;
}
get processes(): JobExecutor[] {
return this.executors;
}
getByJobId(id: string): JobExecutor | null {
return this.executors.find((x) => x.runningJob && x.runningJob.job.id === id) || null;
}
async launchJob(info: RunningJobInfo): Promise<Throws<void, Error>> {
let proc: JobExecutor;
if (this.procMutex) {
proc = await this.warmedProcQueue.get();
if (this.procUnlock) {
this.procUnlock();
this.procUnlock = undefined;
}
} else {
proc = new JobProcExecutor(
this.agent,
this.inferenceExecutor,
this.initializeTimeout,
this.closeTimeout,
this.memoryWarnMB,
this.memoryLimitMB,
2500,
60000,
500,
);
this.executors.push(proc);
await proc.start();
await proc.initialize();
}
await proc.launchJob(info);
}
async procWatchTask() {
const proc = new JobProcExecutor(
this.agent,
this.inferenceExecutor,
this.initializeTimeout,
this.closeTimeout,
this.memoryWarnMB,
this.memoryLimitMB,
2500,
60000,
500,
);
try {
this.executors.push(proc);
const unlock = await this.initMutex.lock();
if (this.closed) {
return;
}
await proc.start();
try {
await proc.initialize();
await this.warmedProcQueue.put(proc);
} catch {
if (this.procUnlock) {
this.procUnlock();
this.procUnlock = undefined;
}
}
unlock();
await proc.join();
} finally {
const procIndex = this.executors.indexOf(proc);
if (procIndex !== -1) {
this.executors.splice(procIndex, 1);
} else {
throw new Error(`proc ${proc} not found in executors`);
}
}
}
start() {
if (this.started) {
return;
}
this.started = true;
this.run(this.controller.signal);
}
async run(signal: AbortSignal) {
if (this.procMutex) {
while (!signal.aborted) {
this.procUnlock = await this.procMutex.lock();
const task = this.procWatchTask();
this.tasks.push(task);
task.finally(() => {
const taskIndex = this.tasks.indexOf(task);
if (taskIndex !== -1) {
this.tasks.splice(taskIndex, 1);
} else {
throw new Error(`task ${task} not found in tasks`);
}
});
}
}
}
async close() {
if (!this.started) {
return;
}
this.closed = true;
this.controller.abort();
this.warmedProcQueue.items.forEach((e) => e.close());
this.executors.forEach((e) => e.close());
await Promise.allSettled(this.tasks);
}
}