@aponiajs/queue 0.0.0-stage → 1.0.0-beta.65
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 +275 -2
- package/dist/index.d.mts +427 -0
- package/dist/index.mjs +1001 -0
- package/llms.txt +38 -0
- package/package.json +57 -4
package/README.md
CHANGED
|
@@ -1,3 +1,276 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @aponiajs/queue
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An in-process job queue and transactional outbox for AponiaJS, with bounded
|
|
4
|
+
retries, exponential backoff, dead letters, and idempotent enqueue.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
bun add @aponiajs/queue@alpha
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Use it
|
|
11
|
+
|
|
12
|
+
### Register the module
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { Module } from "@aponiajs/common";
|
|
16
|
+
import { QueueModule } from "@aponiajs/queue";
|
|
17
|
+
|
|
18
|
+
@Module({
|
|
19
|
+
imports: [QueueModule.forRoot({ queue: { concurrency: 4, maxAttempts: 5 } })],
|
|
20
|
+
})
|
|
21
|
+
export class AppModule {}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Use it in a controller
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { Body, Controller, Post } from "@aponiajs/common";
|
|
28
|
+
import { InjectQueue, type QueueService } from "@aponiajs/queue";
|
|
29
|
+
|
|
30
|
+
@Controller("signups")
|
|
31
|
+
export class SignupsController {
|
|
32
|
+
constructor(@InjectQueue("default") private readonly queue: QueueService) {}
|
|
33
|
+
|
|
34
|
+
@Post()
|
|
35
|
+
register(@Body() body: { email: string }) {
|
|
36
|
+
return this.queue.enqueue(
|
|
37
|
+
"email.send",
|
|
38
|
+
{ to: body.email },
|
|
39
|
+
{ idempotencyKey: `welcome:${body.email}` },
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Use it in a service
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { Injectable } from "@aponiajs/common";
|
|
49
|
+
import { InjectQueue, type QueueService } from "@aponiajs/queue";
|
|
50
|
+
|
|
51
|
+
@Injectable()
|
|
52
|
+
export class SignupService {
|
|
53
|
+
constructor(@InjectQueue("default") private readonly queue: QueueService) {}
|
|
54
|
+
|
|
55
|
+
welcome(email: string) {
|
|
56
|
+
return this.queue.enqueue("email.send", { to: email }, { idempotencyKey: `welcome:${email}` });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
A handler is registered once, where the application owns the work. A controller
|
|
62
|
+
or service enqueues through `QueueService`; nothing registers a handler at
|
|
63
|
+
module scope.
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { Injectable, type OnModuleInit } from "@aponiajs/common";
|
|
67
|
+
import { QueueService } from "@aponiajs/queue";
|
|
68
|
+
|
|
69
|
+
@Injectable()
|
|
70
|
+
export class EmailJobs implements OnModuleInit {
|
|
71
|
+
constructor(
|
|
72
|
+
private readonly queue: QueueService,
|
|
73
|
+
private readonly mailer: Mailer,
|
|
74
|
+
) {}
|
|
75
|
+
|
|
76
|
+
onModuleInit(): void {
|
|
77
|
+
this.queue.registerHandler("email.send", async (payload, job) => {
|
|
78
|
+
await this.mailer.send(payload, { idempotencyKey: job.idempotencyKey });
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
`Mailer` is an application provider, so the handler closes over the same
|
|
85
|
+
dependencies as the rest of the application.
|
|
86
|
+
|
|
87
|
+
### Async registration
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
QueueModule.forRootAsync({
|
|
91
|
+
useFactory: () => ({ queue: { concurrency: 4, maxAttempts: 5 } }),
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`useClass` constructs the factory class and reads its `create()` method;
|
|
96
|
+
`useExisting` aliases an already-registered provider through the same method.
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { Injectable } from "@aponiajs/common";
|
|
100
|
+
import { QueueModule, type QueueModuleOptions } from "@aponiajs/queue";
|
|
101
|
+
|
|
102
|
+
@Injectable()
|
|
103
|
+
class QueueOptionsFactory {
|
|
104
|
+
create(): QueueModuleOptions {
|
|
105
|
+
return { queue: { concurrency: 4, maxAttempts: 5 } };
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
QueueModule.forRootAsync({ useClass: QueueOptionsFactory });
|
|
110
|
+
QueueModule.forRootAsync({ useExisting: QueueOptionsFactory });
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
`useExisting` expects `QueueOptionsFactory` to be a provider of the module that
|
|
114
|
+
imports the registration.
|
|
115
|
+
|
|
116
|
+
### Multiple registrations
|
|
117
|
+
|
|
118
|
+
Name each registration and inject it through the matching `@InjectQueue()` token;
|
|
119
|
+
two registrations are distinct modules.
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
import { Injectable, Module } from "@aponiajs/common";
|
|
123
|
+
import { InjectQueue, QueueModule, type QueueService } from "@aponiajs/queue";
|
|
124
|
+
|
|
125
|
+
@Module({
|
|
126
|
+
imports: [
|
|
127
|
+
QueueModule.forRoot({ name: "email", queue: { concurrency: 2 } }),
|
|
128
|
+
QueueModule.forRoot({ name: "reports", queue: { concurrency: 1 } }),
|
|
129
|
+
],
|
|
130
|
+
})
|
|
131
|
+
export class AppModule {}
|
|
132
|
+
|
|
133
|
+
@Injectable()
|
|
134
|
+
class EmailService {
|
|
135
|
+
constructor(@InjectQueue("email") private readonly queue: QueueService) {}
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
`enqueue` schedules a drain by default. Call `await queue.drain()` to run every
|
|
140
|
+
due job now, or disable `autoStart` and drive the queue yourself.
|
|
141
|
+
`pause()` stops automatic draining and cancels its timer; `resume()` schedules
|
|
142
|
+
it again. An explicit `drain()` still runs while paused.
|
|
143
|
+
|
|
144
|
+
## Retries and dead letters
|
|
145
|
+
|
|
146
|
+
- A failed attempt is retried with `backoffMs * 2^(attempts - 1)`, capped at `maxBackoffMs`.
|
|
147
|
+
- A job that exhausts `maxAttempts` moves to the dead letter queue.
|
|
148
|
+
- Dead letters keep their payloads, so `maxDeadLetters` bounds them; it defaults
|
|
149
|
+
to `maxJobs` and evicts the oldest entry when full.
|
|
150
|
+
- `queue.deadLetters()` lists them; `queue.retryDeadLetter(id)` grants a fresh attempt budget.
|
|
151
|
+
- `queue.stats()` reports pending, running, and dead-lettered counts.
|
|
152
|
+
- `priority` orders due jobs: a higher value runs first, then earlier enqueue time.
|
|
153
|
+
- `jobTimeoutMs` (or a per-job `timeoutMs`) fails an attempt that runs too long; a failed attempt follows the retry policy, and an exhausted job is dead-lettered with the timeout as its last error.
|
|
154
|
+
- The handler receives `(payload, job, signal)`; the signal aborts on timeout and on `close()`. Cancellation is cooperative.
|
|
155
|
+
- A handler runs outside every registered request-context store. Read the
|
|
156
|
+
caller and tenant from the payload; `AuthService.current()` and
|
|
157
|
+
`TenantService.current()` are undefined inside a job.
|
|
158
|
+
- `onModuleDestroy` drains due jobs, aborts in-flight work, and flushes tracked writes.
|
|
159
|
+
|
|
160
|
+
## Durable backlog
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
const store: QueueStore = createSharedBacklog();
|
|
164
|
+
const jobs = QueueModule.forRoot({ queue: { store } });
|
|
165
|
+
|
|
166
|
+
@Module({ imports: [jobs] })
|
|
167
|
+
export class AppModule {}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
A store receives each job state and rebuilds the backlog when the module
|
|
171
|
+
initializes, so a restart no longer starts with an empty queue. The service
|
|
172
|
+
still owns scheduling, retries, and handlers; a store is a backlog, not a
|
|
173
|
+
broker, and two processes sharing one store both read the same pending jobs.
|
|
174
|
+
|
|
175
|
+
- Writes are tracked, not awaited by `enqueue`. `await queue.flush()` waits for
|
|
176
|
+
them and rethrows the first store failure.
|
|
177
|
+
- A job stored as `running` (a process that stopped mid-run) is hydrated as
|
|
178
|
+
pending, with its attempt count kept.
|
|
179
|
+
- `MemoryQueueStore` is the bounded in-memory implementation for tests and
|
|
180
|
+
single-process durability checks.
|
|
181
|
+
- `onModuleDestroy` stops scheduling and flushes outstanding writes.
|
|
182
|
+
|
|
183
|
+
## Outbox
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
import { Injectable } from "@aponiajs/common";
|
|
187
|
+
import { DatabaseService } from "@aponiajs/database";
|
|
188
|
+
import { OutboxService } from "@aponiajs/queue";
|
|
189
|
+
import { sql } from "drizzle-orm";
|
|
190
|
+
|
|
191
|
+
@Injectable()
|
|
192
|
+
class OrdersService {
|
|
193
|
+
constructor(
|
|
194
|
+
private readonly database: DatabaseService,
|
|
195
|
+
private readonly outbox: OutboxService,
|
|
196
|
+
private readonly publisher: OrderPublisher,
|
|
197
|
+
) {}
|
|
198
|
+
|
|
199
|
+
async create(id: string): Promise<void> {
|
|
200
|
+
await this.database.transaction(async (tx) => {
|
|
201
|
+
await tx.execute(sql`insert into orders (id) values (${id})`);
|
|
202
|
+
this.outbox.record("order.created", { orderId: id });
|
|
203
|
+
});
|
|
204
|
+
await this.outbox.flush((message) => this.publisher.publish(message.name, message.payload));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
`OrderPublisher` is an application provider that performs the actual delivery.
|
|
210
|
+
The outbox records the message inside the transaction and flushes it afterwards.
|
|
211
|
+
|
|
212
|
+
A message the sink rejects stays at the front of the outbox and the flush throws
|
|
213
|
+
`OUTBOX_DELIVERY_FAILED`, so the next flush retries it in order.
|
|
214
|
+
|
|
215
|
+
## Limits
|
|
216
|
+
|
|
217
|
+
Without a store, the queue is process-local and loses its backlog on restart.
|
|
218
|
+
A supplied store makes the backlog survive a restart, but it is still not a
|
|
219
|
+
broker: nothing claims a job, so run one consumer per backlog. The outbox is
|
|
220
|
+
process-local unless an `OutboxStore` is supplied; `SqliteOutboxStore` is the
|
|
221
|
+
bundled durable one, and the application owns its database. `waitForIdle()`
|
|
222
|
+
waits for the work that is runnable now, not for a job whose backoff has not
|
|
223
|
+
elapsed.
|
|
224
|
+
|
|
225
|
+
## Processor surface
|
|
226
|
+
|
|
227
|
+
The BullMQ-shaped names are available over the same in-process queue:
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
import { Processor, WorkerHost, InjectQueue } from "@aponiajs/queue";
|
|
231
|
+
import type { QueueJob } from "@aponiajs/queue";
|
|
232
|
+
|
|
233
|
+
@Processor("email")
|
|
234
|
+
class EmailProcessor extends WorkerHost {
|
|
235
|
+
async process(job: QueueJob<{ to: string }>): Promise<void> {
|
|
236
|
+
await send(job.payload.to);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
@Injectable()
|
|
241
|
+
class SignupService {
|
|
242
|
+
constructor(@InjectQueue("email") private readonly queue: QueueService) {}
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
The processor's name is the **job name** it handles; the class must be a
|
|
247
|
+
provider of a module that imports the registration. `JOB_REF` is a forwarding
|
|
248
|
+
view of the job a handler is running, for a provider that cannot take the
|
|
249
|
+
argument directly. `getQueueToken(name)` is `AponiaQueue_<name>`. This is not a
|
|
250
|
+
broker: there is no worker subscription, lock renewal, or cross-process claim.
|
|
251
|
+
|
|
252
|
+
## Registration contract
|
|
253
|
+
|
|
254
|
+
`QueueModule.forRoot` is a `ConfigurableModuleBuilder` definition: it provides
|
|
255
|
+
and exports `MODULE_OPTIONS_TOKEN` (`QUEUE_MODULE_OPTIONS`), accepts `isGlobal`
|
|
256
|
+
as a definition-shaping extra and the three asynchronous shapes through
|
|
257
|
+
`forRootAsync`, and keeps `register`/`registerAsync` as deprecated aliases. Both
|
|
258
|
+
forms validate the queue bounds, the outbox bounds, and both store shapes before
|
|
259
|
+
the boot proceeds, reporting every issue at once.
|
|
260
|
+
|
|
261
|
+
## Bootstrap
|
|
262
|
+
|
|
263
|
+
```ts
|
|
264
|
+
import { AponiaFactory } from "@aponiajs/platform-elysia";
|
|
265
|
+
import { AppModule } from "./app.module.ts";
|
|
266
|
+
|
|
267
|
+
const application = await AponiaFactory.create(AppModule);
|
|
268
|
+
await application.listen(Number(Bun.env.PORT ?? 3000));
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Documentation
|
|
272
|
+
|
|
273
|
+
- [Agent surface](../../docs/agent/README.md)
|
|
274
|
+
- [Jobs and outbox guide](../../docs/services/queues.md)
|
|
275
|
+
- [Published packages](../../docs/reference/packages.md)
|
|
276
|
+
- [npm](https://www.npmjs.com/package/@aponiajs/queue)
|