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
7 changes: 4 additions & 3 deletions src/routes/readQueues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function readQueuesRoute(app: FastifyInstance) {
{
schema: {
summary: 'Add job to a queue',
description: 'Enqueue one or more URLs into the specified queue. Optional flags include autoApprove and forceReindex (alias: force-reindex).',
description: 'Enqueue one or more URLs into the specified queue. Optional flags include autoApprove, replaceAllEmissions and forceReindex (alias: force-reindex).',
tags: ['Queues'],
params: readQueuePathParamsSchema,
body: addQueueJobBodySchema,
Expand All @@ -88,7 +88,7 @@ export async function readQueuesRoute(app: FastifyInstance) {
if (!resolvedName) {
return reply.status(400).send({ error: `Unknown queue '${name}'. Valid queues: ${Object.values(QUEUE_NAMES).join(', ')}` });
}
const { urls, autoApprove, forceReindex, threadId } = request.body as any;
const { urls, autoApprove, forceReindex, threadId, replaceAllEmissions } = request.body as any;

// Resolve threadId: accept provided threadId/runId or generate a new one
const providedOrCreatedThreadId = threadId || `run_${Date.now()}_${Math.random().toString(36).slice(2,8)}`;
Expand All @@ -99,13 +99,14 @@ export async function readQueuesRoute(app: FastifyInstance) {
urlsCount: Array.isArray(urls) ? urls.length : 0,
autoApprove: !!autoApprove,
forceReindex: !!forceReindex,
replaceAllEmissions: !!replaceAllEmissions,
},
'Enqueue request received'
);
const queueService = await QueueService.getQueueService();
const addedJobs: BaseJob[] = [];
for(const url of urls) {
const addedJob = await queueService.addJob(resolvedName, url, autoApprove, { forceReindex, threadId: providedOrCreatedThreadId });
const addedJob = await queueService.addJob(resolvedName, url, autoApprove, { forceReindex, threadId: providedOrCreatedThreadId, replaceAllEmissions });
addedJobs.push(addedJob);
}
return reply.send(addedJobs);
Expand Down
1 change: 1 addition & 0 deletions src/schemas/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export const addQueueJobBodySchema = z.object({
urls: z.array(string().url()),
forceReindex: z.boolean().optional().describe('Re-index markdown even if already indexed'),
threadId: z.string().optional(),
replaceAllEmissions: z.boolean().optional().default(false).describe('Replace all scope 1,2,3 emissions and totals (delete old ones from all periods) before adding new ones')
});
1 change: 1 addition & 0 deletions src/schemas/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const baseJobSchema = z.object({
url: z.string().url().optional(),
autoApprove: z.boolean().optional().default(false),
timestamp: z.number(),
processedOn: z.number().optional(),
processId: z.string().optional(),
threadId: z.string().optional(),
queue: z.string(),
Expand Down
5 changes: 3 additions & 2 deletions src/services/QueueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,16 @@ export class QueueService {
return jobs;
}

public async addJob(queueName: string, url: string, autoApprove: boolean = false, options?: { forceReindex?: boolean; threadId?: string }): Promise<BaseJob> {
public async addJob(queueName: string, url: string, autoApprove: boolean = false, options?: { forceReindex?: boolean; threadId?: string; replaceAllEmissions?: boolean }): Promise<BaseJob> {
const queue = await this.getQueue(queueName);
const id = crypto.randomUUID();
const job = await queue.add('download ' + url.slice(-20), {
url: url.trim(),
autoApprove,
id,
...(options?.threadId ? { threadId: options.threadId } : {}),
...(options?.forceReindex !== undefined ? { forceReindex: options.forceReindex } : {})
...(options?.forceReindex !== undefined ? { forceReindex: options.forceReindex } : {}),
...(options?.replaceAllEmissions !== undefined ? { replaceAllEmissions: options.replaceAllEmissions } : {})
});
return transformJobtoBaseJob(job);
}
Expand Down