import {
type MessageChannel,
type MessagePort,
- SHARE_ENV,
type TransferListItem,
- Worker,
- type WorkerOptions,
+ type Worker,
isMainThread
} from 'node:worker_threads'
import type { MessageValue } from '../../utility-types'
import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
import { type WorkerType, WorkerTypes } from '../worker'
-/**
- * Options for a poolifier thread pool.
- */
-export interface ThreadPoolOptions extends PoolOptions<Worker> {
- /**
- * Worker options.
- *
- * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
- */
- workerOptions?: WorkerOptions
-}
-
/**
* A thread pool with a fixed number of threads.
*
public constructor (
numberOfThreads: number,
filePath: string,
- protected readonly opts: ThreadPoolOptions = {}
+ protected readonly opts: PoolOptions<Worker> = {}
) {
super(numberOfThreads, filePath, opts)
}
return isMainThread
}
- /** @inheritDoc */
- protected async destroyWorkerNode (workerNodeKey: number): Promise<void> {
- this.flagWorkerNodeAsNotReady(workerNodeKey)
- this.flushTasksQueue(workerNodeKey)
- // FIXME: wait for tasks to be finished
- const workerNode = this.workerNodes[workerNodeKey]
- const worker = workerNode.worker
- const waitWorkerExit = new Promise<void>(resolve => {
- worker.once('exit', () => {
- resolve()
- })
- })
- await this.sendKillMessageToWorker(workerNodeKey)
- workerNode.closeChannel()
- workerNode.removeAllListeners()
- await worker.terminate()
- await waitWorkerExit
- }
-
/** @inheritDoc */
protected sendToWorker (
workerNodeKey: number,
)
}
- /** @inheritDoc */
- protected createWorker (): Worker {
- return new Worker(this.filePath, {
- env: SHARE_ENV,
- ...this.opts.workerOptions
- })
- }
-
/** @inheritDoc */
protected get type (): PoolType {
return PoolTypes.fixed