From 4cf87bac0e0c80e4adb2454b0cfbc3b3d7376a7a Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 5 Sep 2023 00:30:34 +0200 Subject: [PATCH] modules/consul-distributed-services: wait for consul to start --- modules/consul-distributed-services/default.nix | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/modules/consul-distributed-services/default.nix b/modules/consul-distributed-services/default.nix index 39c34e2..61f73e1 100644 --- a/modules/consul-distributed-services/default.nix +++ b/modules/consul-distributed-services/default.nix @@ -34,9 +34,16 @@ in ''${@} ''; + waitForConsul = pkgs.writeShellScript "wait-for-consul" '' + while ! ${consul}/bin/consul lock --name="pre-flight-check" --n=${toString cfg.replicas} --shell=false "$1" ${pkgs.coreutils}/bin/true; do + sleep 1 + done + ''; + hasSpecialPrefix = elem (substring 0 1 ExecStart) [ "@" "-" ":" "+" "!" ]; in assert !hasSpecialPrefix; pkgs.writeTextDir "etc/systemd/system/${n}.service.d/distributed.conf" '' [Service] + ExecStartPre=${waitForConsul} 'services/${n}%i' ExecStart= ExecStart=${consul}/bin/consul lock --name=${n} --n=${toString cfg.replicas} --shell=false --child-exit-code 'services/${n}%i' ${optionalString (cfg.registerService != null) runWithRegistration} ${ExecStart} Environment="CONSUL_HTTP_ADDR=${consulHttpAddr}"