# HG changeset patch # User Matthew Wild # Date 1680346598 -3600 # Node ID 06453c56414182dd1a60f01d5fee19d5dc89ae38 # Parent 430333198e4cb0b87c2b669cda988f71204a4df2 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. diff -r 430333198e4c -r 06453c564141 util/startup.lua --- a/util/startup.lua Fri Mar 31 22:01:27 2023 +0200 +++ b/util/startup.lua Sat Apr 01 11:56:38 2023 +0100 @@ -430,8 +430,16 @@ 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 @@ 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)