|
111 | 111 | serviceConfig.TimeoutStartSec = lib.mkForce "infinity"; |
112 | 112 |
|
113 | 113 | # We add a ExecStartPost with a script that runs the readiness probe |
114 | | - serviceConfig.ExecStartPost = |
| 114 | + serviceConfig.ExecStartPre = |
115 | 115 | let |
116 | 116 | scriptPath = lib.makeBinPath ( |
117 | 117 | [ |
|
126 | 126 | pkgs.writeShellScript "${mainServiceName}-readiness-check" '' |
127 | 127 | #!${pkgs.runtimeShell} |
128 | 128 | set -o nounset |
129 | | - export PATH="${scriptPath}:$PATH" |
130 | | -
|
131 | | - echo "Health check: starting background readiness probe for ${mainServiceName}." |
132 | | - sleep ${toString probeCfg.initialDelay} |
133 | | - retryCount=${toString probeCfg.retryCount} |
134 | | - while true; do |
135 | | - if (timeout ${toString probeCfg.timeout}s ${probeCfg.command} &> /dev/null); then |
136 | | - echo "Health check: probe successful. Notifying systemd that the service is ready." |
137 | | - systemd-notify --ready --status="${probeCfg.statusReadyMessage}" |
138 | | - exit 0 |
139 | | - else |
140 | | - echo "Health check: probe not successful. Notifying systemd that the service is still waiting. Retrying in ${toString probeCfg.interval} seconds..." |
141 | | - systemd-notify --status="${probeCfg.statusWaitingMessage}" |
142 | | - if [[ ''${retryCount} -ne -1 ]]; then |
143 | | - retryCount=$((retryCount - 1)) |
144 | | - if [[ ''${retryCount} -le 0 ]]; then |
145 | | - echo "Health check: probe failed after maximum retries. Exiting." |
146 | | - exit 1 |
| 129 | +
|
| 130 | + export NOTIFY_SOCKET |
| 131 | + monitor() { |
| 132 | + export PATH="${scriptPath}:$PATH" |
| 133 | +
|
| 134 | + echo "Health check: starting background readiness probe for ${mainServiceName}." 1>>/tmp/banica1 2>>/tmp/banica2 |
| 135 | + sleep ${toString probeCfg.initialDelay} |
| 136 | + retryCount=${toString probeCfg.retryCount} |
| 137 | + while true; do |
| 138 | + if (timeout ${toString probeCfg.timeout}s ${probeCfg.command} &> /dev/null); then |
| 139 | + echo "Health check: probe successful. Notifying systemd that the service is ready." 1>>/tmp/banica1 2>>/tmp/banica2 |
| 140 | + systemd-notify --ready --status="${probeCfg.statusReadyMessage}" 1>>/tmp/banica1 2>>/tmp/banica2 |
| 141 | + exit 0 |
| 142 | + else |
| 143 | + echo "Health check: probe not successful. Notifying systemd that the service is still waiting. Retrying in ${toString probeCfg.interval} seconds..." 1>>/tmp/banica1 2>>/tmp/banica2 |
| 144 | + systemd-notify --status="${probeCfg.statusWaitingMessage}" 1>>/tmp/banica1 2>>/tmp/banica2 |
| 145 | + if [[ ''${retryCount} -ne -1 ]]; then |
| 146 | + retryCount=$((retryCount - 1)) |
| 147 | + if [[ ''${retryCount} -le 0 ]]; then |
| 148 | + echo "Health check: probe failed after maximum retries. Exiting." 1>>/tmp/banica1 2>>/tmp/banica2 |
| 149 | + exit 1 |
| 150 | + fi |
147 | 151 | fi |
148 | 152 | fi |
149 | | - fi |
150 | | - sleep ${toString probeCfg.interval} |
151 | | - done |
| 153 | + sleep ${toString probeCfg.interval} |
| 154 | + done |
| 155 | + } |
| 156 | +
|
| 157 | + monitor & |
152 | 158 | ''; |
153 | 159 | } |
154 | 160 | )) |
|
0 commit comments