type PoolType,
PoolTypes,
type TasksQueueOptions,
- type WorkerType
+ type WorkerType,
+ WorkerTypes
} from './pool'
import type {
IWorker,
return message => {
if (message.workerId != null && message.started != null) {
// Worker started message received
- this.workerNodes[
- this.getWorkerNodeKey(this.getWorkerById(message.workerId) as Worker)
- ].info.started = message.started
+ const worker = this.getWorkerById(message.workerId)
+ if (worker != null) {
+ this.workerNodes[this.getWorkerNodeKey(worker)].info.started =
+ message.started
+ } else {
+ throw new Error('Worker started message received from unknown worker')
+ }
} else if (message.id != null) {
// Task execution response received
const promiseResponse = this.promiseResponseMap.get(message.id)
private pushWorkerNode (worker: Worker): number {
this.workerNodes.push({
worker,
- info: { id: worker.threadId ?? worker.id, started: false },
+ info: { id: this.getWorkerId(worker), started: false },
usage: this.getWorkerUsage(),
tasksQueue: new Queue<Task<Data>>()
})
return this.workerNodes.length
}
+ /**
+ * Gets the worker id.
+ *
+ * @param worker - The worker.
+ * @returns The worker id.
+ */
+ private getWorkerId (worker: Worker): number | undefined {
+ if (this.worker === WorkerTypes.thread) {
+ return worker.threadId
+ } else if (this.worker === WorkerTypes.cluster) {
+ return worker.id
+ }
+ }
+
// /**
// * Sets the given worker in the pool worker nodes.
// *