What's the exact shutdown procedure of an Application?

My situation: I want to send one last round of messages (notifying about shutdown of the service) out of all my open cowboy websocket connections when my application is shut down (e.g. via SIGTERM).

From the documentation I get that Application.pre_stop should be used for this. Now what I’m concerned about is: If I use “cast” style messages here to send from my application process to the websocket processes that in turn send out the shutdown notices, are all messages actually always going to “make it out in time”? Or is it possible that the shutdown of the application somehow overtakes my final messages and I’d better used “call” style messaging to block in the pre_stop function until I got notice that all messages were sent?

1 Like

You are looking for connection draining.

2 Likes

Interesting! Do you know of documentation that details how to use this with Phoenix? I guess I just sort of assumed it did this by default.

Looking at the docs, not specifying the refs option should do it.

Doh! Good catch, thanks

Thanks, that looks helpful when using Plug, unfortunately, I’m currently using plain cowboy - will I need to change that?

https://ninenines.eu/docs/en/ranch/2.0/manual/ranch.wait_for_connections/

3 Likes