+ /** @inheritdoc */
+ public hasBackPressure (): boolean {
+ return this.tasksQueue.size >= this.tasksQueueBackPressureSize
+ }
+
+ /** @inheritdoc */
+ public async terminate (): Promise<void> {
+ const waitWorkerExit = new Promise<void>(resolve => {
+ this.registerOnceWorkerEventHandler('exit', () => {
+ resolve()
+ })
+ })
+ this.closeMessageChannel()
+ this.removeAllListeners()
+ switch (this.info.type) {
+ case WorkerTypes.thread:
+ this.worker.unref?.()
+ await this.worker.terminate?.()
+ break
+ case WorkerTypes.cluster:
+ this.registerOnceWorkerEventHandler('disconnect', () => {
+ this.worker.kill?.()
+ })
+ this.worker.disconnect?.()
+ break
+ }
+ await waitWorkerExit
+ }
+
+ /** @inheritdoc */
+ public registerWorkerEventHandler (
+ event: string,
+ handler: EventHandler<Worker>
+ ): void {
+ this.worker.on(event, handler)
+ }
+
+ /** @inheritdoc */
+ public registerOnceWorkerEventHandler (
+ event: string,
+ handler: EventHandler<Worker>
+ ): void {
+ this.worker.once(event, handler)
+ }
+
+ /** @inheritdoc */
+ public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined {
+ if (!Array.isArray(this.info.taskFunctionsProperties)) {
+ throw new Error(
+ `Cannot get task function worker usage for task function name '${name}' when task function properties list is not yet defined`
+ )
+ }
+ if (
+ Array.isArray(this.info.taskFunctionsProperties) &&
+ this.info.taskFunctionsProperties.length < 3
+ ) {
+ throw new Error(
+ `Cannot get task function worker usage for task function name '${name}' when task function properties list has less than 3 elements`
+ )
+ }
+ if (name === DEFAULT_TASK_NAME) {
+ name = this.info.taskFunctionsProperties[1].name
+ }
+ if (!this.taskFunctionsUsage.has(name)) {
+ this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name))
+ }
+ return this.taskFunctionsUsage.get(name)