Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/shared/modules/global/queue-scheduler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export class QueueSchedulerService implements OnModuleInit, OnModuleDestroy {
private readonly logger: Logger = new Logger(QueueSchedulerService.name);
private boss: PgBoss;

private jobsHandlersMap = new Map<string, () => void>();
private jobsHandlersMap = new Map<
string,
(resolution?: 'fail' | 'complete', result?: any) => void
>();

get isEnabled() {
return String(process.env.DISPATCH_AI_REVIEW_WORKFLOWS) === 'true';
Expand Down Expand Up @@ -111,11 +114,13 @@ export class QueueSchedulerService implements OnModuleInit, OnModuleDestroy {
return;
}

await this.boss[resolution](queueName, jobId);
if (this.jobsHandlersMap.has(jobId)) {
this.jobsHandlersMap.get(jobId)?.call(null);
this.jobsHandlersMap.get(jobId)?.call(null, resolution);
this.jobsHandlersMap.delete(jobId);
} else {
await this.boss[resolution](queueName, jobId);
}

this.logger.log(`Job ${jobId} ${resolution} called.`);
}

Expand Down Expand Up @@ -146,7 +151,10 @@ export class QueueSchedulerService implements OnModuleInit, OnModuleDestroy {
);
}

registerJobHandler(jobId: string, handler: () => void) {
registerJobHandler(
jobId: string,
handler: (resolution?: string, result?: any) => void,
) {
this.jobsHandlersMap.set(jobId, handler);
}
}
19 changes: 8 additions & 11 deletions src/shared/modules/global/workflow-queue.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,20 @@ export class WorkflowQueueHandler implements OnModuleInit {
data: {
status: 'DISPATCHED',
scheduledJobId: job.id,
completedJobs: 0,
},
});

// return not-resolved promise,
// this will put a pause on the job
// until it is marked as completed via webhook call
return new Promise<void>((resolve) => {
this.scheduler.registerJobHandler(job.id, () => resolve());
return new Promise<void>((resolve, reject) => {
this.scheduler.registerJobHandler(
job.id,
(resolution: string = 'complete', result: any) => {
(resolution === 'fail' ? reject : resolve)(result);
},
);
});
}

Expand Down Expand Up @@ -258,15 +264,6 @@ export class WorkflowQueueHandler implements OnModuleInit {
}

if (conclusion === 'FAILURE') {
// reset data for aiWorkflowRun
await this.prisma.aiWorkflowRun.update({
where: { id: aiWorkflowRun.id },
data: {
status: 'INIT',
completedJobs: 0,
},
});

await this.scheduler.completeJob(
(aiWorkflowRun as any).workflow.gitWorkflowId,
aiWorkflowRun.scheduledJobId as string,
Expand Down