Skip to content
Open
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
6 changes: 6 additions & 0 deletions .changeset/kind-otters-task-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"graphile-worker": patch
---

Clear the task identifier cache when a lookup fails, so a later poll retries
instead of reusing the rejected promise (#638).
37 changes: 37 additions & 0 deletions __tests__/taskIdentifiers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { EnhancedWithPgClient, TaskList } from "../src/interfaces.ts";
import type { CompiledSharedOptions } from "../src/lib.ts";
import { getTaskDetails } from "../src/taskIdentifiers.ts";

const tasks: TaskList = {
send_email: () => undefined,
};

test("retries a task identifier lookup after the cached lookup rejects", async () => {
let attempts = 0;
const withPgClient = Object.assign(async () => undefined, {
withRetries: async () => {
attempts += 1;
if (attempts === 1) {
const error = new Error("admin shutdown");
Object.assign(error, { code: "57P01" });
throw error;
}
return { rows: [{ id: 7, identifier: "send_email" }] };
},
}) as EnhancedWithPgClient;
const compiled = {
escapedWorkerSchema: "graphile_worker",
} as CompiledSharedOptions;

await expect(getTaskDetails(compiled, withPgClient, tasks)).rejects.toThrow(
"admin shutdown",
);

const details = await getTaskDetails(compiled, withPgClient, tasks);
expect(attempts).toBe(2);
expect(details.taskIds).toEqual([7]);
expect(details.supportedTaskIdentifierByTaskId[7]).toBe("send_email");

await getTaskDetails(compiled, withPgClient, tasks);
expect(attempts).toBe(2);
});
8 changes: 7 additions & 1 deletion src/taskIdentifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function getTaskDetails(
const { escapedWorkerSchema } = compiledSharedOptions;
assert.ok(supportedTaskNames.length, "No runnable tasks!");
cache.lastStr = str;
cache.lastDigest = (async () => {
const digestPromise = (async () => {
const { rows } = await withPgClient.withRetries(async (client) => {
await client.query({
text: `insert into ${escapedWorkerSchema}._private_tasks as tasks (identifier) select i from unnest($1::text[]) as u(i) where not exists (select 1 from ${escapedWorkerSchema}._private_tasks as existing where existing.identifier = u.i) on conflict do nothing`,
Expand Down Expand Up @@ -69,6 +69,12 @@ export function getTaskDetails(
cache.lastStr = str;
return cache.lastDigest;
})();
cache.lastDigest = digestPromise;
void digestPromise.catch(() => {
if (cache.lastStr === str) {
cache.lastStr = "";
}
});
}
return cache.lastDigest;
}
Expand Down