How best to implement a graceful shutdown?
I found
client.destroy()
but it seems to be an immediate shutdown rather than a graceful one based on the source code. I also looked for existing third-party packages that offer this but found none.21 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
- ✅
Marked as resolved by staffTo quote google,
a graceful shutdown is a process where a program or server stops its operations in a controlled manner, ensuring that all in-flight operations are completed before the system is fully shut downsimply calling
client.destroy()
is an immediate shutdown that doesn't allow in-flight operations to complete
I'm not saying there's anything wrong with that, especially since a graceful shutdown would depend on implementation details. I just want to figure out how to do such a thing myself.the only operations are the heartbeat and requests for data and neither would make sense to complete before exiting the connection
unless you mean the rest requests which is its own separate thing
one example would be slash commands, allowing those to fully execute and then exiting
which yes could easily involve rest requests
it destroys the rest client as well
and the "client" as well
does the websockets connection not have a way to replay events that were missed? i thought there was something like that
it doesn't take 5 seconds to restart
some will be lost, but not all
what's the actual issue you're trying to solve? sounds like an xy question
wdym by an xy question?
a question where you ask about your solution rather than your actual problem
well, my problem is that errors are thrown when I use
client.destroy()
because in-flight interactions are no longer able to access everything they need from the discordjs client.
I want to make the shutdown "graceful" to resolve that issue, if you have a better solution I'm open to ityou could just not destroy the client if there's still an interaction being processed
sometimes it'll restart, other times would be a shutdown. but i'd like to handle the restart case as well if possible.
but then new interactions will be incoming and that'll keep it connected forever
new ones yes
in-flight ones should be able to complete
one that i have already received but haven't finished handling & replying to
several of them rely on the client to still be up and running, so errors are thrown when it's already destroyed
imo you could
- push to an array of promises that resolve when a given interaction is completed (and then it's removed from the array)
- when you want to shutdown, Promise.all the array before shutting down
- success
those are interactions that can easily be completed
also thanks, how would this compare/include to dropping the interactionCreate listener? and would dropping that listener make it impossible to handle the restart case where discord can replay missed events?
pretty sure you can only replay when you're resuming
not when starting a connection
yea that's what I just found

would it be possible to save that value and resume instead of start a new one?
it seems very ideal to do so based on the flow chart in the discord docs

because it would replay all missed events, rather than simply losing them outright
allowing me to salvage as many interactions as possible
but then you won't receive any data from the identification flow
so i guess i would need to cache that as well
this is sounding more complicated than it's worth
but this much seems worth it to me, thanks again
i hate how every time i ask here i have to fight my way to an answer that could've been given at the start
yeah thing is we need more data than just the attempted solution
I can only give that answer knowing you only want to handle interactions
and knowing what you want to do when receiving new interactions while shutting down
etc
I guess the tone of messages and how quick people are to latch on any excuse to not answer makes it feel like more than that.
I did thank them twice, but yes I am frustrated.
But either way I'll drop it, nothing productive is going to come of this. Sorry for my tone.