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
d.js toolkit
d.js toolkit3d ago
- 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 staff
ElJay
ElJayOP3d ago
To 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 down
simply 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.
Amgelo
Amgelo3d ago
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
ElJay
ElJayOP3d ago
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
Amgelo
Amgelo3d ago
what's the actual issue you're trying to solve? sounds like an xy question
ElJay
ElJayOP3d ago
wdym by an xy question?
Amgelo
Amgelo3d ago
a question where you ask about your solution rather than your actual problem
ElJay
ElJayOP3d ago
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 it
Amgelo
Amgelo3d ago
you could just not destroy the client if there's still an interaction being processed
ElJay
ElJayOP3d ago
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
Amgelo
Amgelo3d ago
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
ElJay
ElJayOP3d ago
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?
Amgelo
Amgelo3d ago
pretty sure you can only replay when you're resuming not when starting a connection
ElJay
ElJayOP3d ago
yea that's what I just found
No description
ElJay
ElJayOP3d ago
would it be possible to save that value and resume instead of start a new one?
ElJay
ElJayOP3d ago
it seems very ideal to do so based on the flow chart in the discord docs
No description
ElJay
ElJayOP3d ago
because it would replay all missed events, rather than simply losing them outright allowing me to salvage as many interactions as possible
Amgelo
Amgelo3d ago
but then you won't receive any data from the identification flow
ElJay
ElJayOP3d ago
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
Amgelo
Amgelo3d ago
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
ElJay
ElJayOP3d ago
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.

Did you find this page helpful?