aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2024-04-04 19:00:27 +0200
committerKim Alvefur <zash@zash.se>2024-04-04 19:00:27 +0200
commit502b8d399839e23cafdba350ceab962950eaee24 (patch)
tree11b9257bb3d80bb64c992a515656629fe10b3db2
parentMUC: Fix legacy hats (thanks nicoco) (diff)
downloadprosody-502b8d399839e23cafdba350ceab962950eaee24.tar.xz
prosody-502b8d399839e23cafdba350ceab962950eaee24.zip
util.startup: Support systemd Type=notify service type
This lets Prosody report its lifecycle status to systemd, so it knows when Prosody has completed its startup, when it's reloading and shutting down. Both Type=notify and Type=notify-reload is supported Example systemd .service configuration snippet: [Service] Type=notify
-rw-r--r--CHANGES1
-rw-r--r--util/startup.lua34
2 files changed, 35 insertions, 0 deletions
diff --git a/CHANGES b/CHANGES
index 4d85d3312..95b636ded 100644
--- a/CHANGES
+++ b/CHANGES
@@ -66,6 +66,7 @@ TRUNK
- Intervals of mod_cron managed periodic jobs made configurable
- When mod_smacks is enabled, s2s connections not responding to ack requests are closed.
- Arguments to `prosodyctl shell` that start with ':' are now turned into method calls
+- Support for Type=notify and notify-reload systemd service type added
## Removed
diff --git a/util/startup.lua b/util/startup.lua
index 60fd63390..a627f11c6 100644
--- a/util/startup.lua
+++ b/util/startup.lua
@@ -809,6 +809,39 @@ function startup.hook_posix_signals()
end);
end
+function startup.systemd_notify()
+ local notify_socket_name = os.getenv("NOTIFY_SOCKET");
+ if not notify_socket_name then return end
+ local have_unix, unix = pcall(require, "socket.unix");
+ if not have_unix or type(unix) ~= "table" then
+ log("error", "LuaSocket without UNIX socket support, can't notify systemd.")
+ return os.exit(1);
+ end
+ log("debug", "Will notify on socket %q", notify_socket_name);
+ notify_socket_name = notify_socket_name:gsub("^@", "\0");
+ local notify_socket = unix.dgram();
+ local ok, err = notify_socket:setpeername(notify_socket_name);
+ if not ok then
+ log("error", "Could not connect to systemd notification socket %q: %q", notify_socket_name, err);
+ return os.exit(1);
+ end
+ local time = require "prosody.util.time";
+
+ prosody.notify_socket = notify_socket;
+ prosody.events.add_handler("server-started", function()
+ notify_socket:send("READY=1");
+ end);
+ prosody.events.add_handler("config-reloading", function()
+ notify_socket:send(string.format("RELOADING=1\nMONOTONIC_USEC=%d", math.floor(time.monotonic() * 1000000)));
+ end);
+ prosody.events.add_handler("config-reloaded", function()
+ notify_socket:send("READY=1");
+ end);
+ prosody.events.add_handler("server-stopping", function()
+ notify_socket:send("STOPPING=1");
+ end);
+end
+
function startup.cleanup()
prosody.log("info", "Shutdown status: Cleaning up");
prosody.events.fire_event("server-cleanup");
@@ -890,6 +923,7 @@ function startup.prosody()
startup.posix_daemonize();
startup.write_pidfile();
startup.hook_posix_signals();
+ startup.systemd_notify();
startup.prepare_to_start();
startup.notify_started();
end