@amqp-contract/worker 0.7.0 → 0.9.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 +33 -4
- package/dist/index.cjs +397 -95
- package/dist/index.d.cts +362 -99
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +362 -99
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +394 -96
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +995 -213
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -20,6 +20,7 @@ pnpm add @amqp-contract/worker
|
|
|
20
20
|
|
|
21
21
|
- ✅ **Type-safe message consumption** — Handlers are fully typed based on your contract
|
|
22
22
|
- ✅ **Automatic validation** — Messages are validated before reaching your handlers
|
|
23
|
+
- ✅ **Automatic retry with exponential backoff** — Built-in retry mechanism using RabbitMQ TTL+DLX pattern
|
|
23
24
|
- ✅ **Prefetch configuration** — Control message flow with per-consumer prefetch settings
|
|
24
25
|
- ✅ **Batch processing** — Process multiple messages at once for better throughput
|
|
25
26
|
- ✅ **Automatic reconnection** — Built-in connection management with failover support
|
|
@@ -67,7 +68,33 @@ const worker = await TypedAmqpWorker.create({
|
|
|
67
68
|
|
|
68
69
|
### Advanced Features
|
|
69
70
|
|
|
70
|
-
For advanced features like prefetch configuration
|
|
71
|
+
For advanced features like prefetch configuration, batch processing, and **automatic retry with exponential backoff**, see the [Worker Usage Guide](https://btravers.github.io/amqp-contract/guide/worker-usage).
|
|
72
|
+
|
|
73
|
+
#### Retry with Exponential Backoff
|
|
74
|
+
|
|
75
|
+
Enable automatic retry for failed messages:
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
const worker = await TypedAmqpWorker.create({
|
|
79
|
+
contract,
|
|
80
|
+
handlers: {
|
|
81
|
+
processOrder: async (message) => {
|
|
82
|
+
// If this throws, message is automatically retried with exponential backoff
|
|
83
|
+
await processPayment(message);
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
urls: ["amqp://localhost"],
|
|
87
|
+
retry: {
|
|
88
|
+
maxRetries: 3, // Retry up to 3 times
|
|
89
|
+
initialDelayMs: 1000, // Start with 1 second delay
|
|
90
|
+
maxDelayMs: 30000, // Max 30 seconds between retries
|
|
91
|
+
backoffMultiplier: 2, // Double the delay each time
|
|
92
|
+
jitter: true, // Add randomness to prevent thundering herd
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The retry mechanism uses RabbitMQ's native TTL and Dead Letter Exchange pattern, so it doesn't block the consumer during retry delays. See the [Error Handling and Retry](https://btravers.github.io/amqp-contract/guide/worker-usage#error-handling-and-retry) section in the guide for complete details.
|
|
71
98
|
|
|
72
99
|
## Defining Handlers Externally
|
|
73
100
|
|
|
@@ -86,7 +113,8 @@ handlers: {
|
|
|
86
113
|
// Message acknowledged automatically on success
|
|
87
114
|
} catch (error) {
|
|
88
115
|
// Exception automatically caught by worker
|
|
89
|
-
//
|
|
116
|
+
// With retry configured: message is retried with exponential backoff
|
|
117
|
+
// Without retry: message is immediately requeued
|
|
90
118
|
throw error;
|
|
91
119
|
}
|
|
92
120
|
};
|
|
@@ -95,12 +123,13 @@ handlers: {
|
|
|
95
123
|
|
|
96
124
|
**Error Types:**
|
|
97
125
|
|
|
98
|
-
Worker defines error classes
|
|
126
|
+
Worker defines error classes:
|
|
99
127
|
|
|
100
128
|
- `TechnicalError` - Runtime failures (parsing, processing)
|
|
101
129
|
- `MessageValidationError` - Message fails schema validation
|
|
130
|
+
- `RetryableError` - Optional error class for explicit retry signaling (all errors are retryable by default when retry is configured)
|
|
102
131
|
|
|
103
|
-
|
|
132
|
+
**Handlers don't need to use these error classes** - just throw standard exceptions. The worker handles retry automatically based on your configuration.
|
|
104
133
|
|
|
105
134
|
## API
|
|
106
135
|
|