Repository navigation
feat(plugin-sharder): add shard exit hooks and manager destroy hooks - #287
Merged
Merged
Conversation
addDispatchWrapper runs code around the processing of each dispatch received from Discord, so a tracing span or an AsyncLocalStorage store covers the cache writes and the listeners. addDestroyHook runs at the end of every destroy(), once the shards are disconnected and the dispatches processed, to flush what is left before the process exits. Refs #261
A shard stops with process.exit, which skips beforeExit, so whatever a library flushes there is lost. ShardClient#addExitHook runs a hook first, on exit(), restart(), a close from the manager, and when the manager is gone. ShardManager#addDestroyHook runs one at the end of destroy(), once the shards are closed. Refs #261
Architecture Data flow Review ✨ Nothing new to flag · reviewed Tip After a push, tick Redraw to draw the latest commit 🪧 More tips
Thanks for using PR Lens! It's free for OSS. If it helped, a star helps other teams find it. |
Contributor
There was a problem hiding this comment.
ℹ️ No critical issues — one docs rough edge inline.
Reviewed changes
Reviewed the full diff (5 files) of the exit and destroy hooks for plugin-sharder.
ShardClient#addExitHook: hooks run in#stop(exit(),restart(), and the managerOp.Closepath after the close handler) and on the orphanedonDisconnectpath, before the shard signals its status. Errors go through#report, and the hook set is copied so hooks added mid-run wait for the next stop.ShardManager#addDestroyHook:destroy()now wraps closing intry/finallyso the hooks run even when closing a shard rejects, and the original rejection still propagates.- Tests, README and changeset:
tests/hooks.test.tscovers ordering, the manager-close path, failing and removed hooks, the manager-gone path and destroy when closing fails. Theminorchangeset matches the new public API.ShardExitHookandShardManagerDestroyHookare exported throughexport *insrc/index.ts.
claude-sonnet-5-5 | 𝕏
7 of 11 tasks
…y close Share the exit hooks in progress across the paths that stop a shard, so the manager going away during exit() waits for the first flush instead of starting another and exiting before it is done. Let every shard close settle before the destroy hooks run, and keep the JSDoc of ShardManagerOptions attached to it.
@wolfstar/plugin-api
@wolfstar/plugin-broker
@wolfstar/plugin-cache
@wolfstar/plugin-gateway
@wolfstar/plugin-i18next
@wolfstar/plugin-logger
@wolfstar/plugin-opentelemetry
@wolfstar/plugin-scheduled-tasks
@wolfstar/plugin-sharder
@wolfstar/plugin-subcommands-advanced
commit: |
Contributor
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Reviewed the delta since the prior Pullfrog review (bcc74e1 → 7ebafc2).
- Shared exit-hook run:
ShardClient#runExitHooksmemoizes the in-progress run in#exiting, soexit()and the manager-disconnect path share one run. The hooks run once and neither path callstransport.exitbefore they finish. - Destroy waits for every close:
ShardManager#destroyusesPromise.allSettled, rethrows the first rejection, and runs the destroy hooks infinally. No shard is still stopping when the hooks run. - Docs placement fix:
ShardManagerDestroyHooknow sits above theShardManagerOptionsdoc block, which fixes the earlier inline finding. - Tests: Added an overlapping-stops test and a one-shard-fails-to-close ordering test in
tests/hooks.test.ts.
claude-sonnet-5-5 | 𝕏
1 of 8 tasks
3 of 11 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

🔗 Linked issue
Refs #261
🧭 Context
Second half of #261, in three PRs:
plugin-gatewaydispatch wrappers and destroy hooksplugin-sharderexit and destroy hooksplugin-opentelemetrydispatch spans and theflushTraces()wiring, which resolves request: a plugin that traces the bot with OpenTelemetry #261A shard stops with
process.exit, which skipsbeforeExit. The OpenTelemetry preload exports its last spans onbeforeExitorSIGTERM, so a shard that exits or restarts loses the batch it has not exported yet. Nothing inShardClientgives code a chance to flush first.📚 Description
ShardClient#addExitHook(hook)runs a hook before the shard stops: onexit()andrestart(), after the close handler when the manager closes the shard, and when the manager is gone and nothing listens todisconnect. The hooks run before the shard signals that it is stopping, so they count towards the manager's close timeout.ShardManager#addDestroyHook(hook)runs a hook at the end ofdestroy(), once the shards are closed, even when closing one failed.In both, a hook that throws is reported through
errorand the next hooks still run. Each call returns a function that removes the hook. Nothing changes for a shard or a manager that adds none.Key changes
ShardClient:addExitHookand theShardExitHooktype.ShardManager:addDestroyHookand theShardManagerDestroyHooktype.tests/hooks.test.ts: hook order againstshardExit, a close from the manager, the manager going away, failing and removed hooks, and destroy hooks when closing fails.minorchangeset.Type of Change
Pre-flight Checklist
pnpm changeset(orpnpm changeset add --emptyif no release is needed). See .changeset/README.md.

Confidence Score: 5/5
The reviewed changes appear safe to merge.Reviews (2) · Last reviewed commit: "fix(plugin-sharder): run exit hooks once..." · Reviewed by Greptile