@amqp-contract/worker 0.6.0 → 0.8.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 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
@@ -29,9 +30,9 @@ pnpm add @amqp-contract/worker
29
30
  ### Basic Usage
30
31
 
31
32
  ```typescript
32
- import { TypedAmqpWorker } from '@amqp-contract/worker';
33
- import type { Logger } from '@amqp-contract/core';
34
- import { contract } from './contract';
33
+ import { TypedAmqpWorker } from "@amqp-contract/worker";
34
+ import type { Logger } from "@amqp-contract/core";
35
+ import { contract } from "./contract";
35
36
 
36
37
  // Optional: Create a logger implementation
37
38
  const logger: Logger = {
@@ -46,7 +47,7 @@ const worker = await TypedAmqpWorker.create({
46
47
  contract,
47
48
  handlers: {
48
49
  processOrder: async (message) => {
49
- console.log('Processing order:', message.orderId);
50
+ console.log("Processing order:", message.orderId);
50
51
 
51
52
  // Your business logic here
52
53
  await processPayment(message);
@@ -55,7 +56,7 @@ const worker = await TypedAmqpWorker.create({
55
56
  // If an exception is thrown, the message is automatically requeued
56
57
  },
57
58
  },
58
- urls: ['amqp://localhost'],
59
+ urls: ["amqp://localhost"],
59
60
  logger, // Optional: logs message consumption and errors
60
61
  });
61
62
 
@@ -67,7 +68,33 @@ const worker = await TypedAmqpWorker.create({
67
68
 
68
69
  ### Advanced Features
69
70
 
70
- For advanced features like prefetch configuration and batch processing, see the [Worker Usage Guide](https://btravers.github.io/amqp-contract/guide/worker-usage).
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,21 +113,23 @@ handlers: {
86
113
  // Message acknowledged automatically on success
87
114
  } catch (error) {
88
115
  // Exception automatically caught by worker
89
- // Message is requeued for retry
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
+ };
93
121
  }
94
122
  ```
95
123
 
96
124
  **Error Types:**
97
125
 
98
- Worker defines error classes for internal use:
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
- These errors are logged but **handlers don't need to use them** - just throw standard exceptions.
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