Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 3x 1x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x | import { autoInjectable, inject } from "tsyringe";
import { AmountMapper } from "../../commons/mappers/amount.mapper.js";
import { tokens } from "../../registry/tokens.js";
import { Metadata, Tr8ASTType } from "../../tr8-script/domain/ast.js";
import { MetadataSubs } from "../../tr8-script/domain/metadata-subs.js";
import { Amount } from "../../tr8-script/domain/value.js";
import { ExecutionEvent, ExecutionEventDlq } from "./execution-event.js";
import type { Logger } from "pino";
import { isAxiosError, type AxiosInstance } from "axios";
import { ExecutionConfig } from "../../config/execution.config.js";
import { ExecutionEntity } from "../entity/execution.entity.js";
import type { KafkaProducer } from "../../commons/interfaces/kafka-producer.base.js";
import { RedisExecutionStore } from "../store/redis-execution.store.js";
interface ExecutionData {
id: string;
type: Tr8ASTType;
status: "APPROVED" | "REJECTED";
metadata: Metadata;
message: string;
}
@autoInjectable()
class ExecutionEventHandler {
constructor(
@inject(tokens.ExecHttpClient)
private readonly client: AxiosInstance,
private readonly metadataSubs: MetadataSubs,
@inject(tokens.Logger) private readonly logger: Logger,
@inject(ExecutionConfig)
private ExecutionConfig: ExecutionConfig,
@inject(tokens.DlqExecutionEventProducer)
private readonly dlqExecutionEventProducer: KafkaProducer<ExecutionEventDlq>,
private readonly redisExecutionStore: RedisExecutionStore,
) {}
async handle(
event: ExecutionEvent,
partition: number,
offset: string,
): Promise<void> {
const { callback, status, scriptId, scriptType, partial_notif_id } = event;
const redisCheck = await this.redisExecutionStore.get(
scriptId,
scriptType,
status,
partial_notif_id,
);
Iif (redisCheck.ok && redisCheck.value) {
this.logger.info(
`[ExecutionHandler] Skipping already processed event scriptId=${scriptId}, scriptType=${scriptType}, status=${status}, partial_notif_id=${partial_notif_id}`,
);
return;
}
let response: ExecutionData;
const Vars = event.vars ?? {};
const rawMetadata = event.metadata;
Iif (!rawMetadata) {
throw new Error("Metadata is undefined");
}
const metadata = this.metadataSubs.substitute(
JSON.parse(rawMetadata),
Object.keys(Vars).reduce(
(acc, k) => {
acc[k] = AmountMapper.fromDTO(Vars[k]!);
return acc;
},
{} as Record<string, Amount>,
),
);
if (status === "APPROVED") {
response = {
id: event.scriptId,
type: event.scriptType,
status: "APPROVED",
metadata,
message: event.message,
};
} else {
response = {
id: event.scriptId,
type: event.scriptType,
status: "REJECTED",
metadata,
message: event.message,
};
}
this.logger.info({ response }, "response");
// Add the Bearer Token to the headers
const config = {
headers: {
Authorization: this.ExecutionConfig.token,
},
};
try {
await this.client.post(callback, response, config);
const executionEntity = new ExecutionEntity();
executionEntity.scriptId = response.id;
executionEntity.scriptType = response.type;
executionEntity.callback = callback;
executionEntity.status = response.status;
executionEntity.metadata = JSON.stringify(response.metadata);
executionEntity.message = response.message;
executionEntity.response_status = 200;
executionEntity.error_message = null;
executionEntity.is_send = true;
executionEntity.partition = partition;
executionEntity.offset = offset;
executionEntity.partial_notif_id = partial_notif_id;
await this.redisExecutionStore.set(executionEntity);
this.logger.info({ event }, "Callback sent successfully");
return;
} catch (error: unknown) {
this.logger.error({ error }, "Callback failed");
const { message, statusCode } = this.extractErrorInfo(error);
// TODO: Optionally, push the event to a Dead Letter Queue (DLQ)
const dlqEvent: ExecutionEventDlq = {
...event,
error: message,
statusCode: statusCode,
};
await this.dlqExecutionEventProducer.send(dlqEvent);
this.logger.warn(`Sent to Execution DLQ: ${event.scriptId}`);
}
}
private extractErrorInfo(error: unknown): {
message: string;
statusCode: number;
} {
let message = "Unknown error occurred";
let statusCode = 500;
if (isAxiosError(error)) {
if (error.response) {
statusCode = error.response.status;
message = `HTTP ${error.response.status}: ${error.response.statusText}`;
this.logger.error({ data: error.response.data }, "Response error data");
} else if (error.request) {
message = "No response received from server";
} else {
message = error.message;
}
} else if (error instanceof Error) {
message = error.message;
} else if (typeof error === "string") {
message = error;
}
return { message, statusCode };
}
}
export { ExecutionEventHandler };
export type { ExecutionData };
|