this.executeTask = this.executeTask.bind(this)
this.enqueueTask = this.enqueueTask.bind(this)
this.dequeueTask = this.dequeueTask.bind(this)
- this.checkAndEmitEvents = this.checkAndEmitEvents.bind(this)
+ this.checkAndEmitTaskExecutionEvents =
+ this.checkAndEmitTaskExecutionEvents.bind(this)
+ this.checkAndEmitTaskQueuingEvents =
+ this.checkAndEmitTaskQueuingEvents.bind(this)
if (this.opts.enableEvents === true) {
this.emitter = new PoolEmitter()
0
)
}),
+ ...(this.opts.enableTasksQueue === true && {
+ backPressure: this.hasBackPressure()
+ }),
failedTasks: this.workerNodes.reduce(
(accumulator, workerNode) =>
accumulator + workerNode.usage.tasks.failed,
} else {
this.enqueueTask(workerNodeKey, task)
}
- this.checkAndEmitEvents()
})
}
}
}
- private checkAndEmitEvents (): void {
+ private checkAndEmitTaskExecutionEvents (): void {
if (this.emitter != null) {
if (this.busy) {
this.emitter.emit(PoolEvents.busy, this.info)
if (this.type === PoolTypes.dynamic && this.full) {
this.emitter.emit(PoolEvents.full, this.info)
}
- if (this.hasBackPressure()) {
- this.emitter.emit(PoolEvents.backPressure, this.info)
- }
+ }
+ }
+
+ private checkAndEmitTaskQueuingEvents (): void {
+ if (this.hasBackPressure()) {
+ this.emitter?.emit(PoolEvents.backPressure, this.info)
}
}
this.opts.enableTasksQueue === true &&
this.workerNodes.findIndex(
(workerNode) => !workerNode.hasBackPressure()
- ) !== -1
+ ) === -1
)
}
private executeTask (workerNodeKey: number, task: Task<Data>): void {
this.beforeTaskExecutionHook(workerNodeKey, task)
this.sendToWorker(workerNodeKey, task, task.transferList)
+ this.checkAndEmitTaskExecutionEvents()
}
private enqueueTask (workerNodeKey: number, task: Task<Data>): number {
- return this.workerNodes[workerNodeKey].enqueueTask(task)
+ const tasksQueueSize = this.workerNodes[workerNodeKey].enqueueTask(task)
+ this.checkAndEmitTaskQueuingEvents()
+ return tasksQueueSize
}
private dequeueTask (workerNodeKey: number): Task<Data> | undefined {
await pool.destroy()
})
- it("Verify that pool event emitter 'full' event can register a callback", async () => {
- const pool = new DynamicThreadPool(
+ it("Verify that pool event emitter 'ready' event can register a callback", async () => {
+ const pool = new DynamicClusterPool(
Math.floor(numberOfWorkers / 2),
numberOfWorkers,
- './tests/worker-files/thread/testWorker.js'
+ './tests/worker-files/cluster/testWorker.js'
)
- const promises = new Set()
- let poolFull = 0
let poolInfo
- pool.emitter.on(PoolEvents.full, (info) => {
- ++poolFull
+ let poolReady = 0
+ pool.emitter.on(PoolEvents.ready, (info) => {
+ ++poolReady
poolInfo = info
})
- for (let i = 0; i < numberOfWorkers * 2; i++) {
- promises.add(pool.execute())
- }
- await Promise.all(promises)
- // The `full` event is triggered when the number of submitted tasks at once reach the maximum number of workers in the dynamic pool.
- // So in total numberOfWorkers * 2 - 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool with min = (max = numberOfWorkers) / 2.
- expect(poolFull).toBe(numberOfWorkers * 2 - 1)
+ await waitPoolEvents(pool, PoolEvents.ready, 1)
+ expect(poolReady).toBe(1)
expect(poolInfo).toStrictEqual({
version,
type: PoolTypes.dynamic,
- worker: WorkerTypes.thread,
- ready: expect.any(Boolean),
+ worker: WorkerTypes.cluster,
+ ready: true,
strategy: WorkerChoiceStrategies.ROUND_ROBIN,
minSize: expect.any(Number),
maxSize: expect.any(Number),
await pool.destroy()
})
- it("Verify that pool event emitter 'ready' event can register a callback", async () => {
- const pool = new DynamicClusterPool(
- Math.floor(numberOfWorkers / 2),
+ it("Verify that pool event emitter 'busy' event can register a callback", async () => {
+ const pool = new FixedThreadPool(
numberOfWorkers,
- './tests/worker-files/cluster/testWorker.js'
+ './tests/worker-files/thread/testWorker.js'
)
+ const promises = new Set()
+ let poolBusy = 0
let poolInfo
- let poolReady = 0
- pool.emitter.on(PoolEvents.ready, (info) => {
- ++poolReady
+ pool.emitter.on(PoolEvents.busy, (info) => {
+ ++poolBusy
poolInfo = info
})
- await waitPoolEvents(pool, PoolEvents.ready, 1)
- expect(poolReady).toBe(1)
+ for (let i = 0; i < numberOfWorkers * 2; i++) {
+ promises.add(pool.execute())
+ }
+ await Promise.all(promises)
+ // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
+ // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool.
+ expect(poolBusy).toBe(numberOfWorkers + 1)
expect(poolInfo).toStrictEqual({
version,
- type: PoolTypes.dynamic,
- worker: WorkerTypes.cluster,
- ready: true,
+ type: PoolTypes.fixed,
+ worker: WorkerTypes.thread,
+ ready: expect.any(Boolean),
strategy: WorkerChoiceStrategies.ROUND_ROBIN,
minSize: expect.any(Number),
maxSize: expect.any(Number),
await pool.destroy()
})
- it("Verify that pool event emitter 'busy' event can register a callback", async () => {
- const pool = new FixedThreadPool(
+ it("Verify that pool event emitter 'full' event can register a callback", async () => {
+ const pool = new DynamicThreadPool(
+ Math.floor(numberOfWorkers / 2),
numberOfWorkers,
'./tests/worker-files/thread/testWorker.js'
)
const promises = new Set()
- let poolBusy = 0
+ let poolFull = 0
let poolInfo
- pool.emitter.on(PoolEvents.busy, (info) => {
- ++poolBusy
+ pool.emitter.on(PoolEvents.full, (info) => {
+ ++poolFull
poolInfo = info
})
for (let i = 0; i < numberOfWorkers * 2; i++) {
promises.add(pool.execute())
}
await Promise.all(promises)
- // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
- // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool.
- expect(poolBusy).toBe(numberOfWorkers + 1)
+ // The `full` event is triggered when the number of submitted tasks at once reach the maximum number of workers in the dynamic pool.
+ // So in total numberOfWorkers * 2 - 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool with min = (max = numberOfWorkers) / 2.
+ expect(poolFull).toBe(numberOfWorkers * 2 - 1)
expect(poolInfo).toStrictEqual({
version,
- type: PoolTypes.fixed,
+ type: PoolTypes.dynamic,
worker: WorkerTypes.thread,
ready: expect.any(Boolean),
strategy: WorkerChoiceStrategies.ROUND_ROBIN,