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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | 1x 20x 20x 20x 14x 14x 1x 1x 13x 13x 13x 13x 13x 4x 4x 4x 4x 4x 1x 1x 3x 3x 9x 7x 7x 9x 9x 1x 1x 8x 8x 11x 8x 3x 19x 74x | import { autoInjectable, inject, singleton } from "tsyringe";
import type { Logger } from "pino";
import { tokens } from "../../registry/tokens.js";
import { Result } from "../../tr8-script/domain/utils/types.js";
import { DBAccountSummaryDlqStore } from "../store/DB-account-summary-dlq.store.js";
import { AccountSummaryEventDtoV2Dlq } from "../../account-summary/dtos/account-summary-event.dto.js";
import { LarkNotificationService } from "../../commons/services/lark-notification.service.js";
@singleton()
@autoInjectable()
class AccountSummaryEventDlqHandler {
constructor(
private readonly dbAccountSummaryStore: DBAccountSummaryDlqStore,
@inject(tokens.Logger) private readonly logger: Logger,
private readonly larkNotification: LarkNotificationService,
) {}
async handle(
event: AccountSummaryEventDtoV2Dlq,
): Promise<Result<true, Error>> {
const getResult = await this.dbAccountSummaryStore.get(
event.id,
event.type,
);
if (!getResult.ok) {
this.logger.error(
{ err: getResult.error },
"[AccountSummaryDLQHandler] Failed to fetch existing DLQ record",
);
return { ok: false, error: getResult.error };
}
const existing = getResult.value;
const parsedError = new Error(event.error);
const isRetryable = this.isTransientError(parsedError);
let processedSuccessfully = false;
if (existing !== null) {
const retries = (existing.retries ?? 1) + 1;
const maxRetriesExceeded = retries > 5;
// Send Lark notification only once when max retries exceeded (retries exactly equals 6)
Iif (maxRetriesExceeded && retries === 6) {
this.logger.error(
`[AccountSummaryDLQHandler] Max retries exceeded for event ID: ${event.id}`,
{
eventId: event.id,
eventType: event.type,
retries,
error: event.error,
isRetryable,
},
);
// Send Lark alert (non-blocking)
await this.larkNotification.sendDLQMaxRetriesAlert({
service: "Account Summary",
eventId: event.id,
eventType: event.type,
retries,
error: event.error,
bookId: event.bookId,
});
}
const updateResult = await this.dbAccountSummaryStore.update(
existing.id,
{
retries,
status: maxRetriesExceeded
? "FAILED"
: isRetryable
? "RETRYING"
: "FAILED",
nextRetryAt:
isRetryable && !maxRetriesExceeded
? new Date(Date.now() + 5 * 60 * 1000)
: undefined,
error: event.error,
},
);
if (!updateResult.ok) {
this.logger.error(
{ err: updateResult.error },
"[AccountSummaryDLQHandler] Failed to update DLQ record",
);
return { ok: false, error: updateResult.error };
}
this.logger.warn(
`[AccountSummaryDLQHandler] Updated existing DLQ event: id=${
event.id
}, retries=${retries}, status=${
maxRetriesExceeded
? "FAILED (MAX_RETRIES)"
: isRetryable
? "RETRYING"
: "FAILED"
}`,
);
processedSuccessfully = true;
} else {
// Send Lark notification for new permanent errors
if (!isRetryable) {
this.logger.error(
`[AccountSummaryDLQHandler] Permanent failure detected for new event ID: ${event.id}`,
{
eventId: event.id,
eventType: event.type,
retries: 0,
error: event.error,
isRetryable,
},
);
// Send Lark alert (non-blocking)
await this.larkNotification.sendDLQMaxRetriesAlert({
service: "Account Summary",
eventId: event.id,
eventType: event.type,
retries: 0,
error: event.error,
bookId: event.bookId,
});
}
const saveResult = await this.dbAccountSummaryStore.save({
...event,
statusDlq: isRetryable ? "RETRYING" : "FAILED",
retries: 0,
nextRetryAt: isRetryable
? new Date(Date.now() + 5 * 60 * 1000)
: undefined,
});
if (!saveResult.ok) {
this.logger.error(
{ err: saveResult.error },
"[AccountSummaryDLQHandler] Failed to save new DLQ record",
);
return { ok: false, error: saveResult.error };
}
this.logger.info(
`[AccountSummaryDLQHandler] Saved new DLQ event: id=${
event.id
}, status=${isRetryable ? "RETRYING" : "FAILED"}`,
);
processedSuccessfully = true;
}
// Throw error for permanent failures after all processing is complete
if (processedSuccessfully && !isRetryable) {
throw new Error(
`Permanent failure for AccountSummaryEvent ${event.id}: ${event.error}`,
);
}
return { ok: true, value: true };
}
private isTransientError(error: Error): boolean {
const transientPatterns = [
"ECONNRESET",
"timeout",
"ETIMEDOUT",
"ENOTFOUND",
"HTTP 400",
];
return transientPatterns.some((pattern) => error.message.includes(pattern));
}
}
export { AccountSummaryEventDlqHandler };
|