diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2022-05-28 23:03:45 +0200 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2022-05-28 23:23:36 +0200 |
| commit | b04ae71defe0bd2a5fbd2df07d55cfe3eb40cba9 (patch) | |
| tree | b7a072bd3553a4fed1bbae7d91911ca04f2b31d5 /gnu/services | |
| parent | 0542905a2c5cb4f645399e19c2a4924dc757057e (diff) | |
services: herd: Add 'wait-for-service'.
* gnu/services/herd.scm (wait-for-service): New procedure.
Diffstat (limited to 'gnu/services')
| -rw-r--r-- | gnu/services/herd.scm | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/gnu/services/herd.scm b/gnu/services/herd.scm index 80d08f849e1..a7c845b4b04 100644 --- a/gnu/services/herd.scm +++ b/gnu/services/herd.scm | |||
| @@ -58,7 +58,8 @@ | |||
| 58 | load-services/safe | 58 | load-services/safe |
| 59 | start-service | 59 | start-service |
| 60 | stop-service | 60 | stop-service |
| 61 | restart-service)) | 61 | restart-service |
| 62 | wait-for-service)) | ||
| 62 | 63 | ||
| 63 | ;;; Commentary: | 64 | ;;; Commentary: |
| 64 | ;;; | 65 | ;;; |
| @@ -313,6 +314,39 @@ when passed a service with an already-registered name." | |||
| 313 | (with-shepherd-action name ('restart) result | 314 | (with-shepherd-action name ('restart) result |
| 314 | result)) | 315 | result)) |
| 315 | 316 | ||
| 317 | (define* (wait-for-service name #:key (timeout 20)) | ||
| 318 | "Wait for the service providing NAME, a symbol, to be up and running, and | ||
| 319 | return its \"running value\". Give up after TIMEOUT seconds and raise a | ||
| 320 | '&shepherd-error' exception. Raise a '&service-not-found-error' exception | ||
| 321 | when NAME is not found." | ||
| 322 | (define (relevant-service? service) | ||
| 323 | (memq name (live-service-provision service))) | ||
| 324 | |||
| 325 | (define start | ||
| 326 | (car (gettimeofday))) | ||
| 327 | |||
| 328 | ;; Note: As of Shepherd 0.9.1, we cannot just call the 'start' method and | ||
| 329 | ;; wait for it: it would spawn an additional elogind process. Thus, poll. | ||
| 330 | (let loop ((attempts 0)) | ||
| 331 | (define services | ||
| 332 | (current-services)) | ||
| 333 | |||
| 334 | (define now | ||
| 335 | (car (gettimeofday))) | ||
| 336 | |||
| 337 | (when (>= (- now start) timeout) | ||
| 338 | (raise (condition (&shepherd-error)))) ;XXX: better exception? | ||
| 339 | |||
| 340 | (match (find relevant-service? services) | ||
| 341 | (#f | ||
| 342 | (raise (condition (&service-not-found-error | ||
| 343 | (service name))))) | ||
| 344 | (service | ||
| 345 | (or (live-service-running service) | ||
| 346 | (begin | ||
| 347 | (sleep 1) | ||
| 348 | (loop (+ attempts 1)))))))) | ||
| 349 | |||
| 316 | ;; Local Variables: | 350 | ;; Local Variables: |
| 317 | ;; eval: (put 'alist-let* 'scheme-indent-function 2) | 351 | ;; eval: (put 'alist-let* 'scheme-indent-function 2) |
| 318 | ;; eval: (put 'with-shepherd 'scheme-indent-function 1) | 352 | ;; eval: (put 'with-shepherd 'scheme-indent-function 1) |
