@shivaedev/jobs 0.22.0 → 0.22.1
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/CHANGELOG.md +12 -0
- package/README.md +151 -151
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.22.1 - 2026-10-30
|
|
4
|
+
|
|
5
|
+
### Changed
|
|
6
|
+
|
|
7
|
+
- Require Effect 4.0.6 and align the Effect companion package peers and generated starter catalog to the same exact release. Install the 4.0.6 Effect stack together.
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
|
|
11
|
+
- Register hosted RPC cache-control handling through Effect 4.0.6's request-scoped pre-response API so responses keep `private, no-store`.
|
|
12
|
+
- Accept native RPC's `application/x-ndjson` media type while preserving `application/ndjson` callers at the transport guard.
|
|
13
|
+
- Keep subscriptions open after their HTTP request body completes; closing the socket still cancels them.
|
|
14
|
+
|
|
3
15
|
## 0.22.0 - 2026-10-28
|
|
4
16
|
|
|
5
17
|
### Added
|
package/README.md
CHANGED
|
@@ -12,13 +12,11 @@ import { Effect, Schema } from "effect";
|
|
|
12
12
|
import { Mail } from "./services.ts";
|
|
13
13
|
|
|
14
14
|
const SendEmail = defineQueue({
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
name: "send-email",
|
|
16
|
+
schema: Schema.Struct({ userId: Schema.NumberFromString }),
|
|
17
17
|
});
|
|
18
18
|
|
|
19
|
-
const emailWorker = SendEmail.handle(({ userId }) =>
|
|
20
|
-
Effect.flatMap(Mail, (mail) => mail.send(userId)),
|
|
21
|
-
);
|
|
19
|
+
const emailWorker = SendEmail.handle(({ userId }) => Effect.flatMap(Mail, (mail) => mail.send(userId)));
|
|
22
20
|
```
|
|
23
21
|
|
|
24
22
|
`Mail` is the application's Effect service, defined in the guide below. The worker receives a number even though the durable payload stores a string. The same Schema encodes the producer's payload and decodes the worker's input; malformed stored data fails before the handler runs. Provide the worker's services when the jobs Layer starts, and the handler uses that captured context when pg-boss calls it later.
|
|
@@ -46,19 +44,19 @@ import { pgBossSchema } from "@shivaedev/jobs/server/schema.ts";
|
|
|
46
44
|
import { appDatabase } from "@shivaedev/server/appHost.ts";
|
|
47
45
|
|
|
48
46
|
const database = appDatabase({
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
47
|
+
migrations: {
|
|
48
|
+
loader,
|
|
49
|
+
components: [pgBossSchema({ schema: "app_jobs" })],
|
|
50
|
+
mode: "migrate",
|
|
51
|
+
},
|
|
54
52
|
});
|
|
55
53
|
```
|
|
56
54
|
|
|
57
55
|
The component uses `installPgBossSchema` as its only install and upgrade path. It is pending when the schema is missing or its stored version is below the installed pg-boss package's required schema version; the readiness error names the schema and both versions (`missing` for an absent version). Start every producer and worker with the same schema and `migrate: false`. Use host mode `"wait"` for services that must wait for a deployment's migration process; no additional jobs-layer wait is needed.
|
|
58
56
|
|
|
59
|
-
With server's `appDatabase({ migrations: { mode: "wait", release, ... } })`, each jobs Layer automatically reads sql's optional `WorkerRelease` Context service. When the database belongs to another release, it calls pg-boss `offWork` for every registered worker queue, lets active jobs finish, logs `Workers paused for another database release` once, and idles. Producers and enqueueing continue. When its release owns the marker again and old workers have drained, it registers workers through the normal path and logs `Workers resumed for database release`. Equality permits rollbacks; no version ordering or payload compatibility policy is added.
|
|
57
|
+
With server's `appDatabase({ migrations: { mode: "wait", release, ... } })`, each jobs Layer automatically reads sql's optional `WorkerRelease` Context service. When the database belongs to another release, it calls pg-boss `offWork` for every registered worker queue, lets active jobs finish, logs `Workers paused for another database release` once, and idles. Producers and enqueueing continue. When its release owns the marker again and old workers have drained, it registers workers through the normal path and logs `Workers resumed for database release`. Equality permits rollbacks; no version ordering or payload compatibility policy is added. Without that service, the Layer registers its workers at startup and never pauses them.
|
|
60
58
|
|
|
61
|
-
A worker keeps its sql
|
|
59
|
+
A paused worker keeps its process registered with sql's worker release while active jobs drain, so the owning release waits for it; [sql's worker release](https://github.com/ShivaeDev/platform/blob/main/packages/sql/README.md) owns the heartbeat and how long a crashed worker can block. Closing the jobs Layer's scope waits for the native pg-boss `stop` before releasing the services the Layer captured. Choose the three bounds together: each queue's `expireInSeconds`, a `stop: { graceful: true, timeout }` in milliseconds that covers it, and a host termination grace beyond that timeout. For example, a queue with `expireInSeconds: 60` pairs with `stop: { graceful: true, timeout: 65_000 }` under a 75-second termination grace.
|
|
62
60
|
|
|
63
61
|
`installPgBossSchema` installs or upgrades pg-boss through the provided Effect SQL client without starting workers or schedules. Run it outside an application transaction, in a release step or the `fixtures` Effect of `makePostgresTestLayer`, so stories inherit the schema from the migrated template:
|
|
64
62
|
|
|
@@ -82,14 +80,17 @@ import { Context, Effect, Layer } from "effect";
|
|
|
82
80
|
|
|
83
81
|
export const Jobs = makePgBoss("@app/Jobs");
|
|
84
82
|
|
|
85
|
-
export class Mail extends Context.Service<
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
83
|
+
export class Mail extends Context.Service<
|
|
84
|
+
Mail,
|
|
85
|
+
{
|
|
86
|
+
readonly send: (userId: number) => Effect.Effect<void>;
|
|
87
|
+
readonly removeExpired: Effect.Effect<void>;
|
|
88
|
+
}
|
|
89
|
+
>()("@app/Mail") {}
|
|
89
90
|
|
|
90
91
|
export const MailLive = Layer.succeed(Mail, {
|
|
91
|
-
|
|
92
|
-
|
|
92
|
+
send: (userId) => Effect.log({ event: "send_email", userId }),
|
|
93
|
+
removeExpired: Effect.log({ event: "remove_expired_mail" }),
|
|
93
94
|
});
|
|
94
95
|
```
|
|
95
96
|
|
|
@@ -105,27 +106,25 @@ import { Effect, Schema } from "effect";
|
|
|
105
106
|
import { Mail } from "./services.ts";
|
|
106
107
|
|
|
107
108
|
export const SendEmail = defineQueue({
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
109
|
+
name: "send-email",
|
|
110
|
+
schema: Schema.Struct({ userId: Schema.NumberFromString }),
|
|
111
|
+
queue: { retryLimit: 5 },
|
|
111
112
|
});
|
|
112
113
|
|
|
113
114
|
export const emailWorker = SendEmail.handle(({ userId }, job) =>
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
115
|
+
Effect.gen(function* () {
|
|
116
|
+
const mail = yield* Mail;
|
|
117
|
+
yield* mail.send(userId);
|
|
118
|
+
yield* Effect.log({ jobId: job.id });
|
|
119
|
+
}),
|
|
119
120
|
);
|
|
120
121
|
|
|
121
122
|
export const Cleanup = defineSchedule({
|
|
122
|
-
|
|
123
|
-
|
|
123
|
+
name: "cleanup-mail",
|
|
124
|
+
cron: "17 3 * * *",
|
|
124
125
|
});
|
|
125
126
|
|
|
126
|
-
export const cleanupWorker = Cleanup.run(
|
|
127
|
-
Effect.flatMap(Mail, (mail) => mail.removeExpired),
|
|
128
|
-
);
|
|
127
|
+
export const cleanupWorker = Cleanup.run(Effect.flatMap(Mail, (mail) => mail.removeExpired));
|
|
129
128
|
```
|
|
130
129
|
|
|
131
130
|
The queue takes `{ userId: number }` in application code and stores `{ userId: string }`. Its handler receives the decoded number and a job context, including the job's `id`. Keep the queue definition available to producers; the runtime registers the worker created by `handle`.
|
|
@@ -142,10 +141,10 @@ Queue and schedule workers default to `batchSize: 10` and `burstWhenBatchFull: t
|
|
|
142
141
|
|
|
143
142
|
```ts
|
|
144
143
|
const Compare = defineQueue({
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
144
|
+
name: "compare",
|
|
145
|
+
schema: Schema.Struct({ id: Schema.Number }),
|
|
146
|
+
queue: { notify: true },
|
|
147
|
+
worker: { batchSize: 2, localConcurrency: 5 },
|
|
149
148
|
});
|
|
150
149
|
```
|
|
151
150
|
|
|
@@ -161,10 +160,12 @@ Fail a queue handler or scheduled Effect with `RetryAt` when a provider gives a
|
|
|
161
160
|
import { DateTime, Effect } from "effect";
|
|
162
161
|
import { RetryAt } from "@shivaedev/jobs/server/definition.ts";
|
|
163
162
|
|
|
164
|
-
const waitForQuota = Effect.fail(
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
163
|
+
const waitForQuota = Effect.fail(
|
|
164
|
+
new RetryAt({
|
|
165
|
+
at: DateTime.makeUnsafe("2030-01-01T00:01:00Z"),
|
|
166
|
+
reason: "Provider quota resets",
|
|
167
|
+
}),
|
|
168
|
+
);
|
|
168
169
|
```
|
|
169
170
|
|
|
170
171
|
The same job returns to waiting with `startAfter` set to `at`; its ID, singleton key, payload and retry count stay unchanged. A past or current `at` is eligible on the next fetch. Deferral does not dead-letter the job or call `onError`. Its job span ends OK with a `job.deferred` event whose `at` is an ISO UTC string and whose `reason` is the supplied string. Deferred jobs rely on polling, including queues using `notify: true`. Stately jobs sharing a singleton key can defer together and still run one at a time. While waiting, their native job metadata reports the adapter's `effect-stately-deferred` policy; a database trigger restores `stately` when pg-boss claims them, preserving its active-job exclusion.
|
|
@@ -177,19 +178,17 @@ Put related job kinds in one tagged-union queue and choose the entity key in the
|
|
|
177
178
|
|
|
178
179
|
```ts
|
|
179
180
|
const PullRequestJobs = defineQueue({
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
181
|
+
name: "pull-request",
|
|
182
|
+
schema: Schema.Union([
|
|
183
|
+
Schema.TaggedStruct("Refresh", { pullRequestId: Schema.NumberFromString }),
|
|
184
|
+
Schema.TaggedStruct("Reconcile", { pullRequestId: Schema.NumberFromString }),
|
|
185
|
+
]),
|
|
186
|
+
queue: { policy: "singleton" },
|
|
187
|
+
key: ({ pullRequestId }) => `pr:${pullRequestId.toFixed(0)}`,
|
|
188
|
+
worker: { batchSize: 1, localConcurrency: 3 },
|
|
188
189
|
});
|
|
189
190
|
|
|
190
|
-
const refresh = Effect.flatMap(Jobs, (jobs) =>
|
|
191
|
-
jobs.enqueue(PullRequestJobs, { _tag: "Refresh", pullRequestId: 42 }),
|
|
192
|
-
);
|
|
191
|
+
const refresh = Effect.flatMap(Jobs, (jobs) => jobs.enqueue(PullRequestJobs, { _tag: "Refresh", pullRequestId: 42 }));
|
|
193
192
|
```
|
|
194
193
|
|
|
195
194
|
`queue.policy` accepts pg-boss's policy literals and rejects typos. The `singleton` policy requires `key: (payload) => string`. The callback sees the decoded payload, so `pullRequestId` is a number. `enqueue` supplies pg-boss's `singletonKey` automatically; callers cannot supply a conflicting key in send options.
|
|
@@ -204,15 +203,15 @@ pg-boss marks every job in a fetched batch active together. Each job's expiry wi
|
|
|
204
203
|
|
|
205
204
|
```ts
|
|
206
205
|
const Ingest = defineQueue({
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
206
|
+
name: "ingest",
|
|
207
|
+
schema: Schema.Struct({ sourceId: Schema.String }),
|
|
208
|
+
queue: { expireInSeconds: 300 },
|
|
209
|
+
worker: { batchSize: 1 },
|
|
211
210
|
});
|
|
212
211
|
|
|
213
212
|
const IndexTitle = defineQueue({
|
|
214
|
-
|
|
215
|
-
|
|
213
|
+
name: "index-title",
|
|
214
|
+
schema: Schema.Struct({ titleId: Schema.String }),
|
|
216
215
|
});
|
|
217
216
|
```
|
|
218
217
|
|
|
@@ -228,10 +227,10 @@ Queue handlers can yield `job.remainingBudget`, an `Effect.Effect<Duration.Durat
|
|
|
228
227
|
import { Duration, Effect } from "effect";
|
|
229
228
|
|
|
230
229
|
const ingestWorker = Ingest.handle(({ sourceId }, job) =>
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
230
|
+
Effect.gen(function* () {
|
|
231
|
+
const budget = yield* job.remainingBudget;
|
|
232
|
+
yield* Effect.log({ sourceId, remainingMilliseconds: Duration.toMillis(budget) });
|
|
233
|
+
}),
|
|
235
234
|
);
|
|
236
235
|
```
|
|
237
236
|
|
|
@@ -247,11 +246,11 @@ import { cleanupWorker, emailWorker } from "./jobs.ts";
|
|
|
247
246
|
import { Jobs, MailLive } from "./services.ts";
|
|
248
247
|
|
|
249
248
|
export const jobsLayer = (databaseUrl: string) =>
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
249
|
+
Jobs.layer({
|
|
250
|
+
connectionString: databaseUrl,
|
|
251
|
+
schema: "app_jobs",
|
|
252
|
+
jobs: [emailWorker, cleanupWorker],
|
|
253
|
+
}).pipe(Layer.provide(MailLive));
|
|
255
254
|
```
|
|
256
255
|
|
|
257
256
|
The constructor options belong to pg-boss. `jobs` is the complete list of registrations for this Layer. The Layer captures `MailLive` for both the queue handler and the scheduled Effect. A startup or registration failure fails Layer acquisition with `PgBossError` or `PgBossQueueOptionsError` and releases its acquired client reference. Registration creates a queue again, up to six times with a short backoff, when PostgreSQL rolls the creation back as a deadlock victim (SQLSTATE `40P01`), which a partitioned queue can meet when other queues in the schema have traffic. A shared client stays running while another scope still owns a reference.
|
|
@@ -266,12 +265,12 @@ import { jobsLayer } from "./runtime.ts";
|
|
|
266
265
|
import { Jobs } from "./services.ts";
|
|
267
266
|
|
|
268
267
|
const inspectJobs = (databaseUrl: string) =>
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
268
|
+
Effect.scoped(
|
|
269
|
+
Effect.gen(function* () {
|
|
270
|
+
const jobs = yield* Jobs;
|
|
271
|
+
return yield* jobs.health;
|
|
272
|
+
}).pipe(Effect.provide(jobsLayer(databaseUrl))),
|
|
273
|
+
);
|
|
275
274
|
```
|
|
276
275
|
|
|
277
276
|
This Effect starts the jobs Layer, reads health and closes the scope. The client stops when that scope closes.
|
|
@@ -284,10 +283,10 @@ import { SendEmail } from "./jobs.ts";
|
|
|
284
283
|
import { Jobs } from "./services.ts";
|
|
285
284
|
|
|
286
285
|
export const enqueueEmail = (userId: number) =>
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
286
|
+
Effect.gen(function* () {
|
|
287
|
+
const jobs = yield* Jobs;
|
|
288
|
+
return yield* jobs.enqueue(SendEmail, { userId });
|
|
289
|
+
});
|
|
291
290
|
```
|
|
292
291
|
|
|
293
292
|
Provide `Jobs` through the application's existing runtime. `enqueue` takes the decoded payload, so passing a string `userId` is a type error. It encodes the number to a string before calling pg-boss. The result type is `Option.Option<string>`: a job identifier becomes `Some`, and a `null` send result becomes `None`.
|
|
@@ -309,15 +308,15 @@ import { Effect } from "effect";
|
|
|
309
308
|
import { Jobs } from "./services.ts";
|
|
310
309
|
|
|
311
310
|
export const readJobCounts = Effect.gen(function* () {
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
311
|
+
const jobs = yield* Jobs;
|
|
312
|
+
const health = yield* jobs.health;
|
|
313
|
+
return {
|
|
314
|
+
active: health.activeTotal,
|
|
315
|
+
deadLettered: health.deadLetteredTotal,
|
|
316
|
+
failed: health.failedTotal,
|
|
317
|
+
queued: health.queuedTotal,
|
|
318
|
+
ready: health.readyTotal,
|
|
319
|
+
};
|
|
321
320
|
});
|
|
322
321
|
```
|
|
323
322
|
|
|
@@ -330,15 +329,16 @@ import { Effect, Option } from "effect";
|
|
|
330
329
|
import { SendEmail } from "./jobs.ts";
|
|
331
330
|
import { Jobs } from "./services.ts";
|
|
332
331
|
|
|
333
|
-
export const inspectEmail = (id: string) =>
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
332
|
+
export const inspectEmail = (id: string) =>
|
|
333
|
+
Effect.gen(function* () {
|
|
334
|
+
const jobs = yield* Jobs;
|
|
335
|
+
const job = yield* jobs.getJob(SendEmail, id);
|
|
336
|
+
if (Option.isSome(job) && job.value.state === "failed") {
|
|
337
|
+
yield* jobs.retry(SendEmail, id);
|
|
338
|
+
}
|
|
339
|
+
const counts = yield* jobs.counts(SendEmail);
|
|
340
|
+
return counts.queuedCount === 0 && counts.activeCount === 0;
|
|
341
|
+
});
|
|
342
342
|
```
|
|
343
343
|
|
|
344
344
|
`counts(queue)` reads the job table directly on each call. `queuedCount` includes created and retry-waiting jobs, including future jobs; `activeCount` counts running jobs, `failedCount` counts retained failures, and `totalCount` includes retained terminal jobs. A queue has drained when queued and active counts are both zero. These counts are a snapshot, so a producer can enqueue more work after the query returns.
|
|
@@ -352,16 +352,17 @@ import { Effect, Option } from "effect";
|
|
|
352
352
|
import { SendEmail } from "./jobs.ts";
|
|
353
353
|
import { Jobs } from "./services.ts";
|
|
354
354
|
|
|
355
|
-
export const failedEmails = (cursor?: string) =>
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
}
|
|
355
|
+
export const failedEmails = (cursor?: string) =>
|
|
356
|
+
Effect.gen(function* () {
|
|
357
|
+
const jobs = yield* Jobs;
|
|
358
|
+
const failed = yield* jobs.failedJobs(SendEmail, { cursor, limit: 20 });
|
|
359
|
+
const deadLettered = yield* jobs.deadLetteredJobs(SendEmail, { limit: 20 });
|
|
360
|
+
return {
|
|
361
|
+
deadLettered: deadLettered.jobs.map((job) => ({ id: job.id, original: Option.getOrNull(job.sourceId), error: job.sourceOutput })),
|
|
362
|
+
failed: failed.jobs.map((job) => ({ id: job.id, attempt: job.attempt, error: job.output, userId: job.data.userId })),
|
|
363
|
+
next: Option.getOrUndefined(failed.next),
|
|
364
|
+
};
|
|
365
|
+
});
|
|
365
366
|
```
|
|
366
367
|
|
|
367
368
|
`health` and `counts` say how many jobs failed; these two listings say which ones and why. `failedJobs(queue, options)` lists the queue's jobs in the `failed` state, the ones that used up their retries. `deadLetteredJobs(queue, options)` lists every job in the queue's dead-letter queue. Dead-letter entries live in the `-dlq` queue: each is a new job with its own ID that pg-boss copied there when the original failed for good. Both listings read pg-boss's `job` table on each call, through the jobs Layer's own pg-boss connection and in its configured schema, so applications never query pg-boss tables themselves. A custom client needs the native `getDb` capability for them.
|
|
@@ -386,12 +387,12 @@ import { cleanupWorker, emailWorker } from "./jobs.ts";
|
|
|
386
387
|
import { Jobs, MailLive } from "./services.ts";
|
|
387
388
|
|
|
388
389
|
const sharedJobsLayer = (databaseUrl: string) =>
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
390
|
+
Jobs.layer({
|
|
391
|
+
connectionString: databaseUrl,
|
|
392
|
+
schema: "app_jobs",
|
|
393
|
+
clientCacheKey: "app-jobs",
|
|
394
|
+
jobs: [emailWorker, cleanupWorker],
|
|
395
|
+
}).pipe(Layer.provide(MailLive));
|
|
395
396
|
```
|
|
396
397
|
|
|
397
398
|
Overlapping Layer builds with the same `clientCacheKey` share one started client. The later build replaces the registered worker, and the final scope release stops the shared client. A later build after that release starts a new client. Without a key, each build creates its own client. The application chooses whether to pass a key; give each distinct constructor configuration its own key.
|
|
@@ -404,8 +405,7 @@ Pass a queue definition directly in the jobs Layer to create its queue and dead-
|
|
|
404
405
|
import { SendEmail } from "./jobs.ts";
|
|
405
406
|
import { Jobs } from "./services.ts";
|
|
406
407
|
|
|
407
|
-
export const producerLayer = (databaseUrl: string) =>
|
|
408
|
-
Jobs.layer({ connectionString: databaseUrl, jobs: [SendEmail] });
|
|
408
|
+
export const producerLayer = (databaseUrl: string) => Jobs.layer({ connectionString: databaseUrl, jobs: [SendEmail] });
|
|
409
409
|
```
|
|
410
410
|
|
|
411
411
|
Provide this Layer to the request runtime and call `jobs.enqueue(SendEmail, { userId })` as above. A separate worker process registers `SendEmail.handle(...)` against the same database. Producer queues use the same retry options and appear in `health`; only worker registrations capture handler services.
|
|
@@ -416,22 +416,22 @@ A handler can yield the same `Jobs` service that owns it. The Layer supplies tha
|
|
|
416
416
|
|
|
417
417
|
```ts
|
|
418
418
|
const Receipt = defineQueue({
|
|
419
|
-
|
|
420
|
-
|
|
419
|
+
name: "receipt",
|
|
420
|
+
schema: Schema.Struct({ userId: Schema.Number }),
|
|
421
421
|
});
|
|
422
422
|
|
|
423
423
|
const sendAndQueueReceipt = SendEmail.handle(({ userId }) =>
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
424
|
+
Effect.gen(function* () {
|
|
425
|
+
const mail = yield* Mail;
|
|
426
|
+
yield* mail.send(userId);
|
|
427
|
+
const jobs = yield* Jobs;
|
|
428
|
+
yield* jobs.enqueue(Receipt, { userId });
|
|
429
|
+
}),
|
|
430
430
|
);
|
|
431
431
|
|
|
432
432
|
const live = Jobs.layer({
|
|
433
|
-
|
|
434
|
-
|
|
433
|
+
connectionString: databaseUrl,
|
|
434
|
+
jobs: [sendAndQueueReceipt, Receipt],
|
|
435
435
|
}).pipe(Layer.provide(MailLive));
|
|
436
436
|
```
|
|
437
437
|
|
|
@@ -450,29 +450,29 @@ import { storyKit } from "@shivaedev/test-story/server/storyKit.ts";
|
|
|
450
450
|
import { makePgBossTesting, testRetryPolicy } from "@shivaedev/jobs/server/testing.ts";
|
|
451
451
|
|
|
452
452
|
const emailStories = storyKit({
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
453
|
+
clock: "live",
|
|
454
|
+
*create() {
|
|
455
|
+
const testing = makePgBossTesting({ retryPolicy: testRetryPolicy });
|
|
456
|
+
const context = yield* Layer.build(
|
|
457
|
+
Jobs.layer({
|
|
458
|
+
connectionString: databaseUrl,
|
|
459
|
+
clientFactory: testing.clientFactory,
|
|
460
|
+
jobs: [SendEmail],
|
|
461
|
+
}),
|
|
462
|
+
);
|
|
463
|
+
return { jobs: Context.get(context, Jobs), testing };
|
|
464
|
+
},
|
|
465
|
+
name: "queued email",
|
|
466
|
+
stages: ["ready"],
|
|
467
|
+
verbs: ({ jobs, testing }) => ({
|
|
468
|
+
enqueue: (userId: number) => jobs.enqueue(SendEmail, { userId }),
|
|
469
|
+
testing,
|
|
470
|
+
}),
|
|
469
471
|
});
|
|
470
472
|
|
|
471
473
|
emailStories.it("decodes the queued email", [], function* ({ enqueue, testing }) {
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
{ id, data: { userId: 42 }, state: "created" },
|
|
475
|
-
]);
|
|
474
|
+
const id = Option.getOrThrow(yield* enqueue(42));
|
|
475
|
+
expect(yield* testing.jobs(SendEmail)).toMatchObject([{ id, data: { userId: 42 }, state: "created" }]);
|
|
476
476
|
});
|
|
477
477
|
```
|
|
478
478
|
|
|
@@ -500,15 +500,15 @@ A kit can also capture `revision` before reading jobs and yield `waitForChange(r
|
|
|
500
500
|
|
|
501
501
|
Import the module that defines the name; the package has no root entry.
|
|
502
502
|
|
|
503
|
-
| Module
|
|
504
|
-
|
|
|
503
|
+
| Module | Public names |
|
|
504
|
+
| -------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
505
505
|
| `@shivaedev/jobs/server/definition.ts` | `defineQueue`, `defineSchedule`, `jobContext`, `RetryAt`; `DefineQueueOptions`, `DefineScheduleOptions`, `JobContext`, `JobPayloadSchema`, `JobRegistration`, `QueueDefinition`, `QueueOptions`, `QueuePolicy`, `QueueWorker`, `RegistrationRequirements`, `ScheduleDefinition`, `ScheduledWorker` |
|
|
506
|
-
| `@shivaedev/jobs/server/service.ts`
|
|
507
|
-
| `@shivaedev/jobs/server/schema.ts`
|
|
508
|
-
| `@shivaedev/jobs/server/health.ts`
|
|
509
|
-
| `@shivaedev/jobs/server/error.ts`
|
|
510
|
-
| `@shivaedev/jobs/server/client.ts`
|
|
511
|
-
| `@shivaedev/jobs/server/testing.ts`
|
|
506
|
+
| `@shivaedev/jobs/server/service.ts` | `makePgBoss`; `PgBossDefinition`, `PgBossLayerOptions`, `PgBossService` |
|
|
507
|
+
| `@shivaedev/jobs/server/schema.ts` | `installPgBossSchema`, `pgBossSchema`; `PgBossSchemaOptions` |
|
|
508
|
+
| `@shivaedev/jobs/server/health.ts` | `deadLetterQueueName`; `DeadLetteredJob`, `JobPage`, `JobPageOptions`, `JobsHealth`, `ListedJob`, `QueueCounts`, `QueueHealth` |
|
|
509
|
+
| `@shivaedev/jobs/server/error.ts` | `PgBossError`, `PgBossPayloadError`, `PgBossQueueOptionsError`, `PgBossClientOptionsError`, `PgBossTestingError`, `toPgBossError`; `PgBossOperation` |
|
|
510
|
+
| `@shivaedev/jobs/server/client.ts` | `defaultClientFactory`; `PgBossClient`, `PgBossClientFactory` |
|
|
511
|
+
| `@shivaedev/jobs/server/testing.ts` | `makePgBossTesting`, `testRetryPolicy`; `PgBossTesting`, `PgBossTestingOptions`, `SnapshotQueue`, `UnfinishedJob` |
|
|
512
512
|
|
|
513
513
|
The definition options keep the pg-boss names: `queue` uses `QueueOptions` (`Queue` except `name` and `deadLetter`, with literal `QueuePolicy` values and `maxDeferrals`), `worker` uses `WorkOptions`, and a schedule's `schedule` uses `ScheduleOptions`. A queue also requires `name` and `schema`; a schedule requires `name` and `cron`.
|
|
514
514
|
|
|
@@ -523,11 +523,11 @@ Worker registration always requests `includeMetadata: true`, including when `wor
|
|
|
523
523
|
### Install, setup and limits
|
|
524
524
|
|
|
525
525
|
```sh
|
|
526
|
-
pnpm add @shivaedev/jobs effect
|
|
526
|
+
pnpm add @shivaedev/jobs effect pg-boss
|
|
527
527
|
```
|
|
528
528
|
|
|
529
529
|
The package requires Node.js 24 or later and PostgreSQL. `effect` and `pg-boss` are peers; use versions that satisfy the package's published peer ranges.
|
|
530
530
|
|
|
531
|
-
`connectionString` configures pg-boss's own `pg` Pool, separately from the application's Effect SQL pool
|
|
531
|
+
`connectionString` configures pg-boss's own `pg` Pool, separately from the application's Effect SQL pool, and pg-boss's driver decides how its `sslmode` behaves. Set the TLS mode in that URL; the [framework database connection security guide](https://github.com/ShivaeDev/platform/blob/main/docs/framework/quickstart.md#database-connection-security) compares the drivers and the application's connection paths.
|
|
532
532
|
|
|
533
533
|
Provide the database connection and job registrations at the application runtime boundary. Application services own the domain work and provider calls; pg-boss owns queue execution. When `SqlClient` is provided and a SQL transaction is active, `enqueue` uses pg-boss’s per-call `db` option on that transaction’s connection. The application write and queued jobs commit or roll back together. The jobs client and SQL client must use the same PostgreSQL database. Outside a transaction, enqueueing keeps using the jobs client as usual. To keep enqueueing independent of an application transaction, move it after the transaction returns and handle its failure separately.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/jobs",
|
|
3
|
-
"version": "0.22.
|
|
3
|
+
"version": "0.22.1",
|
|
4
4
|
"description": "Effect-native jobs and lifecycle management for pg-boss",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -44,21 +44,21 @@
|
|
|
44
44
|
"README.md"
|
|
45
45
|
],
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@shivaedev/sql": "0.
|
|
47
|
+
"@shivaedev/sql": "0.25.0"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@shivaedev/quality": "0.
|
|
51
|
-
"@shivaedev/test": "0.10.
|
|
52
|
-
"@shivaedev/test-sql": "0.
|
|
53
|
-
"@shivaedev/test-story": "0.
|
|
50
|
+
"@shivaedev/quality": "0.45.0",
|
|
51
|
+
"@shivaedev/test": "0.10.1",
|
|
52
|
+
"@shivaedev/test-sql": "0.11.0",
|
|
53
|
+
"@shivaedev/test-story": "0.24.0",
|
|
54
54
|
"@types/node": "24.19.2",
|
|
55
|
-
"effect": "4.0.
|
|
55
|
+
"effect": "4.0.6",
|
|
56
56
|
"pg-boss": "12.38.0",
|
|
57
57
|
"typescript": "7.0.2",
|
|
58
58
|
"vitest": "5.0.5"
|
|
59
59
|
},
|
|
60
60
|
"peerDependencies": {
|
|
61
|
-
"effect": "4.0.
|
|
61
|
+
"effect": "4.0.6",
|
|
62
62
|
"pg-boss": "12.38.0"
|
|
63
63
|
},
|
|
64
64
|
"engines": {
|