summary refs log tree commit diff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2022-05-28 23:03:45 +0200
committerLudovic Courtès <ludo@gnu.org>2022-05-28 23:23:36 +0200
commitb04ae71defe0bd2a5fbd2df07d55cfe3eb40cba9 (patch)
treeb7a072bd3553a4fed1bbae7d91911ca04f2b31d5
parent0542905a2c5cb4f645399e19c2a4924dc757057e (diff)
downloadguix-b04ae71defe0bd2a5fbd2df07d55cfe3eb40cba9.tar.gz
services: herd: Add 'wait-for-service'.
* gnu/services/herd.scm (wait-for-service): New procedure.
-rw-r--r--gnu/services/herd.scm36
1 files changed, 35 insertions, 1 deletions
diff --git a/gnu/services/herd.scm b/gnu/services/herd.scm
index 80d08f849e..a7c845b4b0 100644
--- a/gnu/services/herd.scm
+++ b/gnu/services/herd.scm
@@ -58,7 +58,8 @@
             load-services/safe
             start-service
             stop-service
-            restart-service))
+            restart-service
+            wait-for-service))
 
 ;;; Commentary:
 ;;;
@@ -313,6 +314,39 @@ when passed a service with an already-registered name."
   (with-shepherd-action name ('restart) result
     result))
 
+(define* (wait-for-service name #:key (timeout 20))
+  "Wait for the service providing NAME, a symbol, to be up and running, and
+return its \"running value\".  Give up after TIMEOUT seconds and raise a
+'&shepherd-error' exception.  Raise a '&service-not-found-error' exception
+when NAME is not found."
+  (define (relevant-service? service)
+    (memq name (live-service-provision service)))
+
+  (define start
+    (car (gettimeofday)))
+
+  ;; Note: As of Shepherd 0.9.1, we cannot just call the 'start' method and
+  ;; wait for it: it would spawn an additional elogind process.  Thus, poll.
+  (let loop ((attempts 0))
+    (define services
+      (current-services))
+
+    (define now
+      (car (gettimeofday)))
+
+    (when (>= (- now start) timeout)
+      (raise (condition (&shepherd-error))))      ;XXX: better exception?
+
+    (match (find relevant-service? services)
+      (#f
+       (raise (condition (&service-not-found-error
+                          (service name)))))
+      (service
+       (or (live-service-running service)
+           (begin
+             (sleep 1)
+             (loop (+ attempts 1))))))))
+
 ;; Local Variables:
 ;; eval: (put 'alist-let* 'scheme-indent-function 2)
 ;; eval: (put 'with-shepherd 'scheme-indent-function 1)