Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

feat(nuq): add times_out_at logging to backlog

Gergő Móriczd42754236b23
diff --git a/apps/api/src/services/queue-jobs.ts b/apps/api/src/services/queue-jobs.ts
index 36a1722ed..dde434a34 100644
--- a/apps/api/src/services/queue-jobs.ts
+++ b/apps/api/src/services/queue-jobs.ts
@@ -55,6 +55,12 @@ async function _addScrapeJobToConcurrencyQueue(
ownerId: webScraperOptions.team_id ?? undefined,
groupId: webScraperOptions.crawl_id ?? undefined,
backlogged: true,
+ backloggedTimesOutAt: webScraperOptions.crawl_id
+ ? undefined
+ : new Date(
+ Date.now() +
+ (webScraperOptions.scrapeOptions?.timeout ?? 60 * 1000),
+ ),
},
);
@@ -90,6 +96,11 @@ async function _addScrapeJobsToConcurrencyQueue(
ownerId: job.data.team_id ?? undefined,
groupId: job.data.crawl_id ?? undefined,
backlogged: true,
+ backloggedTimesOutAt: job.data.crawl_id
+ ? undefined
+ : new Date(
+ Date.now() + (job.data.scrapeOptions?.timeout ?? 60 * 1000),
+ ),
},
})),
);
diff --git a/apps/api/src/services/worker/nuq.ts b/apps/api/src/services/worker/nuq.ts
index 9c857364c..8c5126cd9 100644
--- a/apps/api/src/services/worker/nuq.ts
+++ b/apps/api/src/services/worker/nuq.ts
@@ -44,6 +44,7 @@ type NuQJobOptions = {
ownerId?: string;
groupId?: string;
backlogged?: boolean;
+ backloggedTimesOutAt?: Date;
};
type NuQOptions = {
@@ -567,7 +568,7 @@ class NuQ<JobData = any, JobReturnValue = any> {
const result = this.rowToJob(
(
await nuqPool.query(
- `INSERT INTO ${this.queueName}${options.backlogged ? "_backlog" : ""} (id, data, priority, listen_channel_id, owner_id, group_id) VALUES ($1, $2, $3, $4, $5, $6) RETURNING ${(options.backlogged ? this.jobBacklogReturning : this.jobReturning).join(", ")};`,
+ `INSERT INTO ${this.queueName}${options.backlogged ? "_backlog" : ""} (id, data, priority, listen_channel_id, owner_id, group_id${options.backlogged ? ", times_out_at" : ""}) VALUES ($1, $2, $3, $4, $5, $6${options.backlogged ? ", $7" : ""}) RETURNING ${(options.backlogged ? this.jobBacklogReturning : this.jobReturning).join(", ")};`,
[
id,
data,
@@ -575,6 +576,13 @@ class NuQ<JobData = any, JobReturnValue = any> {
options.listenable ? listenChannelId : null,
normalizeOwnerId(options.ownerId),
options.groupId ?? null,
+ ...(options.backlogged
+ ? [
+ options.backloggedTimesOutAt
+ ? options.backloggedTimesOutAt.toISOString()
+ : null,
+ ]
+ : []),
],
)
).rows[0],
@@ -657,25 +665,47 @@ class NuQ<JobData = any, JobReturnValue = any> {
const valuesPlaceholders: string[] = [];
const params: any[] = [];
+ const columns = [
+ "id",
+ "data",
+ "priority",
+ "listen_channel_id",
+ "owner_id",
+ "group_id",
+ ...(tableSuffix === "_backlog" ? ["times_out_at"] : []),
+ ];
+
for (let i = 0; i < batch.length; i++) {
const job = batch[i];
- const baseIdx = i * 6 + 1;
+ const baseIdx = i * columns.length + 1;
valuesPlaceholders.push(
- `($${baseIdx}, $${baseIdx + 1}, $${baseIdx + 2}, $${baseIdx + 3}, $${baseIdx + 4}, $${baseIdx + 5})`,
+ `(${new Array(columns.length)
+ .fill(0)
+ .map((_, i) => "$" + (baseIdx + i))
+ .join(", ")})`,
);
params.push(
- job.id,
- job.data,
- job.options.priority ?? 0,
- job.options.listenable ? listenChannelId : null,
- normalizeOwnerId(job.options.ownerId),
- job.options.groupId ?? null,
+ ...[
+ job.id,
+ job.data,
+ job.options.priority ?? 0,
+ job.options.listenable ? listenChannelId : null,
+ normalizeOwnerId(job.options.ownerId),
+ job.options.groupId ?? null,
+ ...(tableSuffix === "_backlog"
+ ? [
+ job.options.backloggedTimesOutAt
+ ? job.options.backloggedTimesOutAt.toISOString()
+ : null,
+ ]
+ : []),
+ ],
);
}
- const query = `INSERT INTO ${this.queueName}${tableSuffix} (id, data, priority, listen_channel_id, owner_id, group_id) VALUES ${valuesPlaceholders.join(", ")} RETURNING ${(tableSuffix === "_backlog" ? this.jobBacklogReturning : this.jobReturning).join(", ")};`;
+ const query = `INSERT INTO ${this.queueName}${tableSuffix} (${columns.join(", ")}) VALUES ${valuesPlaceholders.join(", ")} RETURNING ${(tableSuffix === "_backlog" ? this.jobBacklogReturning : this.jobReturning).join(", ")};`;
const result = await nuqPool.query(query, params);
diff --git a/apps/nuq-postgres/nuq.sql b/apps/nuq-postgres/nuq.sql
index df49dad3d..0252dec84 100644
--- a/apps/nuq-postgres/nuq.sql
+++ b/apps/nuq-postgres/nuq.sql
@@ -46,6 +46,7 @@ CREATE TABLE IF NOT EXISTS nuq.queue_scrape_backlog (
listen_channel_id text, -- for listenable jobs over rabbitmq
owner_id uuid,
group_id uuid,
+ times_out_at timestamptz,
CONSTRAINT queue_scrape_backlog_pkey PRIMARY KEY (id)
);