Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
trailingComma: "all",
proseWrap: "always",
};
7 changes: 6 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ Read more:

## v0.18.0

- Now published as pure ESM, but worry not as unflagged require(ESM) is now
enabled by default in
[Node 20.19.0+](https://nodejs.org/pt-br/blog/release/v20.19.0),
[Node 22.12.0+](https://nodejs.org/en/blog/release/v22.12.0) and Node 24+ so
everything should continue to work as before.
- Since Node 20 is EOL, Node 22 is now the minimum supported version, per our
[requirements documentation](https://worker.graphile.org/docs/requirements).
- `Runner` gains `[Symbol.asyncDispose]()` method, so you can
`await using runner = await run(...)` and the worker will be released when you
reach the end of the scope. (Primarily useful for tests.)
- Maintenance work: upgrade to latest TypeScript, Jest, eliminate ts-node, fix
yargs, use erasable syntax only for type-stripping support,
yargs, use erasable syntax only for type-stripping support.
- `LogLevel` export is now type only - a string union rather than a TypeScript
const enum.

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { rand } = require("../blah.js");
const { rand } = require("../blah.cjs");
module.exports = (_payload, helpers) => {
helpers.logger.debug(rand());
return "some sausages";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const blah_1 = require("../blah.js");
const blah_1 = require("../blah.cjs");
exports.default = (_payload, helpers) => {
helpers.logger.debug((0, blah_1.rand)());
return "some TS sausages";
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 6 additions & 4 deletions __tests__/getTasks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type {
import { makeEnhancedWithPgClient } from "../src/lib.ts";
import { makeMockJob, withPgClient } from "./helpers.ts";

const __dirname = import.meta.dirname;

const options: WorkerSharedOptions = {};

const neverAbortController = new AbortController();
Expand Down Expand Up @@ -58,7 +60,7 @@ describe("commonjs", () => {
withPgClient(async (client) => {
const { tasks, release, compiledSharedOptions } = (await getTasks(
options,
`${__dirname}/fixtures/tasksFile.js`,
`${__dirname}/fixtures/tasksFile.cjs`,
)) as WatchedTaskList & { compiledSharedOptions: CompiledSharedOptions };
expect(tasks).toBeTruthy();
expect(Object.keys(tasks).sort()).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -89,7 +91,7 @@ describe("commonjs", () => {
withPgClient(async (client) => {
const { tasks, release, compiledSharedOptions } = (await getTasks(
options,
`${__dirname}/fixtures/tasksFile-ts.js`,
`${__dirname}/fixtures/tasksFile-ts.cjs`,
)) as WatchedTaskList & { compiledSharedOptions: CompiledSharedOptions };
expect(tasks).toBeTruthy();
expect(Object.keys(tasks).sort()).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -122,7 +124,7 @@ describe("commonjs", () => {
withPgClient(async (client) => {
const { tasks, release, compiledSharedOptions } = (await getTasks(
options,
`${__dirname}/fixtures/tasksFile_default.js`,
`${__dirname}/fixtures/tasksFile_default.cjs`,
)) as WatchedTaskList & { compiledSharedOptions: CompiledSharedOptions };
expect(tasks).toBeTruthy();
expect(Object.keys(tasks).sort()).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -153,7 +155,7 @@ describe("commonjs", () => {
withPgClient(async (client) => {
const { tasks, release, compiledSharedOptions } = (await getTasks(
options,
`${__dirname}/fixtures/tasksFile_default-ts.js`,
`${__dirname}/fixtures/tasksFile_default-ts.cjs`,
)) as WatchedTaskList & { compiledSharedOptions: CompiledSharedOptions };
expect(tasks).toBeTruthy();
expect(Object.keys(tasks).sort()).toMatchInlineSnapshot(`
Expand Down
10 changes: 6 additions & 4 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const js = require("@eslint/js");
const { FlatCompat } = require("@eslint/eslintrc");
const { readFileSync } = require("node:fs");
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";
import { readFileSync } from "node:fs";

const __dirname = import.meta.dirname;
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
Expand All @@ -22,7 +23,7 @@ const ignores = readFileSync(`${__dirname}/.lintignore`, "utf8")
})
.filter((l) => l != null);

module.exports = [
export default [
{
ignores: [...ignores, "eslint.config.js", ".prettierrc.js"],
},
Expand Down Expand Up @@ -162,6 +163,7 @@ module.exports = [
files: ["perfTest/**/*", "examples/**/*"],
rules: {
"import-x/extensions": "off",
"import-x/no-unresolved": "off",
"@typescript-eslint/no-var-requires": 0,
},
},
Expand Down
4 changes: 1 addition & 3 deletions examples/readme/events.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const { run, addJobAdhoc } = require(
/* "graphile-worker" */ "../../dist/index.js",
);
import { addJobAdhoc, run } from /* "graphile-worker" */ "../../dist/index.js";

async function main() {
// Run a worker to execute jobs:
Expand Down
4 changes: 2 additions & 2 deletions examples/readme/tasks/task_2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = async (payload, helpers) => {
export default async function task2(payload, helpers) {
// async is optional, but best practice
helpers.logger.debug(`Received ${JSON.stringify(payload)}`);
};
}
2 changes: 1 addition & 1 deletion examples/worker-bullmq-exporter/tasks/bullmq-exporter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Queue } = require("bullmq");
import { Queue } from "bullmq";

const defaultQueueName = "database-events";
const queueName = process.env.QUEUE_NAME || defaultQueueName;
Expand Down
6 changes: 3 additions & 3 deletions examples/worker-cloud-tasks-exporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ To create tasks in Cloud Tasks

```js
// tasks/cloud-tasks-exporter.js
const { CloudTasksClient } = require("@google-cloud/tasks");
import { CloudTasksClient } from "@google-cloud/tasks";

const client = new CloudTasksClient();

Expand Down Expand Up @@ -110,7 +110,7 @@ To run Graphile Worker task

```js
// tasks/cloud-tasks-exporter.js
module.exports = async (payload, { logger }) => {
export default async function task(payload, { logger }) {
logger.info(
`Delegating task to Cloud Tasks with payload ${JSON.stringify(payload)}`,
);
Expand All @@ -122,7 +122,7 @@ module.exports = async (payload, { logger }) => {
});

logger.info("Done!");
};
}
```

## Run the worker and add a job
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { CloudTasksClient } = require("@google-cloud/tasks");
import { CloudTasksClient } from "@google-cloud/tasks";

const client = new CloudTasksClient();

Expand Down Expand Up @@ -56,7 +56,7 @@ async function createTask({
}

// Graphile Worker Task
module.exports = async (payload, { logger }) => {
export default async function cloudTasksExporter(payload, { logger }) {
logger.info(
`Delegating task to Cloud Tasks with payload ${JSON.stringify(payload)}`,
);
Expand All @@ -68,4 +68,4 @@ module.exports = async (payload, { logger }) => {
});

logger.info("Done!");
};
}
4 changes: 2 additions & 2 deletions examples/worker-faktory-exporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ job from a queue.

```JS
// tasks/faktory-export.js
const faktory = require("faktory-worker");
import faktory from "faktory-worker";

module.exports = async (payload, helpers) => {
export default async function task(payload, helpers) {
const { param } = payload;
const { logger } = helpers;

Expand Down
6 changes: 3 additions & 3 deletions examples/worker-faktory-exporter/tasks/faktory-exporter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const faktory = require("faktory-worker");
import faktory from "faktory-worker";

module.exports = async (payload, helpers) => {
export default async function faktoryExporter(payload, helpers) {
const { param } = payload;
const { logger } = helpers;

Expand All @@ -17,4 +17,4 @@ module.exports = async (payload, helpers) => {
logger.info(`Received jid from Faktory: ${jid}. Thanks Faktory!`);

await faktoryClient.close();
};
}
2 changes: 1 addition & 1 deletion jest.config.js → jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
},
testRegex: "(/__tests__/.*\\.(test|spec))\\.[tj]sx?$",
moduleFileExtensions: ["ts", "mjs", "js", "json"],
extensionsToTreatAsEsm: [],
extensionsToTreatAsEsm: [".ts"],
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "graphile-worker",
"version": "0.17.2",
"type": "commonjs",
"type": "module",
"description": "Job queue for PostgreSQL",
"main": "dist/index.js",
"scripts": {
Expand Down
48 changes: 42 additions & 6 deletions scripts/options.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,51 @@ import "zx/globals";

import * as fs from "fs/promises";

await $`yarn link`;

// Create a temporary instance so we can read the options
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "gwdu-"));
const prev = process.cwd();

// Determine TypeScript version to synchronise with ours
const packageJson = JSON.parse(
await fs.readFile(`${prev}/package.json`, "utf8"),
);
const typescriptVersion = packageJson.devDependencies.typescript;
if (!typescriptVersion) {
throw new Error("Could not determine `typescript` version");
}
const graphileVersion = packageJson.devDependencies.graphile;
if (!graphileVersion) {
throw new Error("Could not determine `graphile` version");
}

await fs.writeFile(
`${tmp}/package.json`,
JSON.stringify(
{ type: "module", name: "@localrepo/tmp", private: true },
null,
2,
) + "\n",
);
await fs.cp(`${prev}/.yarnrc.yml`, `${tmp}/.yarnrc.yml`);
await fs.cp(`${prev}/.yarn`, `${tmp}/.yarn/`, { recursive: true });
cd(tmp);
await $`yarn link graphile-worker`;
await $`yarn add typescript graphile`;
await $`yarn link ${prev}`;

// If we have the crystal repo locally, use it's version of the `graphile` CLI
try {
const crystalRepo = `${process.env.HOME}/Dev/graphile/crystal`;
const stat = await fs.stat(crystalRepo);
if (stat.isDirectory()) {
console.log(`Using local crystal modules`);
await $`yarn link ${crystalRepo} --all`;
}
} catch {
// Ignore
}

await $`yarn add graphile-worker graphile@${graphileVersion} typescript@${typescriptVersion}`;
await fs.writeFile(
`graphile.config.mts`,
`graphile.config.ts`,
`\
import type {} from "graphile-worker";

Expand All @@ -26,7 +61,7 @@ export default preset;
);

// Get the markdown output for options
const output = await $`graphile config options`;
const output = await $`yarn graphile config options`;

// Go back and destroy our tempdir
cd(prev);
Expand All @@ -37,6 +72,7 @@ await fs.rm(tmp, { recursive: true, force: true });
const SEARCH = "\n## worker\n";
const i = output.stdout.indexOf(SEARCH);
if (i < 0) {
console.log(output.stdout);
throw new Error("Worker heading not found!");
}
const optionsMd = output.stdout
Expand Down
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import { loadConfig } from "graphile-config/load";
import * as yargs from "yargs";
import yargs from "yargs";

import { assertCleanupTasks, cleanup } from "./cleanup.ts";
import { getCronItemsInternal } from "./getCronItems.ts";
Expand All @@ -9,7 +9,7 @@ import { getUtilsAndReleasersFromOptions } from "./lib.ts";
import { EMPTY_PRESET, WorkerPreset } from "./preset.ts";
import { runInternal, runOnceInternal } from "./runner.ts";

const argv = yargs
const argv = yargs(process.argv.slice(2))
.parserConfiguration({
"boolean-negation": false,
})
Expand Down
4 changes: 2 additions & 2 deletions website/docs/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ Create a `tasks/` folder, and place in it JS files containing your task specs.
The names of these files will be the task identifiers, e.g. `hello` below:

```js title="tasks/hello.js"
module.exports = async (payload, helpers) => {
export default async function hello(payload, helpers) {
const { name } = payload;
helpers.logger.info(`Hello, ${name}`);
};
}
```

### Run the worker
Expand Down
Loading
Loading