Something went wrong. Try again.
because I got bored of customising my CV for every job
Something went wrong. Try again.
2.7 kB · 67 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768import { AsyncJobStore, ParseCVMessage } from "@cv/core";import { Inject, Injectable, Logger } from "@nestjs/common";import { type Envelope, type Handler } from "@riotbyte-com/project-q-core";import { HandlerTag } from "@riotbyte-com/project-q-nestjs";import { type TextSource, TextSourceResolver } from "./text-source.resolver";import { type UserCvParser, UserCvParserService,} from "./user-cv-parser.service";
@Injectable()@HandlerTag.decorator({ handles: "parse-cv" })export class ParseCvHandler implements Handler { private readonly logger = new Logger(ParseCvHandler.name);
constructor( private readonly asyncJobs: AsyncJobStore, @Inject(TextSourceResolver) private readonly textSource: TextSource, @Inject(UserCvParserService) private readonly parser: UserCvParser, ) {}
async handle(envelope: Envelope): Promise<void> { const { jobId, input } = ParseCVMessage.parse(envelope.message).data;
const job = await this.asyncJobs.findById(jobId);
// Row gone (cancelled, cascade-deleted, dispatch-rollback won a race). // The message has nothing left to update so retrying is pointless - ack // the envelope by returning normally and let project-q drop the message. if (!job) { this.logger.warn( `Skipping parse-cv job ${jobId}: AsyncJob row no longer exists`, ); return; }
this.logger.log(`Processing parse-cv job ${jobId}`);
// Two-stage error handling. Parse failures go through `markFailed` so // the row reflects the user-facing cause. The terminal `markCompleted` // write is OUTSIDE the parse try/catch so a DB error there doesn't // get misreported as a parse failure - it surfaces raw and project-q // retries. let result: Awaited<ReturnType<UserCvParser["parseForUser"]>>; try { const text = await this.textSource.resolve(input); result = await this.parser.parseForUser(job.userId, text); } catch (err) { const message = err instanceof Error ? err.message : String(err); this.logger.error(`Failed parse-cv job ${jobId}: ${message}`); // Best-effort error write-back. If this fails too (DB gone, row // deleted out from under us), log it but rethrow the ORIGINAL error // so project-q sees the real cause - not "row missing". await this.asyncJobs.markFailed(jobId, message).catch((writeErr) => { this.logger.error( `Failed to persist error onto AsyncJob ${jobId}: ${ writeErr instanceof Error ? writeErr.message : String(writeErr) }`, ); }); throw err; }
await this.asyncJobs.markCompleted(jobId, result); this.logger.log(`Completed parse-cv job ${jobId}`); }}