@workflow/world-postgres 5.0.4 → 5.1.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 +98 -3
- package/dist/config.d.ts +29 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +11 -2
- package/dist/connection-errors.d.ts +23 -0
- package/dist/connection-errors.d.ts.map +1 -0
- package/dist/connection-errors.js +45 -0
- package/dist/drizzle/schema.d.ts +309 -88
- package/dist/drizzle/schema.d.ts.map +1 -1
- package/dist/drizzle/schema.js +106 -30
- package/dist/index.d.ts +7 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +71 -31
- package/dist/job-lease.d.ts +64 -0
- package/dist/job-lease.d.ts.map +1 -0
- package/dist/job-lease.js +317 -0
- package/dist/queue.d.ts +10 -3
- package/dist/queue.d.ts.map +1 -1
- package/dist/queue.js +106 -23
- package/dist/start-hook.d.ts +60 -0
- package/dist/start-hook.d.ts.map +1 -0
- package/dist/start-hook.js +220 -0
- package/dist/storage.d.ts +21 -1
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +729 -467
- package/package.json +7 -7
- package/src/drizzle/migrations/0027_timestamps_with_time_zone.sql +149 -0
- package/src/drizzle/migrations/0028_add_start_hook_admissions.sql +42 -0
- package/src/drizzle/migrations/0029_add_run_job_prefix.sql +4 -0
- package/src/drizzle/migrations/meta/_journal.json +21 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@workflow/world-postgres",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "A reference World implementation based on PostgreSQL",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -38,11 +38,11 @@
|
|
|
38
38
|
"./migrations/*.sql": "./src/drizzle/migrations/*.sql"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@vercel/queue": "0.
|
|
42
|
-
"@workflow/errors": "5.0
|
|
41
|
+
"@vercel/queue": "0.10.0",
|
|
42
|
+
"@workflow/errors": "5.1.0",
|
|
43
43
|
"@workflow/utils": "5.0.2",
|
|
44
|
-
"@workflow/world": "5.0
|
|
45
|
-
"@workflow/world-local": "5.
|
|
44
|
+
"@workflow/world": "5.1.0",
|
|
45
|
+
"@workflow/world-local": "5.2.0",
|
|
46
46
|
"cbor-x": "1.6.0",
|
|
47
47
|
"dotenv": "17.3.1",
|
|
48
48
|
"drizzle-orm": "0.45.2",
|
|
@@ -55,9 +55,9 @@
|
|
|
55
55
|
"@testcontainers/postgresql": "11.12.0",
|
|
56
56
|
"@types/node": "22.19.0",
|
|
57
57
|
"@types/pg": "8.20.0",
|
|
58
|
-
"@workflow/errors": "5.0
|
|
58
|
+
"@workflow/errors": "5.1.0",
|
|
59
59
|
"@workflow/tsconfig": "5.0.0",
|
|
60
|
-
"@workflow/world-testing": "5.0.
|
|
60
|
+
"@workflow/world-testing": "5.0.7",
|
|
61
61
|
"drizzle-kit": "0.31.9",
|
|
62
62
|
"typescript": "^6.0.3",
|
|
63
63
|
"undici": "7.30.0",
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
-- Store every timestamp as `timestamp with time zone`.
|
|
2
|
+
--
|
|
3
|
+
-- These columns were `timestamp without time zone`. Values written from
|
|
4
|
+
-- JavaScript (`started_at`, `completed_at`, `retry_after`, `resume_at`,
|
|
5
|
+
-- `expired_at`, and `updated_at` once a row is updated) arrive as ISO strings
|
|
6
|
+
-- whose `Z` Postgres ignored, so they hold UTC wall times. Values from
|
|
7
|
+
-- `DEFAULT now()` (every `created_at`, `updated_at` until the row's first
|
|
8
|
+
-- update) and from `now()` in SQL (`workflow_invocations.responded_at`) were
|
|
9
|
+
-- cast to the session's TimeZone, so they hold local wall times. The World
|
|
10
|
+
-- reads every value as UTC, so on a server outside UTC `createdAt` was off by
|
|
11
|
+
-- the server's offset.
|
|
12
|
+
--
|
|
13
|
+
-- 1. With the session in UTC, convert every column. Postgres 12+ does not
|
|
14
|
+
-- rewrite a table for this change while TimeZone is UTC, and each stored
|
|
15
|
+
-- value is taken as UTC, which is already right for every column written
|
|
16
|
+
-- from JavaScript.
|
|
17
|
+
-- 2. When the zone the defaults were written in is not UTC, move each value
|
|
18
|
+
-- that came from `now()` from that zone's wall time to the instant it
|
|
19
|
+
-- recorded. That zone is the session's TimeZone, or
|
|
20
|
+
-- `workflow.legacy_timezone` when set (for example
|
|
21
|
+
-- `options=-c workflow.legacy_timezone=Europe/Berlin` in the connection
|
|
22
|
+
-- string, or `ALTER DATABASE ... SET workflow.legacy_timezone = ...`), for
|
|
23
|
+
-- apps whose pool ran in a different session time zone than this migration.
|
|
24
|
+
-- Each value is moved by the offset that zone had on its own date, so rows
|
|
25
|
+
-- from winter and summer both come out right. The one exception is the hour
|
|
26
|
+
-- repeated when clocks go back (01:00-02:00 on the first Sunday of November
|
|
27
|
+
-- in America/Los_Angeles): a naive value from it is ambiguous and is taken
|
|
28
|
+
-- as standard time, so a row written in the first pass lands an hour late.
|
|
29
|
+
-- This rewrites every row it moves, about one rewrite of each table.
|
|
30
|
+
--
|
|
31
|
+
-- `workflow_invocations.expired_at` mixes `now()` with JavaScript dates and is
|
|
32
|
+
-- only ever checked for NULL, so its values are left as they are. Snapshot
|
|
33
|
+
-- `created_at` is always written from JavaScript.
|
|
34
|
+
--
|
|
35
|
+
-- The repair must run once: applied again it would move the repaired values
|
|
36
|
+
-- by the offset a second time. The drizzle migrator takes no lock, so two
|
|
37
|
+
-- `bootstrap` processes started together can both apply this migration. Lock
|
|
38
|
+
-- the tables first, then skip everything if the columns are already converted.
|
|
39
|
+
-- Waiting for the locks queues every other query on these tables behind this
|
|
40
|
+
-- one, so give up after 10 seconds (unless the session set its own
|
|
41
|
+
-- `lock_timeout`) and let `bootstrap` fail rather than stall the World.
|
|
42
|
+
DO $$
|
|
43
|
+
DECLARE
|
|
44
|
+
session_zone text := current_setting('TimeZone');
|
|
45
|
+
session_lock_timeout text := current_setting('lock_timeout');
|
|
46
|
+
legacy_zone text := coalesce(
|
|
47
|
+
nullif(current_setting('workflow.legacy_timezone', true), ''),
|
|
48
|
+
current_setting('TimeZone')
|
|
49
|
+
);
|
|
50
|
+
BEGIN
|
|
51
|
+
IF session_lock_timeout = '0' THEN
|
|
52
|
+
PERFORM set_config('lock_timeout', '10s', true);
|
|
53
|
+
END IF;
|
|
54
|
+
LOCK TABLE
|
|
55
|
+
"workflow"."workflow_runs",
|
|
56
|
+
"workflow"."workflow_events",
|
|
57
|
+
"workflow"."workflow_steps",
|
|
58
|
+
"workflow"."workflow_hooks",
|
|
59
|
+
"workflow"."workflow_waits",
|
|
60
|
+
"workflow"."workflow_invocations",
|
|
61
|
+
"workflow"."workflow_snapshots",
|
|
62
|
+
"workflow"."workflow_stream_chunks"
|
|
63
|
+
IN ACCESS EXCLUSIVE MODE;
|
|
64
|
+
PERFORM set_config('lock_timeout', session_lock_timeout, true);
|
|
65
|
+
IF (
|
|
66
|
+
SELECT atttypid FROM pg_attribute
|
|
67
|
+
WHERE attrelid = '"workflow"."workflow_runs"'::regclass
|
|
68
|
+
AND attname = 'created_at'
|
|
69
|
+
) = 'timestamptz'::regtype THEN
|
|
70
|
+
RETURN;
|
|
71
|
+
END IF;
|
|
72
|
+
|
|
73
|
+
PERFORM set_config('TimeZone', 'UTC', true);
|
|
74
|
+
|
|
75
|
+
ALTER TABLE "workflow"."workflow_runs"
|
|
76
|
+
ALTER COLUMN "created_at" TYPE timestamp with time zone,
|
|
77
|
+
ALTER COLUMN "updated_at" TYPE timestamp with time zone,
|
|
78
|
+
ALTER COLUMN "completed_at" TYPE timestamp with time zone,
|
|
79
|
+
ALTER COLUMN "started_at" TYPE timestamp with time zone,
|
|
80
|
+
ALTER COLUMN "expired_at" TYPE timestamp with time zone;
|
|
81
|
+
ALTER TABLE "workflow"."workflow_events"
|
|
82
|
+
ALTER COLUMN "created_at" TYPE timestamp with time zone;
|
|
83
|
+
ALTER TABLE "workflow"."workflow_steps"
|
|
84
|
+
ALTER COLUMN "started_at" TYPE timestamp with time zone,
|
|
85
|
+
ALTER COLUMN "completed_at" TYPE timestamp with time zone,
|
|
86
|
+
ALTER COLUMN "created_at" TYPE timestamp with time zone,
|
|
87
|
+
ALTER COLUMN "updated_at" TYPE timestamp with time zone,
|
|
88
|
+
ALTER COLUMN "retry_after" TYPE timestamp with time zone;
|
|
89
|
+
ALTER TABLE "workflow"."workflow_hooks"
|
|
90
|
+
ALTER COLUMN "created_at" TYPE timestamp with time zone;
|
|
91
|
+
ALTER TABLE "workflow"."workflow_waits"
|
|
92
|
+
ALTER COLUMN "resume_at" TYPE timestamp with time zone,
|
|
93
|
+
ALTER COLUMN "completed_at" TYPE timestamp with time zone,
|
|
94
|
+
ALTER COLUMN "created_at" TYPE timestamp with time zone,
|
|
95
|
+
ALTER COLUMN "updated_at" TYPE timestamp with time zone;
|
|
96
|
+
ALTER TABLE "workflow"."workflow_invocations"
|
|
97
|
+
ALTER COLUMN "created_at" TYPE timestamp with time zone,
|
|
98
|
+
ALTER COLUMN "responded_at" TYPE timestamp with time zone,
|
|
99
|
+
ALTER COLUMN "expired_at" TYPE timestamp with time zone;
|
|
100
|
+
ALTER TABLE "workflow"."workflow_snapshots"
|
|
101
|
+
ALTER COLUMN "created_at" TYPE timestamp with time zone;
|
|
102
|
+
ALTER TABLE "workflow"."workflow_stream_chunks"
|
|
103
|
+
ALTER COLUMN "created_at" TYPE timestamp with time zone;
|
|
104
|
+
|
|
105
|
+
-- Only a shortcut: in any zone with no offset the `WHERE` clauses below
|
|
106
|
+
-- match no rows, so a UTC alias missing from this list is still correct.
|
|
107
|
+
IF lower(legacy_zone) NOT IN (
|
|
108
|
+
'utc', 'etc/utc', 'uct', 'etc/uct', 'universal', 'etc/universal',
|
|
109
|
+
'zulu', 'etc/zulu', 'gmt', 'etc/gmt', 'gmt0', 'etc/gmt0', 'gmt+0',
|
|
110
|
+
'etc/gmt+0', 'gmt-0', 'etc/gmt-0', 'greenwich', 'etc/greenwich'
|
|
111
|
+
) THEN
|
|
112
|
+
-- An `updated_at` equal to `created_at` came from the same `now()` as it
|
|
113
|
+
-- (the row was never updated). Every other `updated_at` is from JavaScript.
|
|
114
|
+
UPDATE "workflow"."workflow_runs" SET
|
|
115
|
+
"created_at" = timezone(legacy_zone, timezone('UTC', "created_at")),
|
|
116
|
+
"updated_at" = CASE WHEN "updated_at" = "created_at"
|
|
117
|
+
THEN timezone(legacy_zone, timezone('UTC', "updated_at"))
|
|
118
|
+
ELSE "updated_at" END
|
|
119
|
+
WHERE timezone(legacy_zone, timezone('UTC', "created_at")) <> "created_at";
|
|
120
|
+
UPDATE "workflow"."workflow_steps" SET
|
|
121
|
+
"created_at" = timezone(legacy_zone, timezone('UTC', "created_at")),
|
|
122
|
+
"updated_at" = CASE WHEN "updated_at" = "created_at"
|
|
123
|
+
THEN timezone(legacy_zone, timezone('UTC', "updated_at"))
|
|
124
|
+
ELSE "updated_at" END
|
|
125
|
+
WHERE timezone(legacy_zone, timezone('UTC', "created_at")) <> "created_at";
|
|
126
|
+
UPDATE "workflow"."workflow_waits" SET
|
|
127
|
+
"created_at" = timezone(legacy_zone, timezone('UTC', "created_at")),
|
|
128
|
+
"updated_at" = CASE WHEN "updated_at" = "created_at"
|
|
129
|
+
THEN timezone(legacy_zone, timezone('UTC', "updated_at"))
|
|
130
|
+
ELSE "updated_at" END
|
|
131
|
+
WHERE timezone(legacy_zone, timezone('UTC', "created_at")) <> "created_at";
|
|
132
|
+
UPDATE "workflow"."workflow_events" SET
|
|
133
|
+
"created_at" = timezone(legacy_zone, timezone('UTC', "created_at"))
|
|
134
|
+
WHERE timezone(legacy_zone, timezone('UTC', "created_at")) <> "created_at";
|
|
135
|
+
UPDATE "workflow"."workflow_hooks" SET
|
|
136
|
+
"created_at" = timezone(legacy_zone, timezone('UTC', "created_at"))
|
|
137
|
+
WHERE timezone(legacy_zone, timezone('UTC', "created_at")) <> "created_at";
|
|
138
|
+
UPDATE "workflow"."workflow_stream_chunks" SET
|
|
139
|
+
"created_at" = timezone(legacy_zone, timezone('UTC', "created_at"))
|
|
140
|
+
WHERE timezone(legacy_zone, timezone('UTC', "created_at")) <> "created_at";
|
|
141
|
+
UPDATE "workflow"."workflow_invocations" SET
|
|
142
|
+
"created_at" = timezone(legacy_zone, timezone('UTC', "created_at")),
|
|
143
|
+
"responded_at" = timezone(legacy_zone, timezone('UTC', "responded_at"))
|
|
144
|
+
WHERE timezone(legacy_zone, timezone('UTC', "created_at")) <> "created_at"
|
|
145
|
+
OR timezone(legacy_zone, timezone('UTC', "responded_at")) <> "responded_at";
|
|
146
|
+
END IF;
|
|
147
|
+
|
|
148
|
+
PERFORM set_config('TimeZone', session_zone, true);
|
|
149
|
+
END $$;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
-- Atomic `start({ hook })` admission: one immutable accept-or-reject decision
|
|
2
|
+
-- per candidate run, written in the transaction that creates an accepted run.
|
|
3
|
+
-- Rows older than a candidate's 24h window are deleted as admissions go.
|
|
4
|
+
--
|
|
5
|
+
-- Required even with start Hooks off: every Hook insert writes `claims_token`.
|
|
6
|
+
-- The column additions and the unique index take locks that queue every
|
|
7
|
+
-- other query on workflow_runs and workflow_hooks behind them, so give up
|
|
8
|
+
-- after 10 seconds (unless the session set its own `lock_timeout`) and leave
|
|
9
|
+
-- the migration unapplied and retryable rather than stall the World. The
|
|
10
|
+
-- setting lasts for the migrator's transaction.
|
|
11
|
+
DO $$
|
|
12
|
+
BEGIN
|
|
13
|
+
IF current_setting('lock_timeout') = '0' THEN
|
|
14
|
+
PERFORM set_config('lock_timeout', '10s', true);
|
|
15
|
+
END IF;
|
|
16
|
+
END $$;
|
|
17
|
+
--> statement-breakpoint
|
|
18
|
+
CREATE TABLE IF NOT EXISTS "workflow"."workflow_start_hook_admissions" (
|
|
19
|
+
"run_id" varchar PRIMARY KEY NOT NULL,
|
|
20
|
+
"token" varchar NOT NULL,
|
|
21
|
+
"decision" varchar NOT NULL,
|
|
22
|
+
"conflicting_run_id" varchar,
|
|
23
|
+
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
|
24
|
+
);
|
|
25
|
+
--> statement-breakpoint
|
|
26
|
+
CREATE INDEX IF NOT EXISTS "workflow_start_hook_admissions_created_at_index" ON "workflow"."workflow_start_hook_admissions" USING btree ("created_at");
|
|
27
|
+
--> statement-breakpoint
|
|
28
|
+
-- A run's start Hook is an ordinary row in workflow_hooks, written by its
|
|
29
|
+
-- admission. Every Hook row written from here on claims its token, and
|
|
30
|
+
-- claiming rows are unique per token, so two admissions racing for one token,
|
|
31
|
+
-- or an admission and another run's createHook, conflict on the insert. Rows
|
|
32
|
+
-- written before this migration do not claim their token, so the index cannot
|
|
33
|
+
-- fail on existing data.
|
|
34
|
+
ALTER TABLE "workflow"."workflow_hooks" ADD COLUMN IF NOT EXISTS "claims_token" boolean DEFAULT false NOT NULL;
|
|
35
|
+
--> statement-breakpoint
|
|
36
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "workflow_hooks_token_claim_unique" ON "workflow"."workflow_hooks" USING btree ("token") WHERE "claims_token";
|
|
37
|
+
--> statement-breakpoint
|
|
38
|
+
-- The start Hook a run was admitted with (without its metadata), echoed on
|
|
39
|
+
-- the run. Last, so the index build above has scanned workflow_hooks before
|
|
40
|
+
-- this takes workflow_runs' lock.
|
|
41
|
+
ALTER TABLE "workflow"."workflow_runs" ADD COLUMN IF NOT EXISTS "start_hook_cbor" bytea;
|
|
42
|
+
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
-- The `jobPrefix` of the World that created each run, which scopes startup
|
|
2
|
+
-- recovery. `IF NOT EXISTS` because two `bootstrap` processes started together
|
|
3
|
+
-- can both apply a migration.
|
|
4
|
+
ALTER TABLE "workflow"."workflow_runs" ADD COLUMN IF NOT EXISTS "job_prefix" varchar;
|
|
@@ -190,6 +190,27 @@
|
|
|
190
190
|
"when": 1789948800000,
|
|
191
191
|
"tag": "0026_add_seq_in_band",
|
|
192
192
|
"breakpoints": true
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
"idx": 27,
|
|
196
|
+
"version": "7",
|
|
197
|
+
"when": 1790035200000,
|
|
198
|
+
"tag": "0027_timestamps_with_time_zone",
|
|
199
|
+
"breakpoints": true
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
"idx": 28,
|
|
203
|
+
"version": "7",
|
|
204
|
+
"when": 1790121600000,
|
|
205
|
+
"tag": "0028_add_start_hook_admissions",
|
|
206
|
+
"breakpoints": true
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
"idx": 29,
|
|
210
|
+
"version": "7",
|
|
211
|
+
"when": 1790208000000,
|
|
212
|
+
"tag": "0029_add_run_job_prefix",
|
|
213
|
+
"breakpoints": true
|
|
193
214
|
}
|
|
194
215
|
]
|
|
195
216
|
}
|