mirror of
https://github.com/bjc/prosody.git
synced 2025-04-04 13:47:41 +03:00
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
This commit is contained in:
parent
b3e293118d
commit
7dc676faad
1 changed files with 41 additions and 10 deletions
|
@ -422,19 +422,49 @@ function startup.init_data_store()
|
||||||
require "prosody.core.storagemanager";
|
require "prosody.core.storagemanager";
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local running_state = require "util.fsm".new({
|
||||||
|
default_state = "uninitialized";
|
||||||
|
transitions = {
|
||||||
|
{ name = "begin_startup", from = "uninitialized", to = "starting" };
|
||||||
|
{ name = "finish_startup", from = "starting", to = "running" };
|
||||||
|
{ name = "begin_shutdown", from = { "running", "starting" }, to = "stopping" };
|
||||||
|
{ name = "finish_shutdown", from = "stopping", to = "stopped" };
|
||||||
|
};
|
||||||
|
handlers = {
|
||||||
|
transitioned = function (transition)
|
||||||
|
prosody.state = transition.to;
|
||||||
|
end;
|
||||||
|
};
|
||||||
|
state_handlers = {
|
||||||
|
starting = function ()
|
||||||
|
prosody.log("debug", "Firing server-starting event");
|
||||||
|
prosody.events.fire_event("server-starting");
|
||||||
|
prosody.start_time = os.time();
|
||||||
|
end;
|
||||||
|
running = function ()
|
||||||
|
prosody.log("debug", "Startup complete, firing server-started");
|
||||||
|
prosody.events.fire_event("server-started");
|
||||||
|
end;
|
||||||
|
};
|
||||||
|
}):init();
|
||||||
|
|
||||||
function startup.prepare_to_start()
|
function startup.prepare_to_start()
|
||||||
log("info", "Prosody is using the %s backend for connection handling", server.get_backend());
|
log("info", "Prosody is using the %s backend for connection handling", server.get_backend());
|
||||||
|
|
||||||
-- Signal to modules that we are ready to start
|
-- Signal to modules that we are ready to start
|
||||||
prosody.started = require "prosody.util.promise".new(function (resolve)
|
prosody.started = require "prosody.util.promise".new(function (resolve)
|
||||||
prosody.events.add_handler("server-started", function ()
|
if prosody.state == "running" then
|
||||||
resolve();
|
resolve();
|
||||||
end);
|
else
|
||||||
prosody.log("debug", "Firing server-starting event");
|
prosody.events.add_handler("server-started", function ()
|
||||||
prosody.events.fire_event("server-starting");
|
resolve();
|
||||||
prosody.start_time = os.time();
|
end);
|
||||||
|
end
|
||||||
end):catch(function (err)
|
end):catch(function (err)
|
||||||
prosody.log("error", "Prosody startup error: %s", err);
|
prosody.log("error", "Prosody startup error: %s", err);
|
||||||
end);
|
end);
|
||||||
|
|
||||||
|
running_state:begin_startup();
|
||||||
end
|
end
|
||||||
|
|
||||||
function startup.init_global_protection()
|
function startup.init_global_protection()
|
||||||
|
@ -479,10 +509,7 @@ function startup.log_greeting()
|
||||||
end
|
end
|
||||||
|
|
||||||
function startup.notify_started()
|
function startup.notify_started()
|
||||||
require "prosody.util.timer".add_task(0, function ()
|
running_state:finish_startup();
|
||||||
prosody.log("debug", "Firing server-started event");
|
|
||||||
prosody.events.fire_event("server-started");
|
|
||||||
end);
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Override logging config (used by prosodyctl)
|
-- Override logging config (used by prosodyctl)
|
||||||
|
@ -648,11 +675,15 @@ function startup.cleanup()
|
||||||
end
|
end
|
||||||
|
|
||||||
function startup.shutdown()
|
function startup.shutdown()
|
||||||
|
running_state:begin_shutdown();
|
||||||
|
|
||||||
prosody.log("info", "Shutting down...");
|
prosody.log("info", "Shutting down...");
|
||||||
startup.cleanup();
|
startup.cleanup();
|
||||||
prosody.events.fire_event("server-stopped");
|
prosody.events.fire_event("server-stopped");
|
||||||
prosody.log("info", "Shutdown complete");
|
|
||||||
|
|
||||||
|
running_state:finish_shutdown();
|
||||||
|
|
||||||
|
prosody.log("info", "Shutdown complete");
|
||||||
prosody.log("debug", "Shutdown reason was: %s", prosody.shutdown_reason or "not specified");
|
prosody.log("debug", "Shutdown reason was: %s", prosody.shutdown_reason or "not specified");
|
||||||
prosody.log("debug", "Exiting with status code: %d", prosody.shutdown_code or 0);
|
prosody.log("debug", "Exiting with status code: %d", prosody.shutdown_code or 0);
|
||||||
server.setquitting(true);
|
server.setquitting(true);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue