Comparison

mod_graceful_shutdown/mod_graceful_shutdown.lua @ 2170:4652a112a4ba

mod_graceful_shutdown: Experiment in improving the shutdown experience
author Kim Alvefur <zash@zash.se>
date Thu, 05 May 2016 15:43:01 +0200
child 4892:8dbaa5e753f3
comparison
equal deleted inserted replaced
2169:9fa588babbba 2170:4652a112a4ba
1 -- luacheck: ignore 122/prosody 113/prosody
2
3 local timer = require "util.timer";
4 local portman = require "core.portmanager";
5 local server = require "net.server";
6
7 module:set_global();
8 local orig_shutdown = prosody.shutdown;
9
10 local pause = module:get_option_number("shutdown_pause", 1);
11
12 function module.unload()
13 prosody.shutdown = orig_shutdown;
14 end
15
16 prosody.shutdown = coroutine.wrap(function (reason, code)
17 prosody.shutdown_reason = reason;
18 prosody.shutdown_code = code;
19 timer.add_task(pause, prosody.shutdown);
20 coroutine.yield(true, "shutdown initiated");
21 -- Close c2s ports, stop accepting new connections
22 portman.deactivate("c2s");
23 -- Close all c2s sessions
24 for _, sess in pairs(prosody.full_sessions) do
25 sess:close{ condition = "system-shutdown", text = reason }
26 end
27 -- Wait for notifications to be sent
28 coroutine.yield(pause);
29 -- Event for everything else to shut down
30 prosody.events.fire_event("server-stopping", {
31 reason = reason;
32 code = code;
33 });
34 -- And wait
35 coroutine.yield(pause);
36 -- And stop main event loop
37 server.setquitting(true);
38 -- And wait for death
39 coroutine.yield(pause * 3);
40 -- you came back? die zombie!
41 os.exit(1);
42 end);