Skip to content

Commit

Permalink
fix(shuttle): Handle missing group key (farcasterxyz#2133)
Browse files Browse the repository at this point in the history
## Motivation

We didn't handle the case where the group key wasn't already created.

## Change Summary

Handle it.

## Merge Checklist

- [x] PR title adheres to the [conventional
commits](https://www.conventionalcommits.org/en/v1.0.0/) standard
- [x] PR has a
[changeset](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#35-adding-changesets)
- [x] PR has been tagged with a change label(s) (i.e. documentation,
feature, bugfix, or chore)
- [ ] PR includes
[documentation](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#32-writing-docs)
if necessary.
- [x] All [commits have been
signed](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#22-signing-commits)


<!-- start pr-codex -->

---

## PR-Codex overview
This PR focuses on gracefully handling "no such key" errors when
querying a group on the first start in the `shuttle` package.

### Detailed summary
- Added error handling for "no such key" scenario
- Skips group creation if key doesn't exist
- Improved robustness in handling ReplyError

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

<!-- end pr-codex -->

---------

Co-authored-by: Ken Goldfarb <[email protected]>
  • Loading branch information
sds and kengoldfarb authored Jul 8, 2024
1 parent a7fdaa1 commit 4518058
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/calm-rings-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farcaster/shuttle": patch
---

Gracefully handle "no such key" when querying group on first start
12 changes: 11 additions & 1 deletion packages/shuttle/src/shuttle/eventStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@ export class EventStreamConnection {
* Creates a consumer group for the given stream.
*/
async createGroup(key: string, consumerGroup: string) {
// Check if the group already exists
try {
// Check if the group already exists
const groups = (await this.client.xinfo("GROUPS", key)) as [string, string][];
if (groups.some(([_fieldName, groupName]) => groupName === consumerGroup)) return;
} catch (e: unknown) {
if (typeof e === "object" && e !== null && e instanceof ReplyError) {
if ("message" in e && (e.message as string).startsWith("ERR no such key")) {
// Ignore if the group hasn't been created yet
} else {
throw e;
}
}
}

try {
// Otherwise create the group
return await this.client.xgroup("CREATE", key, consumerGroup, "0", "MKSTREAM");
} catch (e: unknown) {
Expand Down

0 comments on commit 4518058

Please sign in to comment.