modules/consul-service-registry: better auto-retry

This commit is contained in:
Max Headroom 2023-04-15 00:47:57 +02:00
parent d1b328996c
commit f1cb977a16

View file

@ -7,6 +7,20 @@ let
consul = "${config.services.consul.package}/bin/consul"; consul = "${config.services.consul.package}/bin/consul";
writeLoopScript = name: cmd: pkgs.writeShellScript name ''
while ! ${cmd}; do
sleep 1
done
'';
consulRegisterScript = writeLoopScript "consul-register" ''${consul} services register "$1"'';
consulDeregisterScript = writeLoopScript "consul-deregister" ''${consul} services deregister "$1"'';
register = servicesJson: "${consulRegisterScript} ${servicesJson}";
deregister = servicesJson: "${consulDeregisterScript} ${servicesJson}";
consulServiceDefinition = types.submodule ({ name, ... }: { consulServiceDefinition = types.submodule ({ name, ... }: {
options = { options = {
unit = mkOption { unit = mkOption {
@ -38,8 +52,8 @@ let
value = { value = {
direct = { direct = {
serviceConfig = { serviceConfig = {
ExecStartPost = "${consul} services register ${servicesJson}"; ExecStartPost = register servicesJson;
ExecStopPost = "${consul} services deregister ${servicesJson}"; ExecStopPost = deregister servicesJson;
}; };
}; };
external = { external = {
@ -49,10 +63,10 @@ let
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
ExecStart = "${consul} services register ${servicesJson}"; ExecStart = register servicesJson;
ExecStop = "${consul} services deregister ${servicesJson}"; ExecStop = deregister servicesJson;
Restart = "on-failure"; Restart = "on-failure";
RestartSec = "1s"; RestartSec = "30s";
}; };
}; };
}.${mode}; }.${mode};