-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
workerUtils.cleanup.test.ts
176 lines (158 loc) · 4.95 KB
/
workerUtils.cleanup.test.ts
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import {
DbJob,
Job,
makeWorkerUtils,
WorkerSharedOptions,
WorkerUtils,
} from "../src/index";
import {
ESCAPED_GRAPHILE_WORKER_SCHEMA,
makeSelectionOfJobs,
reset,
TEST_CONNECTION_STRING,
withPgClient,
} from "./helpers";
/** For sorting arrays of numbers or numeric strings */
function numerically(a: string | number, b: string | number) {
return parseFloat(String(a)) - parseFloat(String(b));
}
const options: WorkerSharedOptions = {};
let utils: WorkerUtils | null = null;
afterEach(async () => {
await utils?.release();
utils = null;
});
// Test DELETE_PERMAFAILED_JOBS
test("cleanup with DELETE_PERMAFAILED_JOBS", () =>
withPgClient(async (pgClient) => {
await reset(pgClient, options);
utils = await makeWorkerUtils({
connectionString: TEST_CONNECTION_STRING,
});
const jobs = await makeSelectionOfJobs(utils, pgClient);
const { failedJob, regularJob1, regularJob2 } = jobs;
const permafailJobIds = [failedJob.id, regularJob1.id, regularJob2.id].sort(
numerically,
);
const remainingJobIds = Object.values(jobs)
.filter((r) => !permafailJobIds.includes(r.id))
.map((r) => r.id)
.sort(numerically);
const failedJobs = await utils.permanentlyFailJobs(
permafailJobIds,
"TESTING!",
);
expect(failedJobs.length).toEqual(permafailJobIds.length);
await utils.cleanup({ tasks: ["DELETE_PERMAFAILED_JOBS"] });
const { rows } = await pgClient.query<DbJob>(
`select * from ${ESCAPED_GRAPHILE_WORKER_SCHEMA}._private_jobs`,
);
const jobIds = rows
.map((r) => r.id)
.filter((id) => !permafailJobIds.includes(id))
.sort(numerically);
expect(jobIds).toEqual(remainingJobIds);
}));
test("cleanup with GC_JOB_QUEUES", () =>
withPgClient(async (pgClient) => {
await reset(pgClient, options);
utils = await makeWorkerUtils({
connectionString: TEST_CONNECTION_STRING,
});
const jobs: Job[] = [];
const WORKER_ID_1 = "worker1";
const WORKER_ID_2 = "worker2";
const WORKER_ID_3 = "worker3";
let a = 0;
const date = new Date();
const specs = [
[WORKER_ID_1, "test", "test_job1"],
[WORKER_ID_2, "test2", "test_job2"],
[WORKER_ID_3, "test3", "test_job3"],
[null, null, "test_job4"],
] as const;
for (const [workerId, queueName, taskIdentifier] of specs) {
date.setMinutes(date.getMinutes() - 1);
const job = await utils.addJob(
taskIdentifier,
{ a: ++a },
{ queueName: queueName ?? undefined },
);
jobs.push(job);
if (workerId) {
await pgClient.query(
`\
with j as (
update ${ESCAPED_GRAPHILE_WORKER_SCHEMA}._private_jobs as jobs
set locked_at = $1, locked_by = $2
where id = $3
returning *
), q as (
update ${ESCAPED_GRAPHILE_WORKER_SCHEMA}._private_job_queues as job_queues
set
locked_by = $2::text,
locked_at = $1
from j
where job_queues.id = j.job_queue_id
)
select * from j`,
[date.toISOString(), workerId, job.id],
);
}
}
const { rows: queuesBefore } = await pgClient.query<{ queue_name: string }>(
`select queue_name from ${ESCAPED_GRAPHILE_WORKER_SCHEMA}._private_job_queues`,
);
expect(queuesBefore.map((q) => q.queue_name).sort()).toEqual([
"test",
"test2",
"test3",
]);
await utils.forceUnlockWorkers(["worker3"]);
const thirdJob = jobs[2]; // Belongs to queueName 'task3'
await utils.completeJobs([thirdJob.id]);
await utils.cleanup({ tasks: ["GC_JOB_QUEUES"] });
const { rows: queuesAfter } = await pgClient.query<{ queue_name: string }>(
`select queue_name from ${ESCAPED_GRAPHILE_WORKER_SCHEMA}._private_job_queues`,
);
expect(queuesAfter.map((q) => q.queue_name).sort()).toEqual([
"test",
"test2",
]);
}));
test("cleanup with GC_TASK_IDENTIFIERS", () =>
withPgClient(async (pgClient) => {
await reset(pgClient, options);
utils = await makeWorkerUtils({
connectionString: TEST_CONNECTION_STRING,
});
for (const taskIdentifier of [
"job3",
"test_job1",
"test_job2",
"test_job3",
]) {
const job = await utils.addJob(taskIdentifier, {});
if (taskIdentifier === "test_job2") {
await utils.completeJobs([job.id]);
}
}
const { rows: tasksBefore } = await pgClient.query<{ identifier: string }>(
`select identifier from ${ESCAPED_GRAPHILE_WORKER_SCHEMA}._private_tasks`,
);
expect(tasksBefore.map((q) => q.identifier).sort()).toEqual([
"job3",
"test_job1",
"test_job2",
"test_job3",
]);
await utils.cleanup({ tasks: ["GC_TASK_IDENTIFIERS"] });
const { rows: tasksAfter } = (await pgClient.query(
`select identifier from ${ESCAPED_GRAPHILE_WORKER_SCHEMA}._private_tasks`,
)) as { rows: { identifier: string }[] };
expect(tasksAfter.map((q) => q.identifier).sort()).toEqual([
"job3",
"test_job1",
"test_job3",
]);
}));