-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathcleanup.ts
More file actions
67 lines (64 loc) · 1.75 KB
/
Copy pathcleanup.ts
File metadata and controls
67 lines (64 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { CleanupOptions, CleanupTask } from "./interfaces";
import { CompiledOptions } from "./lib";
const ALL_CLEANUP_TASKS: CleanupTask[] = [
"GC_TASK_IDENTIFIERS",
"GC_JOB_QUEUES",
"DELETE_PERMAFAILED_JOBS",
];
export function assertCleanupTasks(
tasks: string[],
): asserts tasks is CleanupTask[] {
const invalid = tasks.filter(
(t) => !(ALL_CLEANUP_TASKS as string[]).includes(t),
);
if (invalid.length > 0) {
throw new Error(
`Invalid cleanup tasks; allowed values: '${ALL_CLEANUP_TASKS.join(
"', '",
)}'; you provided: '${tasks.join("', '")}'`,
);
}
}
export async function cleanup(
compiledOptions: CompiledOptions,
options: CleanupOptions,
) {
const {
tasks = ["GC_JOB_QUEUES", "GC_TASK_IDENTIFIERS"],
taskIdentifiersToKeep = [],
} = options;
const { withPgClient, escapedWorkerSchema } = compiledOptions;
await withPgClient(async (client) => {
if (tasks.includes("DELETE_PERMAFAILED_JOBS")) {
await client.query(
`\
delete from ${escapedWorkerSchema}._private_jobs jobs
where attempts = max_attempts
and locked_at is null;`,
);
}
if (tasks.includes("GC_TASK_IDENTIFIERS")) {
await client.query(
`\
delete from ${escapedWorkerSchema}._private_tasks tasks
where tasks.id not in (
select jobs.task_id
from ${escapedWorkerSchema}._private_jobs jobs
)
and tasks.identifier <> all ($1::text[]);`,
[taskIdentifiersToKeep],
);
}
if (tasks.includes("GC_JOB_QUEUES")) {
await client.query(
`\
delete from ${escapedWorkerSchema}._private_job_queues job_queues
where locked_at is null and id not in (
select job_queue_id
from ${escapedWorkerSchema}._private_jobs jobs
where job_queue_id is not null
);`,
);
}
});
}