forked from mtusi2026/mtusi2026lab5
feat: add operator replay, fault injection scenarios and staging cleanup
This commit is contained in:
@@ -72,9 +72,8 @@ async function expireFailedStaging(
|
||||
): Promise<number> {
|
||||
const rows = await prisma.attachment.findMany({
|
||||
where: {
|
||||
status: 'failed',
|
||||
stagingKey: { not: null },
|
||||
updatedAt: { lt: new Date(before) },
|
||||
OR: [{ status: 'ready' }, { status: 'failed', updatedAt: { lt: new Date(before) } }],
|
||||
},
|
||||
select: { id: true, stagingKey: true },
|
||||
take: 500,
|
||||
|
||||
@@ -71,6 +71,9 @@ export class JobProcessor {
|
||||
}
|
||||
const lease = await this.claim(job);
|
||||
if (!lease) {
|
||||
if (job.status === 'ready') {
|
||||
await this.removeStaging(job.attachment);
|
||||
}
|
||||
this.logger.log(`job ${job.id}: ${job.status}, not due or leased elsewhere, skipped`);
|
||||
return 'skipped';
|
||||
}
|
||||
|
||||
+1
-2
@@ -39,8 +39,7 @@ async function publishBatch(channel: ConfirmChannel): Promise<number> {
|
||||
logger.log(`published ${rows.length} job message(s), broker confirmed`);
|
||||
if (FAULT === 'exit-after-confirm') {
|
||||
logger.warn('fault injection: exit after broker confirm, before outbox is marked');
|
||||
process.kill(process.pid, 'SIGKILL');
|
||||
await sleep(60000);
|
||||
process.exit(137);
|
||||
}
|
||||
await tx.outboxEntry.updateMany({
|
||||
where: { id: { in: rows.map((row) => row.id) } },
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { validate as isUuid } from './common/uuid';
|
||||
import { PrismaService } from './db/prisma.service';
|
||||
import { PIPELINE_VERSION } from './jobs/job-message';
|
||||
import { replayJob } from './jobs/job-store';
|
||||
|
||||
const USAGE = 'usage: replay.js --list | <job_id|attachment_id>...';
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const args = process.argv.slice(2);
|
||||
if (args.length === 0 || (args[0] !== '--list' && args.some((arg) => !isUuid(arg)))) {
|
||||
throw new Error(USAGE);
|
||||
}
|
||||
const prisma = new PrismaService();
|
||||
try {
|
||||
if (args[0] === '--list') {
|
||||
const failed = await prisma.attachmentJob.findMany({
|
||||
where: { status: 'failed' },
|
||||
orderBy: { finishedAt: 'asc' },
|
||||
take: 100,
|
||||
});
|
||||
for (const job of failed) {
|
||||
process.stdout.write(
|
||||
`${JSON.stringify({
|
||||
job_id: job.id,
|
||||
attachment_id: job.attachmentId,
|
||||
error_code: job.errorCode,
|
||||
attempt: job.attempt,
|
||||
last_error: job.lastError,
|
||||
finished_at: job.finishedAt?.toISOString(),
|
||||
})}\n`,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const actor = process.env.OPERATOR ?? 'operator';
|
||||
for (const id of args) {
|
||||
const job = await prisma.attachmentJob.findFirst({
|
||||
where: {
|
||||
OR: [{ id }, { attachmentId: id, pipelineVersion: PIPELINE_VERSION }],
|
||||
},
|
||||
});
|
||||
const result = job ? await replayJob(prisma, job.id, actor) : 'not_found';
|
||||
process.stdout.write(`${JSON.stringify({ id, job_id: job?.id ?? null, result })}\n`);
|
||||
}
|
||||
} finally {
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
Logger.error(error instanceof Error ? error.message : 'fatal error', 'Replay');
|
||||
process.exit(1);
|
||||
});
|
||||
+1
-2
@@ -21,8 +21,7 @@ let inFlight = 0;
|
||||
function faultHooks(): ProcessorHooks {
|
||||
const crash = (point: string) => async () => {
|
||||
logger.warn(`fault injection: worker killed ${point}`);
|
||||
process.kill(process.pid, 'SIGKILL');
|
||||
await sleep(60000);
|
||||
process.exit(137);
|
||||
};
|
||||
if (FAULT === 'exit-after-store') {
|
||||
return { afterStore: crash('after storing objects, before commit and ack') };
|
||||
|
||||
Reference in New Issue
Block a user