Skip to content

Commit 8cd9e1c

Browse files
committed
cleanup of dedupe keys lol
1 parent 061d955 commit 8cd9e1c

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

packages/migrations/src/actions/2025.12.12T00-00-00.workflows-deduplication.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ export default {
99
"expires_at" timestamptz NOT NULL,
1010
CONSTRAINT "dedupe_pk" PRIMARY KEY ("task_name", "dedupe_key")
1111
);
12+
13+
CREATE INDEX "graphile_worker_deduplication_expires_at_idx"
14+
ON "graphile_worker_deduplication" ("expires_at")
15+
;
1216
`,
1317
} satisfies MigrationExecutor;

packages/services/storage/src/db/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ export interface document_preflight_scripts {
135135
updated_at: Date;
136136
}
137137

138+
export interface graphile_worker_deduplication {
139+
dedupe_key: string;
140+
expires_at: Date;
141+
task_name: string;
142+
}
143+
138144
export interface migration {
139145
date: Date;
140146
hash: string;
@@ -441,6 +447,7 @@ export interface DBTables {
441447
document_collection_documents: document_collection_documents;
442448
document_collections: document_collections;
443449
document_preflight_scripts: document_preflight_scripts;
450+
graphile_worker_deduplication: graphile_worker_deduplication;
444451
migration: migration;
445452
oidc_integrations: oidc_integrations;
446453
organization_access_tokens: organization_access_tokens;

packages/services/workflows/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const modules = await Promise.all([
3535
import('./tasks/organization-invitation.js'),
3636
import('./tasks/organization-ownership-transfer.js'),
3737
import('./tasks/password-reset.js'),
38+
import('./tasks/purge-expired-dedupe-keys.js'),
3839
import('./tasks/purge-expired-schema-checks.js'),
3940
import('./tasks/schema-change-notification.js'),
4041
import('./tasks/usage-rate-limit-exceeded.js'),
@@ -44,6 +45,8 @@ const modules = await Promise.all([
4445
const crontab = `
4546
# Purge expired schema checks every Sunday at 10:00AM
4647
0 10 * * 0 purgeExpiredSchemaChecks
48+
# Every day at 3:00 AM
49+
0 3 * * * purgeExpiredDedupeKeys
4750
`;
4851

4952
const pg = await createPool(env.postgres.connectionString);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { sql } from 'slonik';
2+
import { z } from 'zod';
3+
import { defineTask, implementTask } from '../kit.js';
4+
5+
export const PurgeExpiredDedupeKeysTask = defineTask({
6+
name: 'purgeExpiredDedupeKeys',
7+
schema: z.unknown(),
8+
});
9+
10+
export const task = implementTask(PurgeExpiredDedupeKeysTask, async args => {
11+
args.logger.debug('purging expired postgraphile task dedupe keys');
12+
const result = await args.context.pg.oneFirst(sql`
13+
WITH "deleted" AS (
14+
DELETE FROM "graphile_worker_deduplication"
15+
WHERE "expires_at" < NOW()
16+
RETURNING 1
17+
)
18+
SELECT COUNT(*) FROM "deleted";
19+
`);
20+
const amount = z.number().parse(result);
21+
args.logger.debug(
22+
{ purgedCount: amount },
23+
'finished purging expired postgraphile task dedupe keys',
24+
);
25+
});

0 commit comments

Comments
 (0)