Skip to content

Commit 4aab1ad

Browse files
committed
tweaks to tasks documentation for clarity and consistency
1 parent 85c36ac commit 4aab1ad

File tree

3 files changed

+106
-114
lines changed

3 files changed

+106
-114
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ declare global {
129129

130130
/**
131131
* A list of file extensions (in priority order) that Graphile Worker
132-
* should attempt to import directly when loading task executors from the
133-
* file system.
132+
* should attempt to import as Node modules when loading task executors from
133+
* the file system.
134134
*
135135
* @defaultValue `[".js", ".cjs", ".mjs"]`
136136
*/

website/docs/config.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ Here are the options under the `worker` key as defined by
177177
}
178178
```
179179

180+
See the
181+
[Graphile Worker source](https://github.com/jcgsville/worker/blob/85c36ac4e684a3a782fc528dca95c8ba6177fa8a/src/config.ts#L13)
182+
for the default `worker` options set by the default Worker Preset.
183+
180184
### worker.concurrentJobs
181185

182186
Type: `number | undefined`
@@ -211,7 +215,8 @@ resolved.)
211215
Type: `string[] | undefined`
212216

213217
A list of file extensions (in priority order) that Graphile Worker should
214-
attempt to import directly when loading task executors from the file system.
218+
attempt to import as Node modules when loading task executors from the file
219+
system.
215220

216221
### worker.getQueueNameBatchDelay
217222

website/docs/tasks.md

Lines changed: 98 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -26,48 +26,32 @@ failure and the task is rescheduled using an exponential-backoff algorithm.
2626

2727
Each task function is passed two arguments:
2828

29-
- `payload` — the (JSON) payload you passed when calling
29+
- `payload` — the JSON payload you passed when calling
3030
`graphile_worker.add_job(...)` in the database, or `addJob(...)` via the JS
3131
API
32-
- `helpers` (see [`helpers`](#helpers) below) — an object containing:
33-
- `logger` — a scoped [Logger](/docs/library/logger) instance, to aid
34-
tracing/debugging
35-
- `job` — the whole job (including `uuid`, `attempts`, etc) — you
36-
shouldn't need this
37-
- `getQueueName()` — get the name of the queue the job is in (may or may
38-
not return a promise - recommend you always `await` it)
39-
- `abortSignal` — could be an `AbortSignal`, or `undefined` if not
40-
supported by this release of worker; if set, use this to abort your task
41-
early on graceful shutdown (can be passed to a number of asynchronous
42-
Node.js methods)
43-
- `abortPromise` — if present, a promise that will reject when
44-
`abortSignal` aborts; convenient for exiting your task when the abortSignal
45-
fires: `Promise.race([abortPromise, doYourThing()])`
46-
- `withPgClient` — a helper to use to get a database client
47-
- `query(sql, values)` — a convenience wrapper for
48-
`withPgClient(pgClient => pgClient.query(sql, values))`
49-
- `addJob` — a helper to schedule a job
32+
- `helpers` see [`helpers`](#helpers) below
5033

5134
:::warning Important
5235

53-
Your jobs must wait for all asynchronous work to be completed before returning,
54-
otherwise we might think they were successful prematurely. Every promise that a
55-
task executor triggers must be `await`-ed; task executors _should not_ create
56-
“untethered” promises.
36+
Your task executors must wait for all asynchronous work for a job to be
37+
completed before returning, otherwise Graphile Worker might think they were
38+
successful prematurely. Every promise that a task executor triggers must be
39+
`await`-ed; task executors _should not_ create “untethered”
40+
promises.
5741

5842
:::
5943

6044
:::tip
6145

62-
We automatically retry the job if it fails, so it's often sensible to split
63-
a large job into multiple smaller jobs, this also allows them to run in parallel
64-
resulting in faster execution. This is particularly important for tasks that are
65-
not idempotent (i.e. running them a second time will have extra side effects)
66-
— for example sending emails.
46+
Graphile Worker automatically retries the job if it fails, so it's often
47+
sensible to split a large job into multiple smaller jobs, this also allows them
48+
to run in parallel resulting in faster execution. This is particularly important
49+
for tasks that are not idempotent (i.e. running them a second time will have
50+
extra side effects) — for example sending emails.
6751

6852
:::
6953

70-
## Example task executors
54+
## Example JS task executors
7155

7256
```js title="tasks/task_1.js"
7357
module.exports = async (payload) => {
@@ -82,46 +66,41 @@ module.exports = async (payload, helpers) => {
8266
};
8367
```
8468

85-
## The `tasks/` folder
69+
## The task directory
8670

87-
When you run `graphile-worker`, it will look in the current directory for a
88-
folder called `tasks`, and it will recursively look for files suitable to run as
89-
tasks. File names excluding the extension and folder names must only use
90-
alphanumeric characters, underscores and dashes (`/^[A-Za-z0-9_-]+$/`) to be
91-
recognized. Graphile Worker will then attempt to load the file as a task
92-
executor; the task identifier for this will be all the folders and the file name
93-
(excluding the extension) joined with `/` characters; e.g.
94-
`tasks/send_notification.js` would get the identifier `send_notification` and
95-
`tasks/users/emails/verify.js` would get the identifier `users/emails/verify`.
96-
How the file is loaded as a task executor will depend on the file in question
97-
and the plugins you have loaded.
71+
When you run Graphile Worker, it will look in the configured
72+
[`taskDirectory`](./config#workertaskdirectory) for files suitable to run as
73+
tasks.
9874

99-
```
100-
current directory
101-
├── package.json
102-
├── node_modules
103-
└── tasks
104-
├── send_notification.js
105-
├── generate_pdf.js
106-
└── users
107-
├── congratulate.js
108-
└── emails
109-
├── verify.js
110-
└── send_otp.js
111-
```
75+
File names excluding the extension and folder names must only use alphanumeric
76+
characters, underscores, and dashes (`/^[A-Za-z0-9_-]+$/`) to be recognized.
77+
78+
Graphile Worker will then attempt to load the file as a task executor; the task
79+
identifier for this task will be all the folders and the file name (excluding
80+
the extension) joined with `/` characters:
81+
82+
- `${taskDirectory}/send_notification.js` would get the task identifier
83+
`send_notification`.
84+
- `${taskDirectory}/users/emails/verify.js` would get the task identifier
85+
`users/emails/verify`.
86+
87+
How the file is loaded as a task executor will depend on the specific file and
88+
the plugins you have loaded.
11289

113-
## Loading JavaScript files
90+
## Loading JavaScript task executors
11491

115-
Out of the box, Graphile Worker will load `.js`, `.cjs` and `.mjs` files using
116-
the `import()` function. If the file is a CommonJS module then Worker will
117-
expect `module.exports` to be the task executor function; if the file is an
118-
ECMAScript module (ESM) then Worker will expect the default export to be the
119-
task executor function.
92+
With the default preset, Graphile Worker will load `.js`, `.cjs` and `.mjs`
93+
files as task executors using the `import()` function. If the file is a CommonJS
94+
module, then Worker will expect `module.exports` to be the task executor
95+
function; if the file is an ECMAScript module (ESM) then Worker will expect the
96+
default export to be the task executor function.
12097

121-
Via plugins, support for other ways of loading task files can be added; look at
122-
the source code of `LoadTaskFromJsPlugin.ts` for inspiration.
98+
You can add support for other ways of loading task executors via plugins; look
99+
at the source code of
100+
[`LoadTaskFromJsPlugin.ts`](https://github.com/graphile/worker/blob/main/src/plugins/LoadTaskFromJsPlugin.ts)
101+
for inspiration.
123102

124-
### Loading TypeScript files
103+
### Loading TypeScript task executors
125104

126105
:::tip
127106

@@ -130,16 +109,15 @@ TypeScript files to JS and then have Graphile Worker load the JS files.
130109

131110
:::
132111

133-
To load TypeScript files directly as tasks (without precompilation), one way is
134-
to:
112+
To load TypeScript files directly as task executors (without precompilation),
113+
one way is to do the following:
135114

136-
1. install `ts-node`,
137-
2. add `".ts"` to the `worker.fileExtensions` list in your `graphile.config.ts`,
138-
3. run Graphile Worker with the environmental variable
115+
1. Install `ts-node`.
116+
2. Add `".ts"` to the `worker.fileExtensions` list in your preset.
117+
3. Run Graphile Worker with the environment variable
139118
`NODE_OPTIONS="--loader ts-node/esm"` set.
140119

141120
```ts title="Example graphile.config.ts"
142-
import type { GraphileConfig } from "graphile-config";
143121
import type {} from "graphile-worker";
144122

145123
const preset: GraphileConfig.Preset = {
@@ -167,18 +145,18 @@ This feature is currently experimental.
167145
:::
168146

169147
If you're running on Linux or Unix (including macOS) then if Graphile Worker
170-
finds an executable file inside of `tasks/` it will create a task executor for
171-
it. When a task of this kind is found, Graphile Worker will execute the file
172-
setting the relevant environmental variables and passing in the payload
148+
finds an executable file inside of the `taskDirectory` it will create a task
149+
executor for it. When a task of this kind is found, Graphile Worker will execute
150+
the file with the relevant environment variables and pass in the payload
173151
according to the encoding. If the executable exits with code `0` then Graphile
174-
Worker will see this as success, all other exit codes are seen as failure.
152+
Worker will see this as success. All other exit codes are seen as failure.
175153

176-
### Environmental variables
154+
### Environment variables
177155

178156
- `GRAPHILE_WORKER_PAYLOAD_FORMAT` — the encoding that Graphile Worker
179-
uses to pass the payload to the binary. Currently this will be the string
180-
`json`, but you should check this before processing the payload in case the
181-
format changes.
157+
used to pass the payload to the binary. Currently this will always be the
158+
string `json`, but you should check this before processing the payload in case
159+
the format changes.
182160
- `GRAPHILE_WORKER_TASK_IDENTIFIER` — the identifier for the task this
183161
file represents (useful if you want multiple task identifiers to be served by
184162
the same binary file, e.g. via symlinks)
@@ -198,34 +176,59 @@ Worker will see this as success, all other exit codes are seen as failure.
198176

199177
In the JSON payload format, your binary will be fed via stdin
200178
`JSON.stringify({payload})`; for example, if you did
201-
`addJob('myScript', {mol: 42})` then your `myScript` task would be sent
179+
`addJob('my_script', {mol: 42})` then your `my_script` task would be sent
202180
`{"payload":{"mol":42}}` via stdin.
203181

204182
## Handling batch jobs
205183

206-
If the payload is an array, then _optionally_ your task may choose to return an
207-
array of the same length, the entries in which are promises. Should any of these
208-
promises reject, then the job will be re-enqueued, but the payload will be
209-
overwritten to only contain the entries associated with the rejected promises
210-
— i.e. the successful entries will be removed.
184+
If the payload is an array, then _optionally_ your task executor may choose to
185+
return an array of the same length, the entries in which are promises. Should
186+
any of these promises reject, then the job will be re-enqueued, but the payload
187+
will be overwritten to only contain the entries associated with the rejected
188+
promises — i.e. the successful entries will be removed.
211189

212190
## `helpers`
213191

192+
### `helpers.abortPromise`
193+
194+
This is a promise that will reject when [`abortSignal`](#helpersabortsignal)
195+
aborts. This makes it convenient for exiting your task when the abortSignal
196+
fires: `Promise.race([abortPromise, doYourAsyncThing()])`.
197+
198+
### `helpers.abortSignal`
199+
200+
This is a Node `AbortSignal`. Use this to be notified that you should abort your
201+
task early due to a graceful shutdown request. `AbortSignal`s can be passed to a
202+
number of asynchronous Node.js methods like
203+
[`http.request()`](https://nodejs.org/api/http.html#httprequesturl-options-callback).
204+
205+
### `helpers.addJob()`
206+
207+
See [`addJob`](/library/add-job.md).
208+
209+
### `helpers.addJobs()`
210+
211+
See [`addJobs`](/library/add-job.md#add-jobs).
212+
213+
### `helpers.getQueueName()`
214+
215+
Get the name of the queue the job is in, or the queue name of the provided queue
216+
ID. This function may or may not return a promise. We recommend that you always
217+
`await` it.
218+
219+
### `helpers.job`
220+
221+
The whole job, including `uuid`, `attempts`, etc.
222+
214223
### `helpers.logger`
215224

216-
So that you may redirect logs to your preferred logging provider, we have
217-
enabled you to supply your own logging provider. Overriding this is currently
218-
only available in library mode (see [Logger](/library/logger.md)). We then wrap
219-
this logging provider with a helper class to ease debugging; the helper class
220-
has the following methods:
225+
See [Logger](./library/logger)
226+
227+
### `helpers.query()`
221228

222-
- `error(message, meta?)`: for logging errors, similar to `console.error`
223-
- `warn(message, meta?)`: for logging warnings, similar to `console.warn`
224-
- `info(message, meta?)`: for logging informational messages, similar to
225-
`console.info`
226-
- `debug(message, meta?)`: to aid with debugging, similar to `console.log`
227-
- `scope(additionalScope)`: returns a new `Logger` instance with additional
228-
scope information
229+
This is a convenience wrapper for
230+
`withPgClient(pgClient => pgClient.query(sql, values))`. See
231+
[`withPgClient()`](#helperswithpgclient)
229232

230233
### `helpers.withPgClient()`
231234

@@ -249,19 +252,3 @@ keeping transactions open may decrease Graphile Worker's performance due to
249252
increasing contention over the pool of database clients.
250253

251254
:::
252-
253-
### `helpers.addJob()`
254-
255-
```ts
256-
await helpers.addJob(identifier, payload, options);
257-
```
258-
259-
See [`addJob`](/library/add-job.md).
260-
261-
### `helpers.addJobs()`
262-
263-
```ts
264-
await helpers.addJobs(specs, preserveRunAt);
265-
```
266-
267-
See [`addJobs`](/library/add-job.md#add-jobs).

0 commit comments

Comments
 (0)