@workflow/world-postgres 5.0.0-beta.9 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +305 -34
- package/bin/setup-deprecated.js +20 -0
- package/dist/config.d.ts +14 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +1 -1
- package/dist/drizzle/schema.d.ts +375 -5
- package/dist/drizzle/schema.d.ts.map +1 -1
- package/dist/drizzle/schema.js +91 -14
- package/dist/executor.d.ts +8 -0
- package/dist/executor.d.ts.map +1 -0
- package/dist/executor.js +79 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +34 -12
- package/dist/invocation-notifications.d.ts +23 -0
- package/dist/invocation-notifications.d.ts.map +1 -0
- package/dist/invocation-notifications.js +156 -0
- package/dist/invocation-retention.d.ts +11 -0
- package/dist/invocation-retention.d.ts.map +1 -0
- package/dist/invocation-retention.js +14 -0
- package/dist/invocations.d.ts +24 -0
- package/dist/invocations.d.ts.map +1 -0
- package/dist/invocations.js +208 -0
- package/dist/message.d.ts +1 -0
- package/dist/message.d.ts.map +1 -1
- package/dist/message.js +5 -3
- package/dist/queue.d.ts +32 -8
- package/dist/queue.d.ts.map +1 -1
- package/dist/queue.js +467 -96
- package/dist/retention.d.ts +66 -0
- package/dist/retention.d.ts.map +1 -0
- package/dist/retention.js +152 -0
- package/dist/run-status.d.ts +54 -0
- package/dist/run-status.d.ts.map +1 -0
- package/dist/run-status.js +138 -0
- package/dist/storage.d.ts +8 -1
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +1613 -319
- package/dist/streamer.d.ts.map +1 -1
- package/dist/streamer.js +109 -41
- package/dist/zod.d.ts.map +1 -1
- package/dist/zod.js +3 -3
- package/package.json +17 -14
- package/src/drizzle/migrations/0014_add_attr_set_event_unique_index.sql +5 -0
- package/src/drizzle/migrations/0015_move_enums_to_workflow_schema.sql +75 -0
- package/src/drizzle/migrations/0016_add_encryption_public_key.sql +1 -0
- package/src/drizzle/migrations/0017_add_hook_resume_context.sql +1 -0
- package/src/drizzle/migrations/0018_add_hook_token_retention.sql +1 -0
- package/src/drizzle/migrations/0019_add_event_slots.sql +74 -0
- package/src/drizzle/migrations/0020_add_invocations.sql +13 -0
- package/src/drizzle/migrations/0021_invocation_identity_retention.sql +19 -0
- package/src/drizzle/migrations/0022_invocation_outcomes.sql +3 -0
- package/src/drizzle/migrations/0023_add_hook_claimed_from.sql +1 -0
- package/src/drizzle/migrations/0024_add_dynamic_workflow_code.sql +1 -0
- package/src/drizzle/migrations/meta/_journal.json +77 -0
package/README.md
CHANGED
|
@@ -1,6 +1,93 @@
|
|
|
1
1
|
# @workflow/world-postgres
|
|
2
2
|
|
|
3
|
-
An embedded worker
|
|
3
|
+
An embedded worker and workflow system backed by PostgreSQL for multi-host self-hosted solutions.
|
|
4
|
+
|
|
5
|
+
> **This is a reference implementation.** It shows how to implement the World
|
|
6
|
+
> interface on top of a database and a queue, and it is a reasonable starting
|
|
7
|
+
> point for self-hosting. It is not a managed backend: it ships **without
|
|
8
|
+
> authentication**, and it is not tuned for the performance or scale properties
|
|
9
|
+
> of one. A production deployment typically runs workers in separate processes
|
|
10
|
+
> with a more robust queuing system, and **must** put its own authentication in
|
|
11
|
+
> front of the workflow HTTP routes. Read [Security](#security) before you
|
|
12
|
+
> deploy it.
|
|
13
|
+
>
|
|
14
|
+
> While some customers have been successful in deploying the Postgres World as
|
|
15
|
+
> is, for production use-cases, we highly recommend cloning this reference
|
|
16
|
+
> implementation and adapting it to your persistence, network stack, scale and
|
|
17
|
+
> security requirements.
|
|
18
|
+
|
|
19
|
+
## Security
|
|
20
|
+
|
|
21
|
+
The Postgres World does **not** automatically authenticate the requests that
|
|
22
|
+
drive workflow execution. Adding that is your responsibility, and it should be
|
|
23
|
+
in place before an app using this World is reachable by untrusted clients.
|
|
24
|
+
|
|
25
|
+
### Protect the queue route
|
|
26
|
+
|
|
27
|
+
`POST /.well-known/workflow/v1/flow` is where the worker delivers workflow
|
|
28
|
+
orchestration and queued step invocations. It is mounted in your application
|
|
29
|
+
like any other route, so it is publicly reachable by default, and the queue
|
|
30
|
+
handler, inherited from
|
|
31
|
+
[`@workflow/world-local`](https://github.com/vercel/workflow/tree/main/packages/world-local),
|
|
32
|
+
accepts any request whose `x-vqs-*` headers and queue-name prefix are
|
|
33
|
+
well-formed, leaving the payload itself to be validated later by the runtime
|
|
34
|
+
that consumes the message. There is no signature, shared secret, or caller check,
|
|
35
|
+
so anyone who can reach the route can forge or replay a workflow or step
|
|
36
|
+
invocation, including steps your application would only reach after its own
|
|
37
|
+
gating. Restrict it before you expose the app.
|
|
38
|
+
|
|
39
|
+
The other routes under `/.well-known/workflow/v1/` differ:
|
|
40
|
+
|
|
41
|
+
- `webhook/:token`, created by
|
|
42
|
+
[`createWebhook()`](https://workflow-sdk.dev/docs/api-reference/workflow/create-webhook),
|
|
43
|
+
is authorized by the token in the URL and nothing else. Use
|
|
44
|
+
[`createHook()`](https://workflow-sdk.dev/docs/api-reference/workflow/create-hook)
|
|
45
|
+
behind your own authenticated route and
|
|
46
|
+
[`resumeHook()`](https://workflow-sdk.dev/docs/api-reference/workflow-api/resume-hook)
|
|
47
|
+
when you need more than that.
|
|
48
|
+
- `manifest.json` responds with `404` unless the app was built with
|
|
49
|
+
`WORKFLOW_PUBLIC_MANIFEST=1`. That variable is read at build time, so
|
|
50
|
+
unsetting it in the runtime environment of an already-built deployment does
|
|
51
|
+
not withdraw the manifest. Leave it unset outside of testing, because the
|
|
52
|
+
manifest lists your workflow and step names.
|
|
53
|
+
|
|
54
|
+
### Bringing your own auth
|
|
55
|
+
|
|
56
|
+
Workflow does not prescribe an auth mechanism, so gate the flow route at the
|
|
57
|
+
network edge rather than inside the application:
|
|
58
|
+
|
|
59
|
+
- **Keep the flow route unreachable from outside.** By default the worker
|
|
60
|
+
delivers to a loopback address (`http://localhost:{PORT}`, or
|
|
61
|
+
`WORKFLOW_LOCAL_BASE_URL` when set), so in the common single-process topology
|
|
62
|
+
nothing outside the container needs to reach it. Blocking external requests to
|
|
63
|
+
`/.well-known/workflow/v1/flow` at your ingress, reverse proxy, or firewall
|
|
64
|
+
costs you nothing, because loopback delivery never traverses that layer. Do
|
|
65
|
+
not block the whole `/.well-known/workflow/` prefix if you use
|
|
66
|
+
`createWebhook()`: its `webhook/:token` route sits under the same prefix and
|
|
67
|
+
has to stay reachable by whoever calls it.
|
|
68
|
+
- **Authenticate at the proxy when the routes must cross hosts.** If your web
|
|
69
|
+
tier and workers are separate deployments, require mTLS or a shared-secret
|
|
70
|
+
header at the proxy in front of the application, and strip any client-supplied
|
|
71
|
+
copy of that header at the edge.
|
|
72
|
+
- **Do not gate these paths in framework middleware.** The setup guides tell you
|
|
73
|
+
to exclude `/.well-known/workflow/*` from your Next.js proxy matcher, because a
|
|
74
|
+
handler that consumes the internal request body breaks execution. Adding the
|
|
75
|
+
paths back in order to gate them reintroduces that failure mode.
|
|
76
|
+
- **Do not expect the World to present a credential.** It does not sign its
|
|
77
|
+
delivery requests or attach a secret to them, so an in-application check that
|
|
78
|
+
requires one will reject the World's own deliveries.
|
|
79
|
+
|
|
80
|
+
### Data at rest
|
|
81
|
+
|
|
82
|
+
This World does not currently implement `getEncryptionKeyForRun()`, so it does
|
|
83
|
+
not participate in Workflow's
|
|
84
|
+
[end-to-end encryption](https://workflow-sdk.dev/docs/how-it-works/encryption):
|
|
85
|
+
workflow and step inputs and return values, hook payloads and metadata, and
|
|
86
|
+
stream chunks are all stored in your database in readable form. A World derived
|
|
87
|
+
from this one can opt in by implementing that single method (see
|
|
88
|
+
[Custom World implementations](https://workflow-sdk.dev/docs/how-it-works/encryption#custom-world-implementations)),
|
|
89
|
+
which is the recommended route if your workflows carry sensitive data. Until
|
|
90
|
+
then, protect the database, its credentials, and its backups accordingly.
|
|
4
91
|
|
|
5
92
|
## Installation
|
|
6
93
|
|
|
@@ -14,9 +101,9 @@ yarn add @workflow/world-postgres
|
|
|
14
101
|
|
|
15
102
|
## Usage
|
|
16
103
|
|
|
17
|
-
### Basic
|
|
104
|
+
### Basic setup
|
|
18
105
|
|
|
19
|
-
The
|
|
106
|
+
The PostgreSQL World can be configured by setting the `WORKFLOW_TARGET_WORLD` environment variable to the package name:
|
|
20
107
|
|
|
21
108
|
```bash
|
|
22
109
|
export WORKFLOW_TARGET_WORLD="@workflow/world-postgres"
|
|
@@ -38,9 +125,15 @@ export WORKFLOW_POSTGRES_WORKER_CONCURRENCY="10"
|
|
|
38
125
|
|
|
39
126
|
# Optional: Internal pg.Pool max size (default: 10)
|
|
40
127
|
export WORKFLOW_POSTGRES_MAX_POOL_SIZE="10"
|
|
128
|
+
|
|
129
|
+
# Optional: Let the application coordinate shutdown (default: false)
|
|
130
|
+
export WORKFLOW_POSTGRES_APPLICATION_MANAGED_SHUTDOWN="1"
|
|
131
|
+
|
|
132
|
+
# Optional: Maximum Hook minimum retention in days (default: 30)
|
|
133
|
+
export WORKFLOW_POSTGRES_HOOK_RETENTION_LIMIT_DAYS="30"
|
|
41
134
|
```
|
|
42
135
|
|
|
43
|
-
### Programmatic
|
|
136
|
+
### Programmatic usage
|
|
44
137
|
|
|
45
138
|
You can also create a PostgreSQL world directly in your code:
|
|
46
139
|
|
|
@@ -61,62 +154,94 @@ const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
|
61
154
|
const worldFromPool = createWorld({ pool });
|
|
62
155
|
```
|
|
63
156
|
|
|
64
|
-
|
|
157
|
+
### Application-managed shutdown
|
|
158
|
+
|
|
159
|
+
By default, Graphile Worker responds automatically when the application is asked to shut down. If your application already coordinates shutdown, set `WORKFLOW_POSTGRES_APPLICATION_MANAGED_SHUTDOWN=1` when selecting the package with `WORKFLOW_TARGET_WORLD`, or set `applicationManagedShutdown: true` when calling `createWorld()` directly. Await `world.close()` from your shutdown path so Graphile Worker cannot terminate the process as soon as its queue stops, before your application finishes closing dependent resources:
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
import { createWorld } from '@workflow/world-postgres';
|
|
163
|
+
|
|
164
|
+
const world = createWorld({
|
|
165
|
+
connectionString: process.env.DATABASE_URL!,
|
|
166
|
+
applicationManagedShutdown: true,
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
await world.start();
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Use this option only when your application or framework has its own shutdown hook. Handle cleanup errors there and await `world.close()` first, then close the workflow HTTP server and any caller-owned `pg.Pool`.
|
|
173
|
+
|
|
174
|
+
Closing the world stops the queue from accepting new jobs and waits for active jobs. After Graphile Worker's graceful-shutdown timeout (5s by default), it aborts any workflow HTTP request that is still pending. Graphile Worker then unlocks the same row through its normal failure handling. Graphile counts a delivery attempt when it claims the row, so the aborted delivery consumes that attempt and is retried only if its Graphile attempt budget remains. A one-attempt or final-attempt job is unlocked but not retried. The shutdown handler does not create a replacement row.
|
|
175
|
+
|
|
176
|
+
An aborted HTTP request does not guarantee that its server-side handler stopped, so workflow and step handlers must continue to tolerate at-least-once execution. Keep the workflow HTTP routes and any caller-owned pool available until `world.close()` resolves.
|
|
177
|
+
|
|
178
|
+
## Configuration options
|
|
65
179
|
|
|
66
180
|
| Option | Type | Default | Description |
|
|
67
181
|
| ------------------ | --------- | -------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
|
|
68
|
-
| `connectionString` | `string` | `process.env.WORKFLOW_POSTGRES_URL` or `'postgres://world:world@localhost:5432/world'` | Used only when `pool` is omitted, to construct an internal pool |
|
|
182
|
+
| `connectionString` | `string` | `process.env.WORKFLOW_POSTGRES_URL`, `process.env.DATABASE_URL`, or `'postgres://world:world@localhost:5432/world'` | Used only when `pool` is omitted, to construct an internal pool |
|
|
69
183
|
| `maxPoolSize` | `number` | `process.env.WORKFLOW_POSTGRES_MAX_POOL_SIZE` or `pg.Pool` default (`10`) | Optional. Sets the internal `pg.Pool` max size when `createWorld()` creates the pool |
|
|
70
|
-
| `pool` | `pg.Pool` |
|
|
184
|
+
| `pool` | `pg.Pool` | Not applicable | Optional. When set, used for Drizzle, Graphile Worker, and stream writes. `world.close()` does not end it. |
|
|
71
185
|
| `jobPrefix` | `string` | `process.env.WORKFLOW_POSTGRES_JOB_PREFIX` | Optional prefix for queue job names |
|
|
72
|
-
| `queueConcurrency` | `number` | `50` | Number of concurrent active step executions per process. Must be high enough to cover any parent→child workflow polling in flight
|
|
186
|
+
| `queueConcurrency` | `number` | `50` | Number of concurrent active step executions per process. Must be high enough to cover any parent→child workflow polling in flight because each `Run#returnValue` await holds a worker slot until the child run terminates. |
|
|
187
|
+
| `applicationManagedShutdown` | `boolean` | `false`; `WORKFLOW_POSTGRES_APPLICATION_MANAGED_SHUTDOWN=1` enables it for the default package configuration | Whether the application coordinates shutdown and awaits `world.close()` instead of Graphile Worker responding automatically. |
|
|
73
188
|
|
|
74
|
-
## Environment
|
|
189
|
+
## Environment variables
|
|
75
190
|
|
|
76
191
|
| Variable | Description | Default |
|
|
77
192
|
| -------------------------------------- | ------------------------------------------------------------ | ----------------------------------------------- |
|
|
78
193
|
| `WORKFLOW_TARGET_WORLD` | Set to `"@workflow/world-postgres"` to use this world | - |
|
|
79
|
-
| `WORKFLOW_POSTGRES_URL` | PostgreSQL connection string | `'postgres://world:world@localhost:5432/world'` |
|
|
194
|
+
| `WORKFLOW_POSTGRES_URL` | PostgreSQL connection string | `DATABASE_URL` or `'postgres://world:world@localhost:5432/world'` |
|
|
80
195
|
| `WORKFLOW_POSTGRES_JOB_PREFIX` | Prefix for queue job names | - |
|
|
81
196
|
| `WORKFLOW_POSTGRES_WORKER_CONCURRENCY` | Number of concurrent workers | `50` |
|
|
82
197
|
| `WORKFLOW_POSTGRES_MAX_POOL_SIZE` | Internal `pg.Pool` max size | `10` |
|
|
198
|
+
| `WORKFLOW_POSTGRES_APPLICATION_MANAGED_SHUTDOWN` | Set to `1` when the application coordinates shutdown and awaits `world.close()` | unset (`false`) |
|
|
199
|
+
| `WORKFLOW_POSTGRES_HOOK_RETENTION_LIMIT_DAYS` | Maximum Hook minimum retention in days | `30` |
|
|
83
200
|
|
|
84
201
|
When `pool` is omitted, `maxPoolSize` precedence is: `createWorld({ maxPoolSize })`, then `WORKFLOW_POSTGRES_MAX_POOL_SIZE`, then the `pg.Pool` default.
|
|
85
202
|
|
|
86
203
|
For higher worker concurrency, Graphile Worker recommends setting `maxPoolSize` to `10` or `queueConcurrency + 2`, whichever is larger.
|
|
87
204
|
|
|
88
|
-
## Database
|
|
205
|
+
## Database setup
|
|
89
206
|
|
|
90
207
|
This package uses PostgreSQL with the following components:
|
|
91
208
|
|
|
92
|
-
- **
|
|
209
|
+
- **Graphile Worker**: For queue processing and job management
|
|
93
210
|
- **Drizzle ORM**: For database operations and schema management
|
|
94
211
|
- **pg** (node-postgres): For PostgreSQL client connections. Drizzle and Graphile Worker share a `pg.Pool`, while LISTEN uses a dedicated `pg.Client` created from the same connection options.
|
|
95
212
|
|
|
96
|
-
### Quick
|
|
213
|
+
### Quick setup with CLI
|
|
97
214
|
|
|
98
|
-
|
|
215
|
+
Set up your database with the included CLI tool:
|
|
99
216
|
|
|
100
217
|
```bash
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
218
|
+
# npm
|
|
219
|
+
npx --package=@workflow/world-postgres bootstrap
|
|
220
|
+
|
|
221
|
+
# pnpm
|
|
222
|
+
pnpm dlx --package @workflow/world-postgres bootstrap
|
|
223
|
+
|
|
224
|
+
# Yarn
|
|
225
|
+
yarn dlx --package @workflow/world-postgres bootstrap
|
|
226
|
+
|
|
227
|
+
# Bun
|
|
228
|
+
bunx --package @workflow/world-postgres bootstrap
|
|
104
229
|
```
|
|
105
230
|
|
|
106
|
-
The CLI
|
|
231
|
+
The CLI and runtime World automatically load the connection string from:
|
|
107
232
|
1. `WORKFLOW_POSTGRES_URL` environment variable
|
|
108
233
|
2. `DATABASE_URL` environment variable
|
|
109
234
|
3. Default: `postgres://world:world@localhost:5432/world`
|
|
110
235
|
|
|
111
|
-
### Database
|
|
236
|
+
### Database schema
|
|
112
237
|
|
|
113
238
|
The setup creates the following tables:
|
|
114
239
|
|
|
115
|
-
- `workflow_runs
|
|
116
|
-
- `workflow_events
|
|
117
|
-
- `workflow_steps
|
|
118
|
-
- `workflow_hooks
|
|
119
|
-
- `workflow_stream_chunks
|
|
240
|
+
- `workflow_runs`: Stores workflow execution runs
|
|
241
|
+
- `workflow_events`: Stores workflow events
|
|
242
|
+
- `workflow_steps`: Stores individual workflow steps
|
|
243
|
+
- `workflow_hooks`: Stores webhook hooks
|
|
244
|
+
- `workflow_stream_chunks`: Stores streaming data chunks
|
|
120
245
|
|
|
121
246
|
You can also access the schema programmatically:
|
|
122
247
|
|
|
@@ -128,22 +253,168 @@ import * as schema from '@workflow/world-postgres/schema';
|
|
|
128
253
|
|
|
129
254
|
Make sure your PostgreSQL database is accessible and the user has sufficient permissions to create tables and manage jobs.
|
|
130
255
|
|
|
256
|
+
### Data retention
|
|
257
|
+
|
|
258
|
+
Postgres World does not yet perform general workflow-run cleanup. After a
|
|
259
|
+
retained Hook's run ends and its deadline passes, reads treat the Hook as absent
|
|
260
|
+
and its token can be reused. If the token is never reused, the expired
|
|
261
|
+
`workflow_hooks` row remains.
|
|
262
|
+
|
|
131
263
|
## Features
|
|
132
264
|
|
|
133
|
-
- **Durable
|
|
134
|
-
- **Queue
|
|
135
|
-
- **Durable
|
|
265
|
+
- **Durable storage**: Stores workflow runs, events, steps, hooks, and webhooks in PostgreSQL
|
|
266
|
+
- **Queue processing**: Uses Graphile Worker as the durable queue and executes jobs over the workflow HTTP routes
|
|
267
|
+
- **Durable delays**: Reschedules waits and retries in PostgreSQL
|
|
136
268
|
- **Streaming**: Real-time event streaming capabilities
|
|
137
|
-
- **Health
|
|
138
|
-
- **Configurable
|
|
139
|
-
|
|
140
|
-
## Queue Behavior
|
|
269
|
+
- **Health checks**: Built-in connection health monitoring
|
|
270
|
+
- **Configurable concurrency**: Adjustable worker concurrency for queue processing
|
|
141
271
|
|
|
272
|
+
## Queue behavior
|
|
142
273
|
|
|
143
|
-
- Graphile jobs are acknowledged only after
|
|
274
|
+
- Graphile jobs are acknowledged only after execution finishes, or after the worker durably schedules a delayed follow-up job
|
|
144
275
|
- Backlog stays in PostgreSQL when all execution slots are busy
|
|
145
276
|
- Retry and sleep-style delays use Graphile `runAt` scheduling
|
|
146
|
-
- Workflow and step execution
|
|
277
|
+
- Workflow orchestration and queued step execution are both sent through `/.well-known/workflow/v1/flow`
|
|
278
|
+
|
|
279
|
+
### Experimental synchronous invocation
|
|
280
|
+
|
|
281
|
+
Enable synchronous hook-input delivery with `WORKFLOW_POSTGRES_INVOKE=1` when
|
|
282
|
+
using `WORKFLOW_TARGET_WORLD`, or pass `enableInvoke: true` to `createWorld()`.
|
|
283
|
+
Invocation is off by default.
|
|
284
|
+
|
|
285
|
+
Apply the database migrations before running the upgraded World, including when
|
|
286
|
+
invocation is disabled, because hook deduplication and data purging use new
|
|
287
|
+
columns. Upgrade producers and workers together, using matching versions and the
|
|
288
|
+
same job prefix and namespace. Migration 0022 versions stored responses so values
|
|
289
|
+
from earlier previews keep their original meaning.
|
|
290
|
+
|
|
291
|
+
With invocation enabled, the World advertises `capabilities.invoke`.
|
|
292
|
+
`resumeHook()` sends the hook input to the run's executor, which validates the
|
|
293
|
+
input, writes the hook event, and responds. The response confirms that processing
|
|
294
|
+
has finished; workflow user code may consume the event later.
|
|
295
|
+
|
|
296
|
+
The World calls the SDK handler with `{ runId, invoke: true, requestId, input }`
|
|
297
|
+
and stores its response as an `InvocationOutcome`. `invoke()` returns the value
|
|
298
|
+
or throws the restored Workflow error with its diagnostic fields. Invocation
|
|
299
|
+
results are response data, including any `timeoutSeconds` property.
|
|
300
|
+
|
|
301
|
+
A terminal error, such as a missing hook or an input-identity conflict, is stored
|
|
302
|
+
as the request's outcome. Retrying that identity returns the stored error while
|
|
303
|
+
the result is retained. Transient or unrecognized failures leave the input pending
|
|
304
|
+
and cause Graphile to retry the workflow execution job. Failure to store or read
|
|
305
|
+
a response leaves the caller's outcome unknown. Earlier event writes may have
|
|
306
|
+
committed in either case.
|
|
307
|
+
|
|
308
|
+
Postgres stores inputs and responses in `workflow.workflow_invocations`. The
|
|
309
|
+
input is inserted into `workflow_invocations` and a request is enqueued in
|
|
310
|
+
Graphile in the same transaction. Retrying a retained input also enqueues a
|
|
311
|
+
workflow execution request, even when its response is already stored. This lets
|
|
312
|
+
the runner check committed events if a previous runner stopped after responding
|
|
313
|
+
but before replaying them.
|
|
314
|
+
|
|
315
|
+
Reuse an `idempotencyKey` with the same payload when retrying an invocation.
|
|
316
|
+
Without a key, each call creates a new input.
|
|
317
|
+
|
|
318
|
+
With invocation disabled (the default), deliveries for the same run may execute
|
|
319
|
+
concurrently. A workflow request can execute a step inline and wait for a hook
|
|
320
|
+
wake to abort that step, so the queue cannot hold a per-run lock for the entire
|
|
321
|
+
request. Each queue instance coalesces in-flight and recently completed deliveries
|
|
322
|
+
with the same idempotency key. Handlers must still tolerate at-least-once delivery
|
|
323
|
+
across worker processes, restarts, or completed-message cache eviction.
|
|
324
|
+
|
|
325
|
+
With invocation enabled, each Graphile worker pool registers two task
|
|
326
|
+
identifiers. A task identifier selects a handler. A named queue controls which
|
|
327
|
+
jobs can execute concurrently. The default job prefix produces these names:
|
|
328
|
+
|
|
329
|
+
| Task identifier | Work | Named queue |
|
|
330
|
+
| --- | --- | --- |
|
|
331
|
+
| `workflow_flows_executor` | Start or resume workflow execution | `workflow_flows:<runId>:executor`, one queue per run |
|
|
332
|
+
| `workflow_flows` | Step execution and health checks | No run-scoped named queue |
|
|
333
|
+
|
|
334
|
+
Graphile permits one active job per run's named queue across worker processes.
|
|
335
|
+
Different runs, step jobs, and health checks can execute concurrently.
|
|
336
|
+
`queueConcurrency` limits the total active Graphile jobs per worker process
|
|
337
|
+
across both task identifiers and defaults to **50**.
|
|
338
|
+
|
|
339
|
+
While a workflow execution job is active, the World passes pending invocation
|
|
340
|
+
inputs to the SDK handler, including while inline steps wait. After an execution
|
|
341
|
+
returns, the World checks for idle time or an expired 120s input-intake budget.
|
|
342
|
+
Before acknowledging the job, the World stops reading new inputs, finishes any
|
|
343
|
+
input already being processed, and replays events committed since the previous
|
|
344
|
+
replay began. A later job can resume the run in another process.
|
|
345
|
+
|
|
346
|
+
A workflow execution request is the HTTP request sent by a Graphile
|
|
347
|
+
`workflow_flows_executor` job to start or resume a run. An invocation is an input
|
|
348
|
+
submitted through `world.invoke()` and stored in `workflow_invocations`. One
|
|
349
|
+
workflow execution request can process several invocations.
|
|
350
|
+
|
|
351
|
+
Before processing pending inputs, the HTTP receiver verifies the execution
|
|
352
|
+
request against Graphile's `jobs` view. The worker supplies the job ID, worker ID,
|
|
353
|
+
and attempt through private HTTP headers. The receiver requires a locked job
|
|
354
|
+
with matching metadata, the executor task identifier, and the run's named queue.
|
|
355
|
+
Application-supplied headers cannot replace this metadata. Step jobs and health
|
|
356
|
+
checks process their own work without reading pending invocation rows.
|
|
357
|
+
|
|
358
|
+
Updated workers move legacy orchestration jobs to the executor task before
|
|
359
|
+
acknowledging them, preserving message identity and the remaining attempt budget.
|
|
360
|
+
Updated HTTP receivers also reroute unmarked legacy requests. Older workers
|
|
361
|
+
cannot enforce these checks, so upgrade participating workers together.
|
|
362
|
+
|
|
363
|
+
Verification happens when the request enters the receiver. A handler that keeps
|
|
364
|
+
running after Graphile reclaims its job can still write events; this
|
|
365
|
+
implementation does not fence those writes.
|
|
366
|
+
|
|
367
|
+
The Postgres World reads pending inputs in pages of 32. One dedicated
|
|
368
|
+
`LISTEN/NOTIFY` connection per World instance signals new inputs and responses.
|
|
369
|
+
The connection opens when needed. Notifications contain hashed identifiers;
|
|
370
|
+
readers fetch payloads and results from the table.
|
|
371
|
+
|
|
372
|
+
Notifications commit with their associated writes. Readers track changes across
|
|
373
|
+
each query and reread after subscribing or reconnecting, covering writes that
|
|
374
|
+
arrived before the subscription became active.
|
|
375
|
+
|
|
376
|
+
Use a database connection that supports sessions for `LISTEN`. Transaction
|
|
377
|
+
pooling alone cannot maintain that subscription. When notifications are
|
|
378
|
+
unavailable, readers poll at 1s intervals and connection retries use a 1s backoff.
|
|
379
|
+
The World logs notification failure and recovery once per transition, excluding
|
|
380
|
+
payloads and connection details.
|
|
381
|
+
|
|
382
|
+
| Setting or limit | Value |
|
|
383
|
+
| --- | --- |
|
|
384
|
+
| Default response-wait timeout | 30s, configurable with `timeoutMs` |
|
|
385
|
+
| Maximum encoded input size | 1 MiB |
|
|
386
|
+
| Maximum encoded outcome size | 1 MiB |
|
|
387
|
+
| Pending-input page size | 32 rows |
|
|
388
|
+
|
|
389
|
+
A timeout ends the caller's wait without canceling processing or establishing
|
|
390
|
+
its outcome. Closing the World aborts local response waits and closes input
|
|
391
|
+
readers and the notification connection.
|
|
392
|
+
|
|
393
|
+
Postgres implements `hookResumeDedup` with a durable resume ID and payload digest.
|
|
394
|
+
Retrying the same identity reuses the committed hook event, including after hook
|
|
395
|
+
disposal or run completion. Reusing the identity with a different payload is
|
|
396
|
+
rejected.
|
|
397
|
+
|
|
398
|
+
The hook event and response are written in separate operations. If storing the
|
|
399
|
+
response fails, deduplication lets a retry recover the event's result. Step code
|
|
400
|
+
remains responsible for making its external side effects safe to retry.
|
|
401
|
+
|
|
402
|
+
`$retention: 0` purges invocation inputs, results, and fingerprints with the run's
|
|
403
|
+
other user data. An expiry marker lets waiting callers and retries receive a 410
|
|
404
|
+
error with code `INVOCATION_DATA_EXPIRED`. Writers lock and recheck the run so
|
|
405
|
+
late writes cannot restore purged data. A caller can receive this expiry error
|
|
406
|
+
after its hook event committed if the result expires before the caller reads it.
|
|
407
|
+
The migration also clears invocation data for runs that already expired or ended
|
|
408
|
+
with zero retention.
|
|
409
|
+
|
|
410
|
+
The experimental implementation has these remaining limitations:
|
|
411
|
+
|
|
412
|
+
- Results outside zero-retention purging have no automatic cleanup.
|
|
413
|
+
- A handler can continue writing after Graphile aborts or reclaims its job.
|
|
414
|
+
- Requests are not routed to a compatible deployment when workers run different code versions.
|
|
415
|
+
|
|
416
|
+
The database integration tests use the built core runtime. Build core before
|
|
417
|
+
running `pnpm exec vitest run test/invoke.test.ts` from `packages/world-postgres`.
|
|
147
418
|
|
|
148
419
|
## Development
|
|
149
420
|
|
|
@@ -176,7 +447,7 @@ pnpm build
|
|
|
176
447
|
pnpm test
|
|
177
448
|
```
|
|
178
449
|
|
|
179
|
-
## World
|
|
450
|
+
## World selection
|
|
180
451
|
|
|
181
452
|
To use the PostgreSQL world, set the `WORKFLOW_TARGET_WORLD` environment variable to the package name:
|
|
182
453
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
console.error(`The workflow-postgres-setup command has moved.
|
|
4
|
+
|
|
5
|
+
Use one of these commands instead:
|
|
6
|
+
|
|
7
|
+
# npm
|
|
8
|
+
npx --package=@workflow/world-postgres bootstrap
|
|
9
|
+
|
|
10
|
+
# pnpm
|
|
11
|
+
pnpm dlx --package @workflow/world-postgres bootstrap
|
|
12
|
+
|
|
13
|
+
# Yarn
|
|
14
|
+
yarn dlx --package @workflow/world-postgres bootstrap
|
|
15
|
+
|
|
16
|
+
# Bun
|
|
17
|
+
bunx --package @workflow/world-postgres bootstrap
|
|
18
|
+
`);
|
|
19
|
+
|
|
20
|
+
process.exit(1);
|
package/dist/config.d.ts
CHANGED
|
@@ -10,7 +10,21 @@ type PgConnectionConfig = {
|
|
|
10
10
|
};
|
|
11
11
|
export type PostgresWorldConfig = PgConnectionConfig & {
|
|
12
12
|
jobPrefix?: string;
|
|
13
|
+
/**
|
|
14
|
+
* namespace for queue topic prefixes (e.g. 'custom' → '__custom_wkf_workflow_').
|
|
15
|
+
* defaults to WORKFLOW_QUEUE_NAMESPACE env var if not provided.
|
|
16
|
+
*/
|
|
17
|
+
namespace?: string;
|
|
13
18
|
queueConcurrency?: number;
|
|
19
|
+
/** Enable experimental invoke() delivery with one Graphile execution queue per run. */
|
|
20
|
+
enableInvoke?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Whether the application coordinates shutdown instead of Graphile Worker
|
|
23
|
+
* responding automatically. The application must await world.close().
|
|
24
|
+
* Defaults to false. The package's default createWorld() configuration
|
|
25
|
+
* enables it when WORKFLOW_POSTGRES_APPLICATION_MANAGED_SHUTDOWN is `1`.
|
|
26
|
+
*/
|
|
27
|
+
applicationManagedShutdown?: boolean;
|
|
14
28
|
/**
|
|
15
29
|
* Override the flush interval (in ms) for buffered stream writes.
|
|
16
30
|
* Default is 10ms. Set to 0 for immediate flushing.
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAE/B,KAAK,kBAAkB,GACnB;IAAE,gBAAgB,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,SAAS,CAAA;CAAE,GACpE;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,gBAAgB,CAAC,EAAE,SAAS,CAAC;IAAC,WAAW,CAAC,EAAE,SAAS,CAAA;CAAE,CAAC;AAE1E,MAAM,MAAM,mBAAmB,GAAG,kBAAkB,GAAG;IACrD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;OAGG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAC;CAChC,CAAC"}
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAE/B,KAAK,kBAAkB,GACnB;IAAE,gBAAgB,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,SAAS,CAAA;CAAE,GACpE;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,gBAAgB,CAAC,EAAE,SAAS,CAAC;IAAC,WAAW,CAAC,EAAE,SAAS,CAAA;CAAE,CAAC;AAE1E,MAAM,MAAM,mBAAmB,GAAG,kBAAkB,GAAG;IACrD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,uFAAuF;IACvF,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;;;OAKG;IACH,0BAA0B,CAAC,EAAE,OAAO,CAAC;IACrC;;;OAGG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAC;CAChC,CAAC"}
|
package/dist/config.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export {};
|
|
2
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL2NvbmZpZy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IHR5cGUgeyBQb29sIH0gZnJvbSAncGcnO1xuXG50eXBlIFBnQ29ubmVjdGlvbkNvbmZpZyA9XG4gIHwgeyBjb25uZWN0aW9uU3RyaW5nOiBzdHJpbmc7IG1heFBvb2xTaXplPzogbnVtYmVyOyBwb29sPzogdW5kZWZpbmVkIH1cbiAgfCB7IHBvb2w6IFBvb2w7IGNvbm5lY3Rpb25TdHJpbmc/
|
|
2
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL2NvbmZpZy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IHR5cGUgeyBQb29sIH0gZnJvbSAncGcnO1xuXG50eXBlIFBnQ29ubmVjdGlvbkNvbmZpZyA9XG4gIHwgeyBjb25uZWN0aW9uU3RyaW5nOiBzdHJpbmc7IG1heFBvb2xTaXplPzogbnVtYmVyOyBwb29sPzogdW5kZWZpbmVkIH1cbiAgfCB7IHBvb2w6IFBvb2w7IGNvbm5lY3Rpb25TdHJpbmc/OiB1bmRlZmluZWQ7IG1heFBvb2xTaXplPzogdW5kZWZpbmVkIH07XG5cbmV4cG9ydCB0eXBlIFBvc3RncmVzV29ybGRDb25maWcgPSBQZ0Nvbm5lY3Rpb25Db25maWcgJiB7XG4gIGpvYlByZWZpeD86IHN0cmluZztcbiAgLyoqXG4gICAqIG5hbWVzcGFjZSBmb3IgcXVldWUgdG9waWMgcHJlZml4ZXMgKGUuZy4gJ2N1c3RvbScg4oaSICdfX2N1c3RvbV93a2Zfd29ya2Zsb3dfJykuXG4gICAqIGRlZmF1bHRzIHRvIFdPUktGTE9XX1FVRVVFX05BTUVTUEFDRSBlbnYgdmFyIGlmIG5vdCBwcm92aWRlZC5cbiAgICovXG4gIG5hbWVzcGFjZT86IHN0cmluZztcbiAgcXVldWVDb25jdXJyZW5jeT86IG51bWJlcjtcbiAgLyoqIEVuYWJsZSBleHBlcmltZW50YWwgaW52b2tlKCkgZGVsaXZlcnkgd2l0aCBvbmUgR3JhcGhpbGUgZXhlY3V0aW9uIHF1ZXVlIHBlciBydW4uICovXG4gIGVuYWJsZUludm9rZT86IGJvb2xlYW47XG4gIC8qKlxuICAgKiBXaGV0aGVyIHRoZSBhcHBsaWNhdGlvbiBjb29yZGluYXRlcyBzaHV0ZG93biBpbnN0ZWFkIG9mIEdyYXBoaWxlIFdvcmtlclxuICAgKiByZXNwb25kaW5nIGF1dG9tYXRpY2FsbHkuIFRoZSBhcHBsaWNhdGlvbiBtdXN0IGF3YWl0IHdvcmxkLmNsb3NlKCkuXG4gICAqIERlZmF1bHRzIHRvIGZhbHNlLiBUaGUgcGFja2FnZSdzIGRlZmF1bHQgY3JlYXRlV29ybGQoKSBjb25maWd1cmF0aW9uXG4gICAqIGVuYWJsZXMgaXQgd2hlbiBXT1JLRkxPV19QT1NUR1JFU19BUFBMSUNBVElPTl9NQU5BR0VEX1NIVVRET1dOIGlzIGAxYC5cbiAgICovXG4gIGFwcGxpY2F0aW9uTWFuYWdlZFNodXRkb3duPzogYm9vbGVhbjtcbiAgLyoqXG4gICAqIE92ZXJyaWRlIHRoZSBmbHVzaCBpbnRlcnZhbCAoaW4gbXMpIGZvciBidWZmZXJlZCBzdHJlYW0gd3JpdGVzLlxuICAgKiBEZWZhdWx0IGlzIDEwbXMuIFNldCB0byAwIGZvciBpbW1lZGlhdGUgZmx1c2hpbmcuXG4gICAqL1xuICBzdHJlYW1GbHVzaEludGVydmFsTXM/OiBudW1iZXI7XG59O1xuIl19
|