Diff

util/startup.lua @ 13014:06453c564141

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.
author Matthew Wild <mwild1@gmail.com>
date Sat, 01 Apr 2023 11:56:38 +0100
parent 12982:fc0109c59807
child 13119:df57c62165fc
line wrap: on
line diff
--- 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)