- public execute (data: Data): Promise<Response> {
- // Configure worker to handle message with the specified task
- const worker = this.chooseWorker()
- const messageId = ++this.nextMessageId
- const res = this.internalExecute(worker, messageId)
- this.checkAndEmitBusy()
- data = data ?? ({} as Data)
- this.sendToWorker(worker, { data, id: messageId })
+ public async execute (data?: Data): Promise<Response> {
+ const [workerNodeKey, workerNode] = this.chooseWorkerNode()
+ const submittedTask: Task<Data> = {
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
+ data: data ?? ({} as Data),
+ id: crypto.randomUUID()
+ }
+ const res = new Promise<Response>((resolve, reject) => {
+ this.promiseResponseMap.set(submittedTask.id as string, {
+ resolve,
+ reject,
+ worker: workerNode.worker
+ })
+ })
+ if (
+ this.opts.enableTasksQueue === true &&
+ (this.busy ||
+ this.workerNodes[workerNodeKey].tasksUsage.running >=
+ ((this.opts.tasksQueueOptions as TasksQueueOptions)
+ .concurrency as number))
+ ) {
+ this.enqueueTask(workerNodeKey, submittedTask)
+ } else {
+ this.executeTask(workerNodeKey, submittedTask)
+ }
+ this.checkAndEmitEvents()
+ // eslint-disable-next-line @typescript-eslint/return-await