From 5ce1fe2603abf0e49b396a34bfb3b11a9ccd2e27 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 1 Apr 2023 11:56:38 +0100 Subject: util.startup: Add prosody.started promise to easily execute code after startup To avoid a race where server-started fires before the promise function body is run (on next tick), I moved server-started to fire on the next tick, which seems sensible anyway. Errors are logged, I'm not sure if we ought to be doing something more here. I'm sure we'll find out. --- util/startup.lua | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'util') diff --git a/util/startup.lua b/util/startup.lua index c000bdb1..70e03a64 100644 --- a/util/startup.lua +++ b/util/startup.lua @@ -430,8 +430,16 @@ end function startup.prepare_to_start() log("info", "Prosody is using the %s backend for connection handling", server.get_backend()); -- Signal to modules that we are ready to start - prosody.events.fire_event("server-starting"); - prosody.start_time = os.time(); + prosody.started = require "util.promise".new(function (resolve) + prosody.events.add_handler("server-started", function () + resolve(); + end); + prosody.log("debug", "Firing server-starting event"); + prosody.events.fire_event("server-starting"); + prosody.start_time = os.time(); + end):catch(function (err) + prosody.log("error", "Prosody startup error: %s", err); + end); end function startup.init_global_protection() @@ -476,7 +484,10 @@ function startup.log_greeting() end function startup.notify_started() - prosody.events.fire_event("server-started"); + require "util.timer".add_task(0, function () + prosody.log("debug", "Firing server-started event"); + prosody.events.fire_event("server-started"); + end); end -- Override logging config (used by prosodyctl) -- cgit v1.2.3