Skip to content

Commit 48406a3

Browse files
authored
Modernize our TypeScript usage (#579)
2 parents 8266475 + 7cab1f9 commit 48406a3

47 files changed

Lines changed: 1947 additions & 3483 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
strategy:
1818
matrix:
19-
node-version: [20.x]
19+
node-version: [22.x]
2020
postgres-version: [12, 13, 14, 15]
2121

2222
services:
@@ -49,7 +49,7 @@ jobs:
4949

5050
strategy:
5151
matrix:
52-
node-version: [20.x]
52+
node-version: [22.x]
5353

5454
steps:
5555
- uses: actions/checkout@v3
@@ -67,7 +67,7 @@ jobs:
6767

6868
strategy:
6969
matrix:
70-
node-version: [20.x]
70+
node-version: [22.x]
7171
postgres-version: [12]
7272

7373
services:
@@ -100,7 +100,7 @@ jobs:
100100

101101
strategy:
102102
matrix:
103-
node-version: [20.x]
103+
node-version: [22.x]
104104
postgres-version: [12]
105105

106106
services:

.yarnrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ enableScripts: false
55
npmMinimalAgeGate: "1w"
66
# Unless it's one of ours:
77
npmPreapprovedPackages:
8+
- "jest-time-helpers"
89
- "graphile"
910
- "graphile-config"
1011
- "@graphile/*"

RELEASE_NOTES.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ to make sure the system as a whole remains consistent.
1919
Read more:
2020
[Worker Pro Migration](https://worker.graphile.org/docs/pro/migration).
2121

22+
## v0.18.0
23+
24+
- Since Node 20 is EOL, Node 22 is now the minimum supported version, per our
25+
[requirements documentation](https://worker.graphile.org/docs/requirements).
26+
- `Runner` gains `[Symbol.asyncDispose]()` method, so you can
27+
`await using runner = await run(...)` and the worker will be released when you
28+
reach the end of the scope. (Primarily useful for tests.)
29+
- Maintenance work: upgrade to latest TypeScript, Jest, eliminate ts-node, fix
30+
yargs, use erasable syntax only for type-stripping support,
31+
2232
## v0.17.2
2333

2434
Implement `npm pkg fix` hint; also: fix `version` export - was _still_

__tests__/__snapshots__/crontab.test.ts.snap

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`parses crontab file correctly 1`] = `
4-
Array [
5-
Object {
4+
[
5+
{
66
"identifier": "simple",
77
"match": [Function],
8-
"options": Object {
8+
"options": {
99
"backfillPeriod": 0,
1010
"jobKey": undefined,
1111
"jobKeyMode": undefined,
@@ -17,10 +17,10 @@ Array [
1717
"task": "simple",
1818
Symbol(isParsed): true,
1919
},
20-
Object {
20+
{
2121
"identifier": "every_day_at_4_am",
2222
"match": [Function],
23-
"options": Object {
23+
"options": {
2424
"backfillPeriod": 0,
2525
"jobKey": undefined,
2626
"jobKeyMode": undefined,
@@ -32,10 +32,10 @@ Array [
3232
"task": "every_day_at_4_am",
3333
Symbol(isParsed): true,
3434
},
35-
Object {
35+
{
3636
"identifier": "every_sunday_at_4_am",
3737
"match": [Function],
38-
"options": Object {
38+
"options": {
3939
"backfillPeriod": 0,
4040
"jobKey": undefined,
4141
"jobKeyMode": undefined,
@@ -47,10 +47,10 @@ Array [
4747
"task": "every_sunday_at_4_am",
4848
Symbol(isParsed): true,
4949
},
50-
Object {
50+
{
5151
"identifier": "sunday_7",
5252
"match": [Function],
53-
"options": Object {
53+
"options": {
5454
"backfillPeriod": 0,
5555
"jobKey": undefined,
5656
"jobKeyMode": undefined,
@@ -62,46 +62,46 @@ Array [
6262
"task": "every_sunday_at_4_am",
6363
Symbol(isParsed): true,
6464
},
65-
Object {
65+
{
6666
"identifier": "every_tuesday_at_4_am",
6767
"match": [Function],
68-
"options": Object {
68+
"options": {
6969
"backfillPeriod": 0,
7070
"jobKey": undefined,
7171
"jobKeyMode": undefined,
7272
"maxAttempts": undefined,
7373
"priority": undefined,
7474
"queueName": undefined,
7575
},
76-
"payload": Object {
76+
"payload": {
7777
"isTuesday": true,
7878
},
7979
"task": "every_tuesday_at_4_am",
8080
Symbol(isParsed): true,
8181
},
82-
Object {
82+
{
8383
"identifier": "stuff",
8484
"match": [Function],
85-
"options": Object {
85+
"options": {
8686
"backfillPeriod": 2685660000,
8787
"jobKey": undefined,
8888
"jobKeyMode": undefined,
8989
"maxAttempts": 3,
9090
"priority": 3,
9191
"queueName": "my_queue",
9292
},
93-
"payload": Object {
94-
"myExtraPayload": Object {
93+
"payload": {
94+
"myExtraPayload": {
9595
"stuff": "here with # hash char",
9696
},
9797
},
9898
"task": "one",
9999
Symbol(isParsed): true,
100100
},
101-
Object {
101+
{
102102
"identifier": "lots_of_spaces",
103103
"match": [Function],
104-
"options": Object {
104+
"options": {
105105
"backfillPeriod": 0,
106106
"jobKey": undefined,
107107
"jobKeyMode": undefined,
@@ -113,10 +113,10 @@ Array [
113113
"task": "lots_of_spaces",
114114
Symbol(isParsed): true,
115115
},
116-
Object {
116+
{
117117
"identifier": "with_key",
118118
"match": [Function],
119-
"options": Object {
119+
"options": {
120120
"backfillPeriod": 0,
121121
"jobKey": "my_key",
122122
"jobKeyMode": "replace",
@@ -128,10 +128,10 @@ Array [
128128
"task": "with_key",
129129
Symbol(isParsed): true,
130130
},
131-
Object {
131+
{
132132
"identifier": "with_key_and_mode",
133133
"match": [Function],
134-
"options": Object {
134+
"options": {
135135
"backfillPeriod": 0,
136136
"jobKey": "my_key",
137137
"jobKeyMode": "preserve_run_at",
@@ -143,10 +143,10 @@ Array [
143143
"task": "with_key_and_mode",
144144
Symbol(isParsed): true,
145145
},
146-
Object {
146+
{
147147
"identifier": "nested/task",
148148
"match": [Function],
149-
"options": Object {
149+
"options": {
150150
"backfillPeriod": 0,
151151
"jobKey": "my_key",
152152
"jobKeyMode": "preserve_run_at",
@@ -158,10 +158,10 @@ Array [
158158
"task": "nested/task",
159159
Symbol(isParsed): true,
160160
},
161-
Object {
161+
{
162162
"identifier": "nested/folder/task",
163163
"match": [Function],
164-
"options": Object {
164+
"options": {
165165
"backfillPeriod": 0,
166166
"jobKey": "my_key",
167167
"jobKeyMode": "preserve_run_at",

__tests__/batchJobs.test.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { jest } from "@jest/globals";
2+
13
import { Task, TaskList, WorkerSharedOptions } from "../src/interfaces";
24
import { runTaskListOnce } from "../src/main";
35
import {
@@ -45,20 +47,20 @@ test("on success, deletes job", () =>
4547

4648
const job3: Task = jest.fn((o) => {
4749
expect(o).toMatchInlineSnapshot(`
48-
Array [
49-
Object {
50+
[
51+
{
5052
"a": 1,
5153
},
52-
Object {
54+
{
5355
"a": 2,
5456
},
55-
Object {
57+
{
5658
"a": 3,
5759
},
5860
]
5961
`);
6062
});
61-
const job4: Task = jest.fn();
63+
const job4: Task = jest.fn(() => {});
6264
const tasks: TaskList = {
6365
job3,
6466
job4,
@@ -83,7 +85,7 @@ test("on failure, re-enqueues job", () =>
8385
const job3: Task = jest.fn(() => {
8486
throw new Error("RETRY");
8587
});
86-
const job4: Task = jest.fn();
88+
const job4: Task = jest.fn(() => {});
8789
const tasks: TaskList = {
8890
job3,
8991
job4,
@@ -115,7 +117,7 @@ test("on partial fail, re-enqueues job with just failed elements", () =>
115117
a % 2 === 1 ? Promise.reject(new Error(String(a))) : null,
116118
);
117119
});
118-
const job4: Task = jest.fn();
120+
const job4: Task = jest.fn(() => {});
119121
const tasks: TaskList = {
120122
job3,
121123
job4,
@@ -147,7 +149,7 @@ test("on partial fail (promise), re-enqueues job with just failed elements", ()
147149
a % 2 === 1 ? Promise.reject(new Error(String(a))) : null,
148150
);
149151
});
150-
const job4: Task = jest.fn();
152+
const job4: Task = jest.fn(() => {});
151153
const tasks: TaskList = {
152154
job3,
153155
job4,

__tests__/cron-timing.test.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { run, Runner } from "../src";
1+
import { run } from "../src";
22
import {
33
EventMonitor,
44
getJobs,
@@ -14,16 +14,6 @@ import {
1414
const { setTime } = setupFakeTimers();
1515
const REFERENCE_TIMESTAMP = 1609459200000; /* 1st January 2021, 00:00:00 UTC */
1616

17-
// Even on test fail we need the runner to shut down, so clean up after each test (rather than during).
18-
let runner: null | Runner = null;
19-
afterEach(() => {
20-
if (runner) {
21-
const promise = runner.stop();
22-
runner = null;
23-
return promise;
24-
}
25-
});
26-
2717
test("check timestamp is correct", () => {
2818
setTime(REFERENCE_TIMESTAMP);
2919
expect(new Date().toISOString()).toMatch(/^2021-01-01T00:00:0.*Z$/);
@@ -38,7 +28,7 @@ test("executes job when expected", () =>
3828
const cronFinishedBackfilling = eventMonitor.awaitNext("cron:started");
3929
const poolReady = eventMonitor.awaitNext("pool:listen:success");
4030
const cronScheduleCalls = eventMonitor.count("cron:schedule");
41-
runner = await run({
31+
await using _runner = await run({
4232
...options,
4333
crontab: `0 */4 * * * my_task`,
4434
events: eventMonitor.events,
@@ -78,7 +68,7 @@ test("doesn't schedule tasks twice when system clock reverses", () =>
7868
const cronFinishedBackfilling = eventMonitor.awaitNext("cron:started");
7969
const poolReady = eventMonitor.awaitNext("pool:listen:success");
8070
const cronScheduleCalls = eventMonitor.count("cron:schedule");
81-
runner = await run({
71+
await using _runner = await run({
8272
...options,
8373
crontab: `0 */4 * * * my_task`,
8474
events: eventMonitor.events,
@@ -110,7 +100,7 @@ test("clock skew doesn't prevent task from being scheduled at the right time", (
110100
const cronFinishedBackfilling = eventMonitor.awaitNext("cron:started");
111101
const poolReady = eventMonitor.awaitNext("pool:listen:success");
112102
const cronScheduleCalls = eventMonitor.count("cron:schedule");
113-
runner = await run({
103+
await using _runner = await run({
114104
...options,
115105
crontab: `0 */4 * * * my_task`,
116106
events: eventMonitor.events,
@@ -148,7 +138,7 @@ test("does not schedule duplicate jobs when a job key is supplied", () =>
148138
const eventMonitor = new EventMonitor();
149139
const cronFinishedBackfilling = eventMonitor.awaitNext("cron:started");
150140
const poolReady = eventMonitor.awaitNext("pool:listen:success");
151-
runner = await run({
141+
await using _runner = await run({
152142
...options,
153143
crontab: `0 */4 * * * my_task ?jobKey=foo`,
154144
events: eventMonitor.events,

__tests__/events.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { jest } from "@jest/globals";
12
import { EventEmitter } from "events";
2-
import { Pool } from "pg";
3+
import type { Pool } from "pg";
34

4-
import { run } from "../src";
5+
import { run, Runner } from "../src";
56
import deferred, { Deferred } from "../src/deferred";
67
import { Task, TaskList, WorkerSharedOptions } from "../src/interfaces";
78
import {
@@ -53,6 +54,7 @@ test("emits the expected events", () =>
5354
const jobPromises: {
5455
[id: string]: Deferred | undefined;
5556
} = {};
57+
let runner: Runner | undefined;
5658
try {
5759
const job1: Task<"job1"> = jest.fn(({ id }) => {
5860
const jobPromise = deferred();
@@ -81,7 +83,7 @@ test("emits the expected events", () =>
8183
});
8284

8385
const CONCURRENCY = 3;
84-
const runner = await run({
86+
runner = await run({
8587
concurrency: CONCURRENCY,
8688
pgPool,
8789
taskList: tasks,
@@ -129,11 +131,15 @@ test("emits the expected events", () =>
129131
await sleep(1);
130132
expect(finished).toBeTruthy();
131133
await runner.promise;
134+
runner = undefined;
132135
expect(eventCount("worker:release")).toEqual(CONCURRENCY);
133136
expect(eventCount("worker:stop")).toEqual(CONCURRENCY);
134137
expect(eventCount("pool:release")).toEqual(1);
135138
await expectJobCount(pgPool, 0);
136139
} finally {
140+
if (runner) {
141+
runner.stop();
142+
}
137143
Object.values(jobPromises).forEach((p) => p?.resolve());
138144
}
139145
}));

__tests__/fixtures-esm/tasks/wouldyoulike.js renamed to __tests__/fixtures-esm/tasks/wouldyoulike.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { rand } from "../blah.js";
1+
import { rand } from "../blah.mjs";
22
export default (_payload, helpers) => {
33
helpers.logger.debug(rand());
44
return "some sausages";
File renamed without changes.

0 commit comments

Comments
 (0)