Skip to content

Commit d4f8a95

Browse files
fix: refactor microservice task execution
1 parent 2162caa commit d4f8a95

File tree

1 file changed

+67
-54
lines changed

1 file changed

+67
-54
lines changed

src/services/abstract-microservice.ts

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ abstract class AbstractMicroservice {
375375
protected sendResponse(
376376
response: MicroserviceResponse,
377377
httpAgent: Agent,
378-
task: MicroserviceRequest | MicroserviceResponse,
378+
task: ITask['task'],
379379
): Promise<ITask> {
380380
const taskId = response.getId();
381381
const receiver =
@@ -390,6 +390,71 @@ abstract class AbstractMicroservice {
390390
return this.getTask(httpAgent, response);
391391
}
392392

393+
/**
394+
* Execute request
395+
* @protected
396+
*/
397+
protected async executeRequest(
398+
task: ITask['task'],
399+
req: ITask['req'],
400+
): Promise<MicroserviceResponse> {
401+
const response = new MicroserviceResponse({ id: task.getId() });
402+
403+
// Response error
404+
if (task instanceof MicroserviceResponse) {
405+
response.setError(task.getError());
406+
} else {
407+
// Handle request
408+
const {
409+
handler,
410+
options: { isDisableMiddlewares, isPrivate },
411+
} = this.endpoints[task.getMethod()] ?? { options: {} };
412+
413+
if (!handler || (isPrivate && !task.getParams()?.payload?.isInternal)) {
414+
response.setError(
415+
this.getException({
416+
code: EXCEPTION_CODE.METHOD_NOT_FOUND,
417+
status: 404,
418+
message: `Unknown method: ${task.getMethod()}`,
419+
}),
420+
);
421+
} else {
422+
try {
423+
// Apply before middleware if enabled
424+
const reqParams =
425+
(!isDisableMiddlewares && (await this.applyMiddlewares({ task }, req))) ||
426+
task.getParams();
427+
const resResult = await handler((reqParams as Record<string, any>) ?? {}, {
428+
app: this,
429+
sender: task.getParams()?.payload?.sender,
430+
req,
431+
});
432+
// Apply after middleware if enabled
433+
const result =
434+
!isDisableMiddlewares &&
435+
(await this.applyMiddlewares(
436+
{ task, result: resResult },
437+
req,
438+
MiddlewareType.response,
439+
));
440+
441+
response.setResult(result || resResult || {});
442+
} catch (e) {
443+
response.setError(
444+
this.getException({
445+
message: `Endpoint exception (${task.getMethod()}): ${e.message as string}`,
446+
code: e.code ?? EXCEPTION_CODE.ENDPOINT_EXCEPTION,
447+
status: e.status ?? 500,
448+
payload: e.payload ?? null,
449+
}),
450+
);
451+
}
452+
}
453+
}
454+
455+
return response;
456+
}
457+
393458
/**
394459
* Start queue worker
395460
* @protected
@@ -402,59 +467,7 @@ abstract class AbstractMicroservice {
402467
let { task, req } = await this.getTask(httpAgent);
403468

404469
while (true) {
405-
const response = new MicroserviceResponse({ id: task.getId() });
406-
407-
// Response error
408-
if (task instanceof MicroserviceResponse) {
409-
response.setError(task.getError());
410-
} else {
411-
// Handle request
412-
const {
413-
handler,
414-
options: { isDisableMiddlewares, isPrivate },
415-
} = this.endpoints[task.getMethod()] ?? { options: {} };
416-
417-
if (!handler || (isPrivate && !task.getParams()?.payload?.isInternal)) {
418-
response.setError(
419-
this.getException({
420-
code: EXCEPTION_CODE.METHOD_NOT_FOUND,
421-
status: 404,
422-
message: `Unknown method: ${task.getMethod()}`,
423-
}),
424-
);
425-
} else {
426-
try {
427-
// Apply before middleware if enabled
428-
const reqParams =
429-
(!isDisableMiddlewares && (await this.applyMiddlewares({ task }, req))) ||
430-
task.getParams();
431-
const resResult = await handler((reqParams as Record<string, any>) ?? {}, {
432-
app: this,
433-
sender: task.getParams()?.payload?.sender,
434-
req,
435-
});
436-
// Apply after middleware if enabled
437-
const result =
438-
!isDisableMiddlewares &&
439-
(await this.applyMiddlewares(
440-
{ task, result: resResult },
441-
req,
442-
MiddlewareType.response,
443-
));
444-
445-
response.setResult(result || resResult || {});
446-
} catch (e) {
447-
response.setError(
448-
this.getException({
449-
message: `Endpoint exception (${task.getMethod()}): ${e.message as string}`,
450-
code: e.code ?? EXCEPTION_CODE.ENDPOINT_EXCEPTION,
451-
status: e.status ?? 500,
452-
payload: e.payload ?? null,
453-
}),
454-
);
455-
}
456-
}
457-
}
470+
const response = await this.executeRequest(task, req);
458471

459472
({ task, req } = await this.sendResponse(response, httpAgent, task));
460473
}

0 commit comments

Comments
 (0)