- /**
- * Execute the task specified into the constructor with the data parameter.
- *
- * @param data The input for the task specified.
- * @returns Promise that is resolved when the task is done.
- */
- public execute (data: Data): Promise<Response> {
- // configure worker to handle message with the specified task
- const worker: WorkerWithMessageChannel = this.chooseWorker()
- // console.log('FixedClusterPool#execute choosen worker:', worker)
- const previousWorkerIndex = this.tasks.get(worker)
- if (previousWorkerIndex !== undefined) {
- this.tasks.set(worker, previousWorkerIndex + 1)
- } else {
- throw Error('Worker could not be found in tasks map')
- }
- const id: number = ++this.id
- const res: Promise<Response> = this.internalExecute(worker, id)
- // console.log('FixedClusterPool#execute send data to worker:', worker)
- worker.send({ data: data || {}, id: id })
- return res
+ /** @inheritDoc */
+ protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
+ worker.send(message)
+ }
+
+ /** @inheritDoc */
+ protected registerWorkerMessageListener<Message extends Data | Response>(
+ worker: Worker,
+ listener: (message: MessageValue<Message>) => void
+ ): void {
+ worker.on('message', listener)
+ }
+
+ /** @inheritDoc */
+ protected createWorker (): Worker {
+ return cluster.fork(this.opts.env)
+ }
+
+ /** @inheritDoc */
+ protected afterWorkerSetup (worker: Worker): void {
+ // Listen to worker messages.
+ this.registerWorkerMessageListener(worker, super.workerListener())
+ }
+
+ /** @inheritDoc */
+ public get type (): PoolType {
+ return PoolType.FIXED