Skip to content

Commit

Permalink
Missing file
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Jun 11, 2024
1 parent 74736b2 commit 7868f57
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/sql/returnJob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { DbJob, EnhancedWithPgClient } from "../interfaces";
import { CompiledSharedOptions } from "../lib";

export async function returnJob(
compiledSharedOptions: CompiledSharedOptions,
withPgClient: EnhancedWithPgClient,
poolId: string,
jobs: ReadonlyArray<DbJob>,
): Promise<void> {
const {
escapedWorkerSchema,
workerSchema,
resolvedPreset: {
worker: { preparedStatements },
},
} = compiledSharedOptions;

const jobsWithQueues: DbJob[] = [];
const jobsWithoutQueues: DbJob[] = [];

for (const job of jobs) {
if (job.job_queue_id != null) {
jobsWithQueues.push(job);
} else {
jobsWithoutQueues.push(job);
}
}

if (jobsWithQueues.length > 0) {
await withPgClient.withRetries((client) =>
client.query({
text: `\
with j as (
update ${escapedWorkerSchema}._private_jobs as jobs
set
attempts = GREATEST(0, attempts - 1),
locked_by = null,
locked_at = null
where id = ANY($2::bigint[])
and locked_by = $1::text
returning *
)
update ${escapedWorkerSchema}._private_job_queues as job_queues
set locked_by = null, locked_at = null
from j
where job_queues.id = j.job_queue_id and job_queues.locked_by = $1::text;`,
values: [poolId, jobsWithQueues.map((job) => job.id)],
name: !preparedStatements ? undefined : `return_job_q/${workerSchema}`,
}),
);
}
if (jobsWithoutQueues.length > 0) {
await withPgClient.withRetries((client) =>
client.query({
text: `\
update ${escapedWorkerSchema}._private_jobs as jobs
set
attempts = GREATEST(0, attempts - 1),
locked_by = null,
locked_at = null
where id = ANY($2::bigint[])
and locked_by = $1::text;`,
values: [poolId, jobsWithoutQueues.map((job) => job.id)],
name: !preparedStatements ? undefined : `return_job/${workerSchema}`,
}),
);
}
}

0 comments on commit 7868f57

Please sign in to comment.