Skip to content

Commit

Permalink
nixos/influxdb2: wait until service is ready
Browse files Browse the repository at this point in the history
Factor out part of the provisioning script into a
wait-until-service-is-ready script, and put it unconditionally in
front of ExecStartPost=, so that services that depend on influxdb2 are
not started until influxdb2 responds to requests.

Fixes #317017 ("Scrutiny tries to start before influxdb has started")

(cherry picked from commit 732d365)
  • Loading branch information
bjornfor authored and github-actions[bot] committed Sep 26, 2024
1 parent 37df9bc commit ad23b68
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions nixos/modules/services/databases/influxdb2.nix
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ let
inherit (cfg.provision) organizations users;
});

provisioningScript = pkgs.writeShellScript "post-start-provision" ''
set -euo pipefail
export INFLUX_HOST="http://"${escapeShellArg (
influxHost = "http://${escapeShellArg (
if ! hasAttr "http-bind-address" cfg.settings
|| hasInfix "0.0.0.0" cfg.settings.http-bind-address
then "localhost:8086"
else cfg.settings.http-bind-address
)}
)}";

# Wait for the influxdb server to come online
waitUntilServiceIsReady = pkgs.writeShellScript "wait-until-service-is-ready" ''
set -euo pipefail
export INFLUX_HOST=${influxHost}
count=0
while ! influx ping &>/dev/null; do
if [ "$count" -eq 300 ]; then
Expand All @@ -92,6 +92,11 @@ let
sleep 0.1
count=$((count++))
done
'';

provisioningScript = pkgs.writeShellScript "post-start-provision" ''
set -euo pipefail
export INFLUX_HOST=${influxHost}
# Do the initial database setup. Pass /dev/null as configs-path to
# avoid saving the token as the active config.
Expand Down Expand Up @@ -447,11 +452,13 @@ in
"admin-token:${cfg.provision.initialSetup.tokenFile}"
];

ExecStartPost = mkIf cfg.provision.enable (
ExecStartPost = [
waitUntilServiceIsReady
] ++ (lib.optionals cfg.provision.enable (
[provisioningScript] ++
# Only the restarter runs with elevated privileges
optional anyAuthDefined "+${restarterScript}"
);
));
};

path = [
Expand Down

0 comments on commit ad23b68

Please sign in to comment.