@actsecurity/job 0.1.63
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.txt +21 -0
- package/README.md +9 -0
- package/dist/cjs/ConcurrentJobQueue.d.ts +65 -0
- package/dist/cjs/ConcurrentJobQueue.d.ts.map +1 -0
- package/dist/cjs/ConcurrentJobQueue.js +160 -0
- package/dist/cjs/ConcurrentJobQueue.js.map +1 -0
- package/dist/cjs/ConcurrentWorkerPool.d.ts +64 -0
- package/dist/cjs/ConcurrentWorkerPool.d.ts.map +1 -0
- package/dist/cjs/ConcurrentWorkerPool.js +166 -0
- package/dist/cjs/ConcurrentWorkerPool.js.map +1 -0
- package/dist/cjs/StreamingJobQueue.d.ts +63 -0
- package/dist/cjs/StreamingJobQueue.d.ts.map +1 -0
- package/dist/cjs/StreamingJobQueue.js +157 -0
- package/dist/cjs/StreamingJobQueue.js.map +1 -0
- package/dist/cjs/index.d.ts +7 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +14 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/job.d.ts +36 -0
- package/dist/cjs/job.d.ts.map +1 -0
- package/dist/cjs/job.js +3 -0
- package/dist/cjs/job.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/runJobs.d.ts +13 -0
- package/dist/cjs/runJobs.d.ts.map +1 -0
- package/dist/cjs/runJobs.js +53 -0
- package/dist/cjs/runJobs.js.map +1 -0
- package/dist/cjs/util.d.ts +7 -0
- package/dist/cjs/util.d.ts.map +1 -0
- package/dist/cjs/util.js +13 -0
- package/dist/cjs/util.js.map +1 -0
- package/dist/esm/ConcurrentJobQueue.d.ts +65 -0
- package/dist/esm/ConcurrentJobQueue.d.ts.map +1 -0
- package/dist/esm/ConcurrentJobQueue.js +155 -0
- package/dist/esm/ConcurrentJobQueue.js.map +1 -0
- package/dist/esm/ConcurrentWorkerPool.d.ts +64 -0
- package/dist/esm/ConcurrentWorkerPool.d.ts.map +1 -0
- package/dist/esm/ConcurrentWorkerPool.js +161 -0
- package/dist/esm/ConcurrentWorkerPool.js.map +1 -0
- package/dist/esm/StreamingJobQueue.d.ts +63 -0
- package/dist/esm/StreamingJobQueue.d.ts.map +1 -0
- package/dist/esm/StreamingJobQueue.js +152 -0
- package/dist/esm/StreamingJobQueue.js.map +1 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/job.d.ts +36 -0
- package/dist/esm/job.d.ts.map +1 -0
- package/dist/esm/job.js +2 -0
- package/dist/esm/job.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/runJobs.d.ts +13 -0
- package/dist/esm/runJobs.d.ts.map +1 -0
- package/dist/esm/runJobs.js +51 -0
- package/dist/esm/runJobs.js.map +1 -0
- package/dist/esm/util.d.ts +7 -0
- package/dist/esm/util.d.ts.map +1 -0
- package/dist/esm/util.js +10 -0
- package/dist/esm/util.js.map +1 -0
- package/package.json +112 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Cloud Copilot
|
|
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/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Job
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@cloud-copilot/job) [](LICENSE.txt) [](https://github.com/cloud-copilot/job/actions/workflows/guarddog.yml) [](https://snyk.io/test/github/cloud-copilot/job?targetFile=package.json)
|
|
4
|
+
|
|
5
|
+
Functions and classes for running node tasks in parallel while controlling concurrency.
|
|
6
|
+
|
|
7
|
+
## License
|
|
8
|
+
|
|
9
|
+
MIT
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { type Logger } from '@cloud-copilot/log';
|
|
2
|
+
import { type Job, type JobResult } from './job.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a queue that runs jobs concurrently up to a specified limit.
|
|
5
|
+
* This will wait for jobs to be added to it and run them up the
|
|
6
|
+
* maximum concurrency.
|
|
7
|
+
*
|
|
8
|
+
* Results are available via `getResults()`.
|
|
9
|
+
*/
|
|
10
|
+
export declare class ConcurrentJobQueue<T = void, P = Record<string, unknown>> {
|
|
11
|
+
private concurrency;
|
|
12
|
+
private logger;
|
|
13
|
+
private queue;
|
|
14
|
+
private results;
|
|
15
|
+
private activeJobs;
|
|
16
|
+
private waitingResolvers;
|
|
17
|
+
private workers;
|
|
18
|
+
private isAcceptingWork;
|
|
19
|
+
private workAvailablePromise;
|
|
20
|
+
private resolveWorkAvailable;
|
|
21
|
+
/**
|
|
22
|
+
* Create a new runner with the specified concurrency.
|
|
23
|
+
*
|
|
24
|
+
* @param concurrency - The maximum number of jobs to run concurrently.
|
|
25
|
+
* @param logger - Optional logger instance for long-running job warnings. Defaults to the module-level logger from @cloud-copilot/log.
|
|
26
|
+
*/
|
|
27
|
+
constructor(concurrency: number, logger?: Logger);
|
|
28
|
+
private worker;
|
|
29
|
+
private waitForWorkAvailable;
|
|
30
|
+
private ensureWorkers;
|
|
31
|
+
private notifyWorkersOfNewWork;
|
|
32
|
+
private checkIfIdle;
|
|
33
|
+
/**
|
|
34
|
+
* Add a job to the queue
|
|
35
|
+
*/
|
|
36
|
+
enqueue(job: Job<T, P>): void;
|
|
37
|
+
/**
|
|
38
|
+
* Add multiple jobs to the queue
|
|
39
|
+
*/
|
|
40
|
+
enqueueAll(jobs: Job<T, P>[]): void;
|
|
41
|
+
/**
|
|
42
|
+
* Returns a promise that resolves when all queued work is complete
|
|
43
|
+
*/
|
|
44
|
+
waitForIdle(): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Get all results accumulated so far
|
|
47
|
+
*/
|
|
48
|
+
getResults(): JobResult<T, P>[];
|
|
49
|
+
/**
|
|
50
|
+
* Shutdown the queue - no new jobs will be accepted, but existing jobs will complete.
|
|
51
|
+
*
|
|
52
|
+
* Returns when a promise that resolves when all jobs have been processed and
|
|
53
|
+
* are available in `getResults()`.
|
|
54
|
+
*/
|
|
55
|
+
finishAllWork(): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Get the current queue length
|
|
58
|
+
*/
|
|
59
|
+
get queueLength(): number;
|
|
60
|
+
/**
|
|
61
|
+
* Get the number of currently active jobs
|
|
62
|
+
*/
|
|
63
|
+
get activeJobCount(): number;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=ConcurrentJobQueue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConcurrentJobQueue.d.ts","sourceRoot":"","sources":["../../src/Con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IAAI,MAAM,CAE3B;CACF"}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConcurrentJobQueue = void 0;
|
|
4
|
+
const log_1 = require("@cloud-copilot/log");
|
|
5
|
+
/**
|
|
6
|
+
* Creates a queue that runs jobs concurrently up to a specified limit.
|
|
7
|
+
* This will wait for jobs to be added to it and run them up the
|
|
8
|
+
* maximum concurrency.
|
|
9
|
+
*
|
|
10
|
+
* Results are available via `getResults()`.
|
|
11
|
+
*/
|
|
12
|
+
class ConcurrentJobQueue {
|
|
13
|
+
concurrency;
|
|
14
|
+
logger;
|
|
15
|
+
queue = [];
|
|
16
|
+
results = [];
|
|
17
|
+
activeJobs = 0;
|
|
18
|
+
waitingResolvers = [];
|
|
19
|
+
workers = [];
|
|
20
|
+
isAcceptingWork = true;
|
|
21
|
+
workAvailablePromise = null;
|
|
22
|
+
resolveWorkAvailable = null;
|
|
23
|
+
/**
|
|
24
|
+
* Create a new runner with the specified concurrency.
|
|
25
|
+
*
|
|
26
|
+
* @param concurrency - The maximum number of jobs to run concurrently.
|
|
27
|
+
* @param logger - Optional logger instance for long-running job warnings. Defaults to the module-level logger from @cloud-copilot/log.
|
|
28
|
+
*/
|
|
29
|
+
constructor(concurrency, logger = log_1.log) {
|
|
30
|
+
this.concurrency = concurrency;
|
|
31
|
+
this.logger = logger;
|
|
32
|
+
}
|
|
33
|
+
async worker(workerId) {
|
|
34
|
+
while (this.isAcceptingWork || this.queue.length > 0) {
|
|
35
|
+
const job = this.queue.shift();
|
|
36
|
+
if (!job) {
|
|
37
|
+
if (!this.isAcceptingWork) {
|
|
38
|
+
// No longer accepting work and no jobs left, exit immediately
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
// No work available, wait for new work to be added
|
|
42
|
+
await this.waitForWorkAvailable();
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
this.activeJobs++;
|
|
46
|
+
const context = { workerId };
|
|
47
|
+
const startTime = Date.now();
|
|
48
|
+
const interval = setInterval(() => {
|
|
49
|
+
this.logger.warn(`Long-running job detected.`, { minutes: Math.floor((Date.now() - startTime) / 60000) }, { ...context, ...job.properties });
|
|
50
|
+
}, 60_000);
|
|
51
|
+
try {
|
|
52
|
+
const value = await job.execute({ ...context, properties: job.properties });
|
|
53
|
+
this.results.push({ status: 'fulfilled', value, properties: job.properties });
|
|
54
|
+
}
|
|
55
|
+
catch (reason) {
|
|
56
|
+
this.results.push({ status: 'rejected', reason, properties: job.properties });
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
clearInterval(interval);
|
|
60
|
+
this.activeJobs--;
|
|
61
|
+
this.checkIfIdle();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
waitForWorkAvailable() {
|
|
66
|
+
if (!this.workAvailablePromise) {
|
|
67
|
+
this.workAvailablePromise = new Promise((resolve) => {
|
|
68
|
+
this.resolveWorkAvailable = resolve;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return this.workAvailablePromise;
|
|
72
|
+
}
|
|
73
|
+
ensureWorkers() {
|
|
74
|
+
if (this.workers.length === 0 && this.isAcceptingWork) {
|
|
75
|
+
for (let i = 0; i < this.concurrency; i++) {
|
|
76
|
+
this.workers.push(this.worker(i + 1));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
notifyWorkersOfNewWork() {
|
|
81
|
+
// Wake up waiting workers
|
|
82
|
+
if (this.resolveWorkAvailable) {
|
|
83
|
+
this.resolveWorkAvailable();
|
|
84
|
+
this.workAvailablePromise = null;
|
|
85
|
+
this.resolveWorkAvailable = null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
checkIfIdle() {
|
|
89
|
+
if (this.activeJobs === 0 && this.queue.length === 0) {
|
|
90
|
+
// Notify all waiting resolvers
|
|
91
|
+
this.waitingResolvers.forEach((resolve) => resolve());
|
|
92
|
+
this.waitingResolvers = [];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Add a job to the queue
|
|
97
|
+
*/
|
|
98
|
+
enqueue(job) {
|
|
99
|
+
if (!this.isAcceptingWork) {
|
|
100
|
+
throw new Error('Cannot enqueue jobs after shutdown');
|
|
101
|
+
}
|
|
102
|
+
this.queue.push(job);
|
|
103
|
+
this.ensureWorkers();
|
|
104
|
+
this.notifyWorkersOfNewWork();
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Add multiple jobs to the queue
|
|
108
|
+
*/
|
|
109
|
+
enqueueAll(jobs) {
|
|
110
|
+
jobs.forEach((job) => this.enqueue(job));
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Returns a promise that resolves when all queued work is complete
|
|
114
|
+
*/
|
|
115
|
+
waitForIdle() {
|
|
116
|
+
// log.debug('waitForIdle called', this.activeJobs, this.queue.length)
|
|
117
|
+
return new Promise((resolve) => {
|
|
118
|
+
if (this.activeJobs === 0 && this.queue.length === 0) {
|
|
119
|
+
resolve();
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
this.waitingResolvers.push(resolve);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Get all results accumulated so far
|
|
128
|
+
*/
|
|
129
|
+
getResults() {
|
|
130
|
+
return this.results;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Shutdown the queue - no new jobs will be accepted, but existing jobs will complete.
|
|
134
|
+
*
|
|
135
|
+
* Returns when a promise that resolves when all jobs have been processed and
|
|
136
|
+
* are available in `getResults()`.
|
|
137
|
+
*/
|
|
138
|
+
async finishAllWork() {
|
|
139
|
+
this.isAcceptingWork = false;
|
|
140
|
+
// Wake up any sleeping workers so they can process remaining jobs or exit
|
|
141
|
+
this.notifyWorkersOfNewWork();
|
|
142
|
+
// Check if we're already idle and notify any waiting resolvers
|
|
143
|
+
await Promise.all(this.workers);
|
|
144
|
+
this.workers = [];
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Get the current queue length
|
|
148
|
+
*/
|
|
149
|
+
get queueLength() {
|
|
150
|
+
return this.queue.length;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Get the number of currently active jobs
|
|
154
|
+
*/
|
|
155
|
+
get activeJobCount() {
|
|
156
|
+
return this.activeJobs;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
exports.ConcurrentJobQueue = ConcurrentJobQueue;
|
|
160
|
+
//# sourceMappingURL=ConcurrentJobQueue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConcurrentJobQueue.js","sourceRoot":"","sources":["../../src/ConcurrentJobQueue.ts"],"names":[],"mappings":";;;AAAA,4CAAsE;AAGtE;;;;;;GAMG;AAEH,MAAa,kBAAkB;IAiBnB;IACA;IAjBF,KAAK,GAAgB,EAAE,CAAA;IACvB,OAAO,GAAsB,EAAE,CAAA;IAC/B,UAAU,GAAG,CAAC,CAAA;IACd,gBAAgB,GAAmB,EAAE,CAAA;IACrC,OAAO,GAAoB,EAAE,CAAA;IAC7B,eAAe,GAAG,IAAI,CAAA;IACtB,oBAAoB,GAAyB,IAAI,CAAA;IACjD,oBAAoB,GAAwB,IAAI,CAAA;IAExD;;;;;OAKG;IACH,YACU,WAAmB,EACnB,SAAiB,SAAa;QAD9B,gBAAW,GAAX,WAAW,CAAQ;QACnB,WAAM,GAAN,MAAM,CAAwB;IACrC,CAAC;IAEI,KAAK,CAAC,MAAM,CAAC,QAAgB;QACnC,OAAO,IAAI,CAAC,eAAe,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACrD,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;YAC9B,IAAI,CAAC,GAAG,EAAE,CAAC;gBACT,IAAI,CAAC,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AA;IACxB,CAAC;CACF;AAjKD,gDAiKC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { type Logger } from '@cloud-copilot/log';
|
|
2
|
+
import { type Job, type JobResult } from './job.js';
|
|
3
|
+
/**
|
|
4
|
+
* A worker pool that will run jobs concurrently using promises up to a specified limit.
|
|
5
|
+
* If no work is being done, it will wait for jobs to be added and run them up to the
|
|
6
|
+
* maximum concurrency.
|
|
7
|
+
*
|
|
8
|
+
* This is designed to create a central queue for jobs of many types to be processed
|
|
9
|
+
* in a streaming fashion, allowing for efficient resource usage and managed network requests.
|
|
10
|
+
*
|
|
11
|
+
*/
|
|
12
|
+
export declare class ConcurrentWorkerPool<T = void, P = Record<string, unknown>> {
|
|
13
|
+
private concurrency;
|
|
14
|
+
private logger;
|
|
15
|
+
private jobCounter;
|
|
16
|
+
private resolveMap;
|
|
17
|
+
private queue;
|
|
18
|
+
private activeJobs;
|
|
19
|
+
private waitingResolvers;
|
|
20
|
+
private workers;
|
|
21
|
+
private isAcceptingWork;
|
|
22
|
+
private workAvailablePromise;
|
|
23
|
+
private resolveWorkAvailable;
|
|
24
|
+
/**
|
|
25
|
+
* Create a new runner with the specified concurrency.
|
|
26
|
+
*
|
|
27
|
+
* @param concurrency - The maximum number of jobs to run concurrently.
|
|
28
|
+
* @param logger - Optional logger instance for long-running job warnings. Defaults to the module-level logger from @cloud-copilot/log.
|
|
29
|
+
*/
|
|
30
|
+
constructor(concurrency: number, logger?: Logger);
|
|
31
|
+
private worker;
|
|
32
|
+
private waitForWorkAvailable;
|
|
33
|
+
private ensureWorkers;
|
|
34
|
+
private notifyWorkersOfNewWork;
|
|
35
|
+
private checkIfIdle;
|
|
36
|
+
/**
|
|
37
|
+
* Add a job to the queue
|
|
38
|
+
*/
|
|
39
|
+
enqueue(job: Job<T, P>): Promise<JobResult<T, P>>;
|
|
40
|
+
/**
|
|
41
|
+
* Add multiple jobs to the queue
|
|
42
|
+
*/
|
|
43
|
+
enqueueAll(jobs: Job<T, P>[]): Promise<JobResult<T, P>>[];
|
|
44
|
+
/**
|
|
45
|
+
* Returns a promise that resolves when all queued work is complete
|
|
46
|
+
*/
|
|
47
|
+
waitForIdle(): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Shutdown the queue - no new jobs will be accepted, but existing jobs will complete.
|
|
50
|
+
*
|
|
51
|
+
* Returns when a promise that resolves when all jobs have been processed and
|
|
52
|
+
* the onComplete callback has been called for each job.
|
|
53
|
+
*/
|
|
54
|
+
finishAllWork(): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Get the current queue length
|
|
57
|
+
*/
|
|
58
|
+
get queueLength(): number;
|
|
59
|
+
/**
|
|
60
|
+
* Get the number of currently active jobs
|
|
61
|
+
*/
|
|
62
|
+
get activeJobCount(): number;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=ConcurrentWorkerPool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConcurrentWorkerPool.d.ts","sourceRoot":"","sources":["../../src/ConcurrentWorkerPool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAA;AACtE,OAAO,EAAE,KAAK,GAAG,EAAmB,KAAK,SAAS,EAAE,MAAM,UAAU,CAAA;AAMpE;;;;;;;;GAQG;AACH,qBAAa,oBAAoB,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAkBnE,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,MAAM;IAlBhB,OAAO,CAAC,UAAU,CAAI;IACtB,OAAO,CAAC,UAAU,CAAuD;IACzE,OAAO,CAAC,KAAK,CAAwB;IACrC,OAAO,CAAC,UAAU,CAAI;IACtB,OAAO,CAAC,gBAAgB,CAAqB;IAC7C,OAAO,CAAC,OAAO,CAAsB;IACrC,OAAO,CAAC,eAAe,CAAO;IAC9B,OAAO,CAAC,oBAAoB,CAA6B;IACzD,OAAO,CAAC,oBAAoB,CAA4B;IAExD;;;;;OAKG;gBAEO,WAAW,EAAE,MAAM,EACnB,MAAM,GAAE,MAAsB;YAG1B,MAAM;IAwCpB,OAAO,CAAC,oBAAoB;IAS5B,OAAO,CAAC,aAAa;IAQrB,OAAO,CAAC,sBAAsB;IAS9B,OAAO,CAAC,WAAW;IAQnB;;OAEG;IACH,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAmBjD;;OAEG;IACH,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE;IAIzD;;OAEG;IACH,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAU5B;;;;;OAKG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IASpC;;OAEG;IACH,IAAI,WAAW,IAAI,MAAM,CAExB;IAED;;OAEG;IACH,IAAI,cAAc,IAAI,MAAM,CAE3B;CACF"}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConcurrentWorkerPool = void 0;
|
|
4
|
+
const log_1 = require("@cloud-copilot/log");
|
|
5
|
+
/**
|
|
6
|
+
* A worker pool that will run jobs concurrently using promises up to a specified limit.
|
|
7
|
+
* If no work is being done, it will wait for jobs to be added and run them up to the
|
|
8
|
+
* maximum concurrency.
|
|
9
|
+
*
|
|
10
|
+
* This is designed to create a central queue for jobs of many types to be processed
|
|
11
|
+
* in a streaming fashion, allowing for efficient resource usage and managed network requests.
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
class ConcurrentWorkerPool {
|
|
15
|
+
concurrency;
|
|
16
|
+
logger;
|
|
17
|
+
jobCounter = 0;
|
|
18
|
+
resolveMap = {};
|
|
19
|
+
queue = [];
|
|
20
|
+
activeJobs = 0;
|
|
21
|
+
waitingResolvers = [];
|
|
22
|
+
workers = [];
|
|
23
|
+
isAcceptingWork = true;
|
|
24
|
+
workAvailablePromise = null;
|
|
25
|
+
resolveWorkAvailable = null;
|
|
26
|
+
/**
|
|
27
|
+
* Create a new runner with the specified concurrency.
|
|
28
|
+
*
|
|
29
|
+
* @param concurrency - The maximum number of jobs to run concurrently.
|
|
30
|
+
* @param logger - Optional logger instance for long-running job warnings. Defaults to the module-level logger from @cloud-copilot/log.
|
|
31
|
+
*/
|
|
32
|
+
constructor(concurrency, logger = log_1.log) {
|
|
33
|
+
this.concurrency = concurrency;
|
|
34
|
+
this.logger = logger;
|
|
35
|
+
}
|
|
36
|
+
async worker(workerId) {
|
|
37
|
+
while (this.isAcceptingWork || this.queue.length > 0) {
|
|
38
|
+
const job = this.queue.shift();
|
|
39
|
+
if (!job) {
|
|
40
|
+
if (!this.isAcceptingWork) {
|
|
41
|
+
// No longer accepting work and no jobs left, exit immediately
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
// No work available, wait for new work to be added
|
|
45
|
+
await this.waitForWorkAvailable();
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
this.activeJobs++;
|
|
49
|
+
const context = { workerId };
|
|
50
|
+
const startTime = Date.now();
|
|
51
|
+
const interval = setInterval(() => {
|
|
52
|
+
this.logger.warn(`Long-running job detected.`, { minutes: Math.floor((Date.now() - startTime) / 60000) }, { ...context, ...job.properties });
|
|
53
|
+
}, 60_000);
|
|
54
|
+
const resolve = this.resolveMap[job.concurrentJobId];
|
|
55
|
+
try {
|
|
56
|
+
const value = await job.execute({ ...context, properties: job.properties });
|
|
57
|
+
resolve({ status: 'fulfilled', value, properties: job.properties });
|
|
58
|
+
}
|
|
59
|
+
catch (reason) {
|
|
60
|
+
resolve({ status: 'rejected', reason, properties: job.properties });
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
clearInterval(interval);
|
|
64
|
+
this.activeJobs--;
|
|
65
|
+
this.checkIfIdle();
|
|
66
|
+
delete this.resolveMap[job.concurrentJobId];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
waitForWorkAvailable() {
|
|
71
|
+
if (!this.workAvailablePromise) {
|
|
72
|
+
this.workAvailablePromise = new Promise((resolve) => {
|
|
73
|
+
this.resolveWorkAvailable = resolve;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return this.workAvailablePromise;
|
|
77
|
+
}
|
|
78
|
+
ensureWorkers() {
|
|
79
|
+
if (this.workers.length === 0 && this.isAcceptingWork) {
|
|
80
|
+
for (let i = 0; i < this.concurrency; i++) {
|
|
81
|
+
this.workers.push(this.worker(i + 1));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
notifyWorkersOfNewWork() {
|
|
86
|
+
// Wake up waiting workers
|
|
87
|
+
if (this.resolveWorkAvailable) {
|
|
88
|
+
this.resolveWorkAvailable();
|
|
89
|
+
this.workAvailablePromise = null;
|
|
90
|
+
this.resolveWorkAvailable = null;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
checkIfIdle() {
|
|
94
|
+
if (this.activeJobs === 0 && this.queue.length === 0) {
|
|
95
|
+
// Notify all waiting resolvers
|
|
96
|
+
this.waitingResolvers.forEach((resolve) => resolve());
|
|
97
|
+
this.waitingResolvers = [];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Add a job to the queue
|
|
102
|
+
*/
|
|
103
|
+
enqueue(job) {
|
|
104
|
+
if (!this.isAcceptingWork) {
|
|
105
|
+
throw new Error('Cannot enqueue jobs after shutdown');
|
|
106
|
+
}
|
|
107
|
+
return new Promise((resolve) => {
|
|
108
|
+
const jobId = this.jobCounter++;
|
|
109
|
+
const jobWithId = {
|
|
110
|
+
...job,
|
|
111
|
+
concurrentJobId: jobId
|
|
112
|
+
};
|
|
113
|
+
this.resolveMap[jobId] = resolve;
|
|
114
|
+
this.queue.push(jobWithId);
|
|
115
|
+
this.ensureWorkers();
|
|
116
|
+
this.notifyWorkersOfNewWork();
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Add multiple jobs to the queue
|
|
121
|
+
*/
|
|
122
|
+
enqueueAll(jobs) {
|
|
123
|
+
return jobs.map((job) => this.enqueue(job));
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Returns a promise that resolves when all queued work is complete
|
|
127
|
+
*/
|
|
128
|
+
waitForIdle() {
|
|
129
|
+
return new Promise((resolve) => {
|
|
130
|
+
if (this.activeJobs === 0 && this.queue.length === 0) {
|
|
131
|
+
resolve();
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
this.waitingResolvers.push(resolve);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Shutdown the queue - no new jobs will be accepted, but existing jobs will complete.
|
|
140
|
+
*
|
|
141
|
+
* Returns when a promise that resolves when all jobs have been processed and
|
|
142
|
+
* the onComplete callback has been called for each job.
|
|
143
|
+
*/
|
|
144
|
+
async finishAllWork() {
|
|
145
|
+
this.isAcceptingWork = false;
|
|
146
|
+
// Wake up any sleeping workers so they can process remaining jobs or exit
|
|
147
|
+
this.notifyWorkersOfNewWork();
|
|
148
|
+
// Check if we're already idle and notify any waiting resolvers
|
|
149
|
+
await Promise.all(this.workers);
|
|
150
|
+
this.workers = [];
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Get the current queue length
|
|
154
|
+
*/
|
|
155
|
+
get queueLength() {
|
|
156
|
+
return this.queue.length;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Get the number of currently active jobs
|
|
160
|
+
*/
|
|
161
|
+
get activeJobCount() {
|
|
162
|
+
return this.activeJobs;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
exports.ConcurrentWorkerPool = ConcurrentWorkerPool;
|
|
166
|
+
//# sourceMappingURL=ConcurrentWorkerPool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConcurrentWorkerPool.js","sourceRoot":"","sources":["../../src/ConcurrentWorkerPool.ts"],"names":[],"mappings":";;;AAAA,4CAAsE;AAOtE;;;;;;;;GAQG;AACH,MAAa,oBAAoB;IAkBrB;IACA;IAlBF,UAAU,GAAG,CAAC,CAAA;IACd,UAAU,GAAqD,EAAE,CAAA;IACjE,KAAK,GAAsB,EAAE,CAAA;IAC7B,UAAU,GAAG,CAAC,CAAA;IACd,gBAAgB,GAAmB,EAAE,CAAA;IACrC,OAAO,GAAoB,EAAE,CAAA;IAC7B,eAAe,GAAG,IAAI,CAAA;IACtB,oBAAoB,GAAyB,IAAI,CAAA;IACjD,oBAAoB,GAAwB,IAAI,CAAA;IAExD;;;;;OAKG;IACH,YACU,WAAmB,EACnB,SAAiB,SAAa;QAD9B,gBAAW,GAAX,WAAW,CAAQ;QACnB,WAAM,GAAN,MAAM,CAAwB;IACrC,CAAC;IAEI,KAAK,CAAC,MAAM,CAAC,QAAgB;QACnC,OAAO,IAAI,CAAC,eAAe,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACrD,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;YAC9B,IAAI,CAAC,GAAG,EAAE,CAAC;gBACT,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE,CAAC;oBAC1B,8DAA8D;oBAC9D,OAAM;gBACR,CAAC;gBACD,mDAAmD;gBACnD,MAAM,IAAI,CAAC,oBAAoB,EAAE,CAAA;gBACjC,SAAQ;YACV,CAAC;YAED,IAAI,CAAC,UAAU,EAAE,CAAA;YACjB,MAAM,OAAO,GAAe,EAAE,QAAQ,EAAE,CAAA;YAExC,MAAM,SAAS,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c;QAChB,OAAO,IAAI,CAAC,UAAU,CAAA;IACxB,CAAC;CACF;AAtKD,oDAsKC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { type Logger } from '@cloud-copilot/log';
|
|
2
|
+
import { type Job, type JobResult } from './job.js';
|
|
3
|
+
/**
|
|
4
|
+
* A queue that will run jobs concurrently using promises up to a specified limit.
|
|
5
|
+
* If no work is being done, it will wait for jobs to be added and run them up to the
|
|
6
|
+
* maximum concurrency.
|
|
7
|
+
*
|
|
8
|
+
* Results are not stored in this queue, but are processed immediately via the `onComplete` callback.
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
export declare class StreamingJobQueue<T = void, P = Record<string, unknown>> {
|
|
12
|
+
private concurrency;
|
|
13
|
+
private logger;
|
|
14
|
+
private onComplete;
|
|
15
|
+
private queue;
|
|
16
|
+
private activeJobs;
|
|
17
|
+
private waitingResolvers;
|
|
18
|
+
private workers;
|
|
19
|
+
private isAcceptingWork;
|
|
20
|
+
private workAvailablePromise;
|
|
21
|
+
private resolveWorkAvailable;
|
|
22
|
+
/**
|
|
23
|
+
* Create a new runner with the specified concurrency.
|
|
24
|
+
*
|
|
25
|
+
* @param concurrency - The maximum number of jobs to run concurrently.
|
|
26
|
+
* @param logger - Logger instance for long-running job warnings.
|
|
27
|
+
* @param onComplete - Callback to handle job completion, receives the job result.
|
|
28
|
+
*/
|
|
29
|
+
constructor(concurrency: number, logger: Logger, onComplete: (response: JobResult<T, P>) => Promise<void>);
|
|
30
|
+
private worker;
|
|
31
|
+
private waitForWorkAvailable;
|
|
32
|
+
private ensureWorkers;
|
|
33
|
+
private notifyWorkersOfNewWork;
|
|
34
|
+
private checkIfIdle;
|
|
35
|
+
/**
|
|
36
|
+
* Add a job to the queue
|
|
37
|
+
*/
|
|
38
|
+
enqueue(job: Job<T, P>): void;
|
|
39
|
+
/**
|
|
40
|
+
* Add multiple jobs to the queue
|
|
41
|
+
*/
|
|
42
|
+
enqueueAll(jobs: Job<T, P>[]): void;
|
|
43
|
+
/**
|
|
44
|
+
* Returns a promise that resolves when all queued work is complete
|
|
45
|
+
*/
|
|
46
|
+
waitForIdle(): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Shutdown the queue - no new jobs will be accepted, but existing jobs will complete.
|
|
49
|
+
*
|
|
50
|
+
* Returns when a promise that resolves when all jobs have been processed and
|
|
51
|
+
* the onComplete callback has been called for each job.
|
|
52
|
+
*/
|
|
53
|
+
finishAllWork(): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Get the current queue length
|
|
56
|
+
*/
|
|
57
|
+
get queueLength(): number;
|
|
58
|
+
/**
|
|
59
|
+
* Get the number of currently active jobs
|
|
60
|
+
*/
|
|
61
|
+
get activeJobCount(): number;
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=StreamingJobQueue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StreamingJobQueue.d.ts","sourceRoot":"","sources":["../../src/StreamingJobQueue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAChD,OAAO,EAAE,KAAK,GAAG,EAAmB,KAAK,SAAS,EAAE,MAAM,UAAU,CAAA;AAEpE;;;;;;;GAOG;AACH,qBAAa,iBAAiB,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAiBhE,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,UAAU;IAlBpB,OAAO,CAAC,KAAK,CAAkB;IAC/B,OAAO,CAAC,UAAU,CAAI;IACtB,OAAO,CAAC,gBAAgB,CAAqB;IAC7C,OAAO,CAAC,OAAO,CAAsB;IACrC,OAAO,CAAC,eAAe,CAAO;IAC9B,OAAO,CAAC,oBAAoB,CAA6B;IACzD,OAAO,CAAC,oBAAoB,CAA4B;IAExD;;;;;;OAMG;gBAEO,WAAW,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