You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+200Lines changed: 200 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -244,6 +244,206 @@ Both publishers and consumers accept a queue name and configuration as parameter
244
244
245
245
If you do not want to create a new queue/topic, you can set `queueLocator` field for `queueConfiguration`. In that case `message-queue-toolkit` will not attempt to create a new queue or topic, and instead throw an error if they don't already exist.
When using `locatorConfig` to reference existing queues or topics, the default behavior is to fail immediately if the resource doesn't exist. However, in some deployment scenarios (especially with cross-service dependencies), resources may not be available at startup time.
250
+
251
+
The `startupResourcePolling` option within `locatorConfig` enables "eventual consistency mode" where consumers poll for resources to become available instead of failing immediately.
252
+
253
+
### Use Case: Cross-Service Dependencies
254
+
255
+
Consider two services that need to subscribe to each other's topics:
256
+
-**Service A** needs to subscribe to **Service B's** topic
257
+
-**Service B** needs to subscribe to **Service A's** topic
258
+
259
+
Without eventual consistency mode, neither service can deploy first because the other's topic doesn't exist yet. With `startupResourcePolling`, both services can start simultaneously and wait for the other's resources to appear.
enabled: true, // Enable polling for resource availability
274
+
pollingIntervalMs: 5000, // Check every 5 seconds (default: 5000)
275
+
timeoutMs: 300000, // Fail after 5 minutes (required)
276
+
}
277
+
}
278
+
})
279
+
```
280
+
281
+
### Configuration Options
282
+
283
+
| Option | Type | Default | Description |
284
+
|--------|------|---------|-------------|
285
+
|`enabled`|`boolean`| - | Must be set to `true` to enable polling |
286
+
|`pollingIntervalMs`|`number`|`5000`| Interval between availability checks (ms) |
287
+
|`timeoutMs`|`number \| NO_TIMEOUT`| - (required) | Maximum wait time before throwing `StartupResourcePollingTimeoutError`. Use `NO_TIMEOUT` to poll indefinitely |
288
+
|`throwOnTimeout`|`boolean`|`true`| When `true`, throws error on timeout. When `false`, reports error via errorReporter, resets timeout counter, and continues polling |
289
+
|`nonBlocking`|`boolean`|`false`| When `true`, `init()` returns immediately if resource is not available, and polling continues in the background. A callback is invoked when the resource becomes available |
// Production - with timeout (throws error when timeout reached)
297
+
{
298
+
locatorConfig: {
299
+
queueUrl: '...',
300
+
startupResourcePolling: {
301
+
enabled: true,
302
+
timeoutMs: 5*60*1000, // 5 minutes
303
+
}
304
+
}
305
+
}
306
+
307
+
// Production - report timeout but keep trying
308
+
// Useful when you want visibility into prolonged unavailability without failing
309
+
{
310
+
locatorConfig: {
311
+
queueUrl: '...',
312
+
startupResourcePolling: {
313
+
enabled: true,
314
+
timeoutMs: 5*60*1000, // Report every 5 minutes
315
+
throwOnTimeout: false, // Don't throw, just report and continue
316
+
}
317
+
}
318
+
}
319
+
320
+
// Development/Staging - poll indefinitely
321
+
{
322
+
locatorConfig: {
323
+
queueUrl: '...',
324
+
startupResourcePolling: {
325
+
enabled: true,
326
+
timeoutMs: NO_TIMEOUT,
327
+
}
328
+
}
329
+
}
330
+
331
+
// Custom timeout and interval
332
+
{
333
+
locatorConfig: {
334
+
queueUrl: '...',
335
+
startupResourcePolling: {
336
+
enabled: true,
337
+
pollingIntervalMs: 10000,
338
+
timeoutMs: 10*60*1000, // 10 minutes
339
+
}
340
+
}
341
+
}
342
+
343
+
// Non-blocking mode - service starts immediately, polling continues in background
344
+
// Useful when you want the service to be available even if dependencies are not ready
345
+
{
346
+
locatorConfig: {
347
+
queueUrl: '...',
348
+
startupResourcePolling: {
349
+
enabled: true,
350
+
timeoutMs: 5*60*1000,
351
+
nonBlocking: true, // init() resolves immediately
352
+
}
353
+
}
354
+
}
355
+
```
356
+
357
+
### Non-Blocking Mode
358
+
359
+
When `nonBlocking: true` is set, `init()` returns immediately if the resource is not available on the first check. Polling continues in the background, and you can use callbacks to be notified when resources become available.
360
+
361
+
**Important behaviors:**
362
+
- If the resource IS immediately available, `init()` returns normally with the resource ready
363
+
- If the resource is NOT immediately available, `init()` returns with `resourcesReady: false` (for SNS/SQS) or `queueArn: undefined` (for SQS)
364
+
- Background polling continues and invokes the callback when the resource becomes available
365
+
- For SNS+SQS consumers, the callback is only invoked when BOTH topic and queue are available
if (errorinstanceofStartupResourcePollingTimeoutError) {
442
+
console.error(`Resource ${error.resourceName} not available after ${error.timeoutMs}ms`)
443
+
}
444
+
}
445
+
```
446
+
247
447
## SQS Policy Configuration
248
448
249
449
SQS queues can be configured with access policies to control who can send messages to and receive messages from the queue. The `policyConfig` parameter allows you to define these policies when creating or updating SQS queues.
0 commit comments