Mercurial > prosody-modules
annotate mod_graceful_shutdown/mod_graceful_shutdown.lua @ 5165:f37d6adce06e
mod_jsxc: Add plugin installer metadata
Needed since this module has extra resources and dependencies
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 16 Feb 2023 17:06:23 +0100 |
parents | d4ce29c772ac |
children |
rev | line source |
---|---|
2170
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- luacheck: ignore 122/prosody 113/prosody |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local timer = require "util.timer"; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local portman = require "core.portmanager"; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local server = require "net.server"; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 module:set_global(); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local orig_shutdown = prosody.shutdown; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local pause = module:get_option_number("shutdown_pause", 1); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 function module.unload() |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 prosody.shutdown = orig_shutdown; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 end |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 prosody.shutdown = coroutine.wrap(function (reason, code) |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 prosody.shutdown_reason = reason; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 prosody.shutdown_code = code; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 timer.add_task(pause, prosody.shutdown); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 coroutine.yield(true, "shutdown initiated"); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 -- Close c2s ports, stop accepting new connections |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 portman.deactivate("c2s"); |
4892
8dbaa5e753f3
mod_graceful_shutdown: Deactivate direct TLS c2s along with plain c2s
Kim Alvefur <zash@zash.se>
parents:
2170
diff
changeset
|
23 portman.deactivate("legacy_ssl"); |
8dbaa5e753f3
mod_graceful_shutdown: Deactivate direct TLS c2s along with plain c2s
Kim Alvefur <zash@zash.se>
parents:
2170
diff
changeset
|
24 portman.deactivate("c2s_direct_tls"); |
8dbaa5e753f3
mod_graceful_shutdown: Deactivate direct TLS c2s along with plain c2s
Kim Alvefur <zash@zash.se>
parents:
2170
diff
changeset
|
25 |
4893
d4ce29c772ac
mod_graceful_shutdown: Close multiplex ports
Kim Alvefur <zash@zash.se>
parents:
4892
diff
changeset
|
26 -- Close multiplexing ports to ensure c2s is not reachable via those either |
d4ce29c772ac
mod_graceful_shutdown: Close multiplex ports
Kim Alvefur <zash@zash.se>
parents:
4892
diff
changeset
|
27 portman.deactivate("multiplex"); |
d4ce29c772ac
mod_graceful_shutdown: Close multiplex ports
Kim Alvefur <zash@zash.se>
parents:
4892
diff
changeset
|
28 portman.deactivate("multiplex_ssl"); |
d4ce29c772ac
mod_graceful_shutdown: Close multiplex ports
Kim Alvefur <zash@zash.se>
parents:
4892
diff
changeset
|
29 portman.deactivate("proxy"); -- mod_net_proxy |
d4ce29c772ac
mod_graceful_shutdown: Close multiplex ports
Kim Alvefur <zash@zash.se>
parents:
4892
diff
changeset
|
30 |
2170
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 -- Close all c2s sessions |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 for _, sess in pairs(prosody.full_sessions) do |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 sess:close{ condition = "system-shutdown", text = reason } |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 end |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 -- Wait for notifications to be sent |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 coroutine.yield(pause); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 -- Event for everything else to shut down |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 prosody.events.fire_event("server-stopping", { |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 reason = reason; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 code = code; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 }); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 -- And wait |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 coroutine.yield(pause); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 -- And stop main event loop |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 server.setquitting(true); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 -- And wait for death |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 coroutine.yield(pause * 3); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 -- you came back? die zombie! |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 os.exit(1); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 end); |