@gravito/stream 1.0.0-beta.1 → 1.0.0-beta.2

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 CHANGED
@@ -7,10 +7,14 @@ Lightweight, high-performance queueing for Gravito. Supports multiple storage dr
7
7
  ## Features
8
8
 
9
9
  - **Zero runtime overhead**: Thin wrappers that delegate to drivers
10
- - **Multi-driver support**: Memory, Database, Redis, Kafka, SQS
10
+ - **Multi-driver support**: Memory, Database, Redis, Kafka, SQS, RabbitMQ
11
11
  - **Modular**: Install only the driver you need (core < 50KB)
12
12
  - **Embedded or standalone workers**: Run in-process during development or standalone in production
13
13
  - **AI-friendly**: Strong typing, clear JSDoc, and predictable APIs
14
+ - **Custom Retry Strategies**: Built-in exponential backoff with per-job overrides
15
+ - **Dead Letter Queue (DLQ)**: Automatic handling of permanently failed jobs, with retry and clear operations
16
+ - **Priority Queues**: Assign priority (critical, high, low) to any job
17
+ - **Rate Limiting**: Control job consumption rate per queue (requires Redis)
14
18
 
15
19
  ## Installation
16
20
 
@@ -37,6 +41,24 @@ export class SendWelcomeEmail extends Job {
37
41
  }
38
42
  ```
39
43
 
44
+ ### 3. Rate Limit & Priority (Optional)
45
+
46
+ ```typescript
47
+ const queue = c.get('queue')
48
+
49
+ // High priority job
50
+ await queue.push(new SendWelcomeEmail(user.id))
51
+ .onQueue('emails')
52
+ .withPriority('high') // 'critical' | 'high' | 'default' | 'low'
53
+
54
+ // Configure rate limits in Consumer
55
+ const consumer = new Consumer(manager, {
56
+ rateLimits: {
57
+ emails: { limit: 10, window: 60 } // Max 10 jobs per minute
58
+ }
59
+ })
60
+ ```
61
+
40
62
  ### 2. Enqueue a job
41
63
 
42
64
  ```typescript
@@ -95,6 +117,31 @@ const core = await PlanetCore.boot({
95
117
  })
96
118
  ```
97
119
 
120
+ ## RabbitMQ Driver Example
121
+
122
+ ```typescript
123
+ import { OrbitStream } from '@gravito/stream'
124
+ import amqp from 'amqplib'
125
+
126
+ const connection = await amqp.connect('amqp://localhost')
127
+
128
+ const core = await PlanetCore.boot({
129
+ orbits: [
130
+ OrbitStream.configure({
131
+ default: 'rabbitmq',
132
+ connections: {
133
+ rabbitmq: {
134
+ driver: 'rabbitmq',
135
+ client: connection,
136
+ exchange: 'gravito.events',
137
+ exchangeType: 'fanout'
138
+ }
139
+ }
140
+ })
141
+ ]
142
+ })
143
+ ```
144
+
98
145
  ## Database Schema
99
146
 
100
147
  ```sql
@@ -110,8 +157,35 @@ CREATE TABLE jobs (
110
157
 
111
158
  CREATE INDEX idx_jobs_queue_available ON jobs(queue, available_at);
112
159
  CREATE INDEX idx_jobs_reserved ON jobs(reserved_at);
160
+ CREATE INDEX idx_jobs_queue_available ON jobs(queue, available_at);
161
+ CREATE INDEX idx_jobs_reserved ON jobs(reserved_at);
162
+ ```
163
+
164
+ ## Persistence and Audit Mode
165
+
166
+ The `@gravito/stream` package supports an optional persistence layer (using SQLite or MySQL) for archiving job history and providing an audit trail.
167
+
168
+ ### Configuration
169
+
170
+ ```typescript
171
+ OrbitStream.configure({
172
+ // ... other config
173
+ persistence: {
174
+ adapter: new SQLitePersistence(DB), // or MySQLPersistence
175
+ archiveCompleted: true, // Archive jobs when they complete successfully
176
+ archiveFailed: true, // Archive jobs when they fail permanently
177
+ archiveEnqueued: true // (Audit Mode) Archive jobs immediately when pushed
178
+ }
179
+ })
113
180
  ```
114
181
 
182
+ ### Audit Mode (`archiveEnqueued: true`)
183
+
184
+ When Audit Mode is enabled, every job pushed to the queue is immediately written to the SQL archive with a `waiting` status. This happens in parallel with the main queue operation (Fire-and-Forget).
185
+
186
+ - **Benefit**: Provides a complete audit trail. Even if the queue driver (e.g., Redis) crashes and loses data, the SQL archive will contain the record of the job being enqueued.
187
+ - **Performance**: Designed to be non-blocking. The SQL write happens asynchronously and does not delay the `push()` operation.
188
+
115
189
  ## Standalone Worker
116
190
 
117
191
  ```bash
@@ -133,6 +207,13 @@ abstract class Job implements Queueable {
133
207
  onQueue(queue: string): this
134
208
  onConnection(connection: string): this
135
209
  delay(seconds: number): this
210
+
211
+ /**
212
+ * Set retry backoff strategy.
213
+ * @param seconds - Initial delay in seconds
214
+ * @param multiplier - Multiplier for each subsequent attempt (default: 2)
215
+ */
216
+ backoff(seconds: number, multiplier = 2): this
136
217
  }
137
218
  ```
138
219
 
@@ -145,6 +226,8 @@ class QueueManager {
145
226
  async pop(queue?: string, connection?: string): Promise<Job | null>
146
227
  async size(queue?: string, connection?: string): Promise<number>
147
228
  async clear(queue?: string, connection?: string): Promise<void>
229
+ async complete(job: Job): Promise<void>
230
+ async fail(job: Job, error: Error): Promise<void>
148
231
  registerJobClasses(jobClasses: Array<new (...args: unknown[]) => Job>): void
149
232
  }
150
233
  ```
@@ -153,9 +236,10 @@ class QueueManager {
153
236
 
154
237
  - **MemoryDriver** - in-memory (development)
155
238
  - **DatabaseDriver** - PostgreSQL/MySQL/SQLite
156
- - **RedisDriver** - delayed jobs supported
239
+ - **RedisDriver** - delayed jobs, priority queues, rate limiting, and DLQ support
157
240
  - **KafkaDriver** - topics and consumer groups
158
241
  - **SQSDriver** - standard/FIFO queues and long polling
242
+ - **RabbitMQDriver** - exchanges, queues, and advanced confirm mode
159
243
 
160
244
  ## License
161
245