Last active
December 24, 2022 17:28
-
-
Save maxlapshin/01773f0fca706acdcb4acb77d91d78bb to your computer and use it in GitHub Desktop.
Systemd support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(systemd). | |
% This is what you need to adopt systemd in erlang | |
% | |
% Do whatever you want license. If you want, you can take this code under terms of MIT license. | |
-export([ready/0, reloading/0, stopping/0, watchdog/0]). | |
-export([start_link/0]). | |
-export([init/1, handle_info/2, terminate/2]). | |
ready() -> call(<<"READY=1">>). | |
reloading() ->call(<<"RELOADING=1">>). | |
stopping() -> call(<<"STOPPING=1">>). | |
watchdog() -> call(<<"WATCHDOG=1">>). | |
call(Call) -> | |
case os:getenv("NOTIFY_SOCKET") of | |
false -> | |
{error, not_configured}; | |
Path -> | |
case gen_udp:open(0, [local]) of | |
{error, SocketError} -> | |
{error, SocketError}; | |
{ok, Socket} -> | |
Result = gen_udp:send(Socket, {local,Path}, 0, Call), | |
gen_udp:close(Socket), | |
Result | |
end | |
end. | |
start_link() -> | |
gen_server:start_link({local,?MODULE}, ?MODULE, [], []). | |
init([]) -> | |
erlang:send_after(60000, self(), watchdog), | |
{ok, state}. | |
handle_info(watchdog, State) -> | |
watchdog(), | |
erlang:send_after(60000, self(), watchdog), | |
{noreply, State}. | |
terminate(_,_) -> ok. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FWIW, I created something similar, except my version keeps the socket in the
gen_server
state rather than reopening it on each notification (and uses the normalgen_server
timeout for triggering watchdog notifications).Maybe we should publish this as a proper library application …