Software /
code /
prosody
Comparison
util/startup.lua @ 13471:afad3b2725bf
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
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 04 Apr 2024 19:00:27 +0200 |
parent | 13467:c2a476f4712a |
child | 13477:e8ac3ce3238e |
comparison
equal
deleted
inserted
replaced
13469:f9171624fd03 | 13471:afad3b2725bf |
---|---|
807 log("info", "Received SIGUSR2"); | 807 log("info", "Received SIGUSR2"); |
808 prosody.events.fire_event("signal/SIGUSR2"); | 808 prosody.events.fire_event("signal/SIGUSR2"); |
809 end); | 809 end); |
810 end | 810 end |
811 | 811 |
812 function startup.systemd_notify() | |
813 local notify_socket_name = os.getenv("NOTIFY_SOCKET"); | |
814 if not notify_socket_name then return end | |
815 local have_unix, unix = pcall(require, "socket.unix"); | |
816 if not have_unix or type(unix) ~= "table" then | |
817 log("error", "LuaSocket without UNIX socket support, can't notify systemd.") | |
818 return os.exit(1); | |
819 end | |
820 log("debug", "Will notify on socket %q", notify_socket_name); | |
821 notify_socket_name = notify_socket_name:gsub("^@", "\0"); | |
822 local notify_socket = unix.dgram(); | |
823 local ok, err = notify_socket:setpeername(notify_socket_name); | |
824 if not ok then | |
825 log("error", "Could not connect to systemd notification socket %q: %q", notify_socket_name, err); | |
826 return os.exit(1); | |
827 end | |
828 local time = require "prosody.util.time"; | |
829 | |
830 prosody.notify_socket = notify_socket; | |
831 prosody.events.add_handler("server-started", function() | |
832 notify_socket:send("READY=1"); | |
833 end); | |
834 prosody.events.add_handler("config-reloading", function() | |
835 notify_socket:send(string.format("RELOADING=1\nMONOTONIC_USEC=%d", math.floor(time.monotonic() * 1000000))); | |
836 end); | |
837 prosody.events.add_handler("config-reloaded", function() | |
838 notify_socket:send("READY=1"); | |
839 end); | |
840 prosody.events.add_handler("server-stopping", function() | |
841 notify_socket:send("STOPPING=1"); | |
842 end); | |
843 end | |
844 | |
812 function startup.cleanup() | 845 function startup.cleanup() |
813 prosody.log("info", "Shutdown status: Cleaning up"); | 846 prosody.log("info", "Shutdown status: Cleaning up"); |
814 prosody.events.fire_event("server-cleanup"); | 847 prosody.events.fire_event("server-cleanup"); |
815 end | 848 end |
816 | 849 |
888 startup.init_data_store(); | 921 startup.init_data_store(); |
889 startup.init_global_protection(); | 922 startup.init_global_protection(); |
890 startup.posix_daemonize(); | 923 startup.posix_daemonize(); |
891 startup.write_pidfile(); | 924 startup.write_pidfile(); |
892 startup.hook_posix_signals(); | 925 startup.hook_posix_signals(); |
926 startup.systemd_notify(); | |
893 startup.prepare_to_start(); | 927 startup.prepare_to_start(); |
894 startup.notify_started(); | 928 startup.notify_started(); |
895 end | 929 end |
896 | 930 |
897 return startup; | 931 return startup; |