-
Notifications
You must be signed in to change notification settings - Fork 0
Handle channel close and consumer cancel cases #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ A lib for rabbit and sqs queues | |
|
|
||
| Requires `amqplib` to be installed separately. If you only need RabbitMQ support you can avoid also needing to install `@aws-sdk` packages by importing from `"@loke/queue-kit/rabbit"`. | ||
|
|
||
| Handling a work queue: | ||
| ### Handling a work queue | ||
|
|
||
| ```ts | ||
| import { RabbitHelper } from "@loke/queue-kit"; // or "@loke/queue-kit/rabbit" | ||
|
|
@@ -26,13 +26,23 @@ async function main() { | |
|
|
||
| const aborter = new AbortController(); | ||
|
|
||
| const doneP = await rabbitHelper.handleQueue({ | ||
| queueName: "work-queue", | ||
| handler: async (msg) => { | ||
| await doWork(msg.body); | ||
| }, | ||
| signal: aborter.signal, | ||
| }); | ||
| const doneP = (async () => { | ||
| // We loop here in case the rabbit channel closes and we need to restart | ||
| // the queue handler. | ||
| while (!aborter.signal.aborted) { | ||
| try { | ||
| await rabbitHelper.handleQueue({ | ||
| queueName: "work-queue", | ||
| handler: async (msg) => { | ||
| await doWork(msg.body); | ||
| }, | ||
| signal: aborter.signal, | ||
| }); | ||
| } catch (err) { | ||
| logger.error("Error with spend tracker queue", err); | ||
| } | ||
| } | ||
| })(); | ||
|
|
||
| await stopSignal(); | ||
|
|
||
|
|
@@ -44,7 +54,11 @@ async function main() { | |
| main(); | ||
| ``` | ||
|
|
||
| **Breaking change in 2.x:** `assertWorkQueue` now requires a `retryDelay` option. This is the delay between retries when a message fails to be processed. To achieve this a dead letter queue is created and attached to the work queue (via the default direct exchange). Because old queues can't be changed via assertQueue, a new will need to be created. | ||
| **Breaking change in 2.x:** `assertWorkQueue` now requires a `retryDelay` | ||
| option. This is the delay between retries when a message fails to be processed. | ||
| To achieve this a dead letter queue is created and attached to the work queue | ||
| (via the default direct exchange). Because old queues can't be changed via | ||
| assertQueue, a new one will need to be created. | ||
|
|
||
| ```ts | ||
| await rabbitHelper.assertWorkQueue("new-queue", { retryDelay: 1000 }); | ||
|
|
@@ -71,7 +85,67 @@ const doneP = await Promise.all([ | |
| ]); | ||
| ``` | ||
|
|
||
| Publishing events: | ||
| ### Handling a subscription queue | ||
|
|
||
| ```ts | ||
| import { RabbitHelper } from "@loke/queue-kit"; // or "@loke/queue-kit/rabbit" | ||
| import amqp from "amqplib"; // must be installed separately | ||
|
|
||
| async function main() { | ||
| const amqpConnection = await amqp.connect("amqp://localhost"); | ||
|
|
||
| const rabbitHelper = new RabbitHelper({ | ||
| amqpConnection, | ||
| logger: console, | ||
| }); | ||
|
|
||
| await rabbitHelper.assertExchange(); | ||
|
|
||
| const aborter = new AbortController(); | ||
|
|
||
| const doneP = (async () => { | ||
| // We loop here in case the rabbit channel closes and we need to restart | ||
| // the queue handler. | ||
| // | ||
| // Because this is a subscription queue, we need to create a new queue each | ||
| // time we start the handler. This is because the queue will be deleted when | ||
| // the channel closes. If the resource the queue is updating could have | ||
| // become stale while the channel was closed, we need to handle that too. | ||
| // | ||
| // In this example we update a cache with the latest value from the queue. | ||
| // If we loose our our connection to the queue, we need to clear the cache | ||
| // and start again. | ||
| while (!aborter.signal.aborted) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar question around retry limits / sleeps in the loop as the other comment |
||
| const { queue } = await rabbitHelper.createSubscriptionQueue(); | ||
| await rabbitHelper.bindQueue(queue, "thing.*"); | ||
|
|
||
| await clearCache(); | ||
|
|
||
| try { | ||
| await rabbitHelper.handleQueue({ | ||
| queueName: queue, | ||
| handler: async (msg) => { | ||
| await updateCache(msg.body); | ||
| }, | ||
| signal: aborter.signal, | ||
| }); | ||
| } catch (err) { | ||
| logger.error("Error with spend tracker queue", err); | ||
| } | ||
| } | ||
| })(); | ||
|
|
||
| await stopSignal(); | ||
|
|
||
| aborter.abort(); | ||
|
|
||
| await doneP; | ||
| } | ||
|
|
||
| main(); | ||
| ``` | ||
|
|
||
| ### Publishing events | ||
|
|
||
| ```ts | ||
| import { RabbitHelper } from "@loke/queue-kit"; // or "@loke/queue-kit/rabbit" | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| export { RabbitData, RabbitHelper } from "./rabbit"; | ||
| export { SQSData, SQSHelper } from "./sqs"; | ||
| export { Logger, MessageHandler, AbortSignal } from "./common"; | ||
| export { Logger, MessageHandler } from "./common"; | ||
| export { registerMetrics } from "./metrics"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to include a sleep interval or a retry limit in the sample code here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine without