-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.ts
99 lines (81 loc) · 2.15 KB
/
worker.ts
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
import { promisify } from 'util'
const sleep = promisify(setTimeout)
export class PromiseQueue {
constructor(private ms: number = 0, private jobs: number | null = null) {
this.setWorkerConfig(ms, jobs)
}
setWorkerConfig(ms: number = 0, jobs: number | null = null) {
if (ms < 0) {
throw RangeError(`Invalid amount of milliseconds to wait: ${ms}`)
}
if (jobs != null && jobs <= 0) {
throw RangeError(`Invalida amount of jobs: ${jobs}`)
}
this.ms = ms
this.jobs = jobs
if (jobs) {
console.log(`[config]: Maximum job limit is ${jobs}`);
} else {
console.log('[config]: No maximum job limit');
}
}
async run <T>(worker: () => Promise<T>, desiredThreadCount: number) {
if (this.jobs != null) {
desiredThreadCount = Math.min(this.jobs, desiredThreadCount)
}
// Produce and run jobs
const jobs = []
for (let i = 0; i < desiredThreadCount; i++) {
jobs.push(worker())
}
return await Promise.all(jobs)
}
async workerDelay() {
await sleep(this.ms)
}
blockingQueue(tasks: (() => Promise<any>)[]) {
return async () => {
while (tasks.length != 0) {
await tasks.pop()!()
await this.workerDelay()
}
}
}
}
/**
* Creates blocking queue (executes tasks one by one)
*/
export function blockingQueue(tasks: (() => Promise<any>)[]) {
return async function() {
while (tasks.length != 0) {
await tasks.pop()!()
}
}
}
/**
* Executes worker in parallel, returns promise which returns only when all workers finish their work
*/
export function parallel<T>(worker: () => Promise<T>, desiredThreadCount: number) {
// Produce and run jobs
const jobs = []
for (let i = 0; i < desiredThreadCount; i++) {
jobs.push(worker())
}
return Promise.all(jobs)
}
/**
* Executes tasks in parallel, returns promise which returns only when all workers finish their work
*/
export function parallelQueue<T>(tasks: (() => Promise<T>)[], desiredThreadCount: number, errorHandler?: (err: any) => void) {
return parallel(async function() {
while (tasks.length != 0) {
try {
await tasks.pop()!()
} catch(err) {
if (errorHandler !== undefined) {
errorHandler(err)
}
}
}
}, desiredThreadCount)
}