guix-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[shepherd] 06/08: service: Remove redundant condition in 'start'.


From: Ludovic Courtès
Subject: [shepherd] 06/08: service: Remove redundant condition in 'start'.
Date: Sat, 25 Mar 2023 17:53:07 -0400 (EDT)

civodul pushed a commit to branch master
in repository shepherd.

commit df8ac0f67fcbd1bbd7baa57bd55cbf144ce7a2e4
Author: Ludovic Courtès <ludo@gnu.org>
AuthorDate: Sat Mar 25 22:37:33 2023 +0100

    service: Remove redundant condition in 'start'.
    
    * modules/shepherd/service.scm (start): Remove redundant and racy
    'service-status' check.  Move "already running" message to after
    'start' message reply.
---
 modules/shepherd/service.scm | 100 +++++++++++++++++++++----------------------
 1 file changed, 49 insertions(+), 51 deletions(-)

diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
index 4daa9be..1f0a0d3 100644
--- a/modules/shepherd/service.scm
+++ b/modules/shepherd/service.scm
@@ -685,57 +685,55 @@ while starting ~a: ~s")
 
 ;; Start the service, including dependencies.
 (define-method (start (obj <service>) . args)
-  (cond ((eq? 'running (service-status obj))
-        (local-output (l10n "Service ~a is already running.")
-                      (canonical-name obj))
-         (service-running-value obj))
-       ((not (service-enabled? obj))
-        (local-output (l10n "Service ~a is currently disabled.")
-                      (canonical-name obj))
-         (service-running-value obj))
-       (else
-        ;; It is not running; go ahead and launch it.
-        (let ((problems
-               ;; Resolve all dependencies.
-               (start-in-parallel (required-by obj))))
-           (define running
-            (if (pair? problems)
-                 (for-each (lambda (problem)
-                            (local-output (l10n "Service ~a depends on ~a.")
-                                          (canonical-name obj)
-                                          problem))
-                           problems)
-                 ;; Start the service itself.
-                 (let ((reply (make-channel)))
-                   (put-message (service-control obj) `(start ,reply))
-                   (match (get-message reply)
-                     (#f
-                      ;; We lost the race: OBJ is already running.
-                      (service-running-value obj))
-                     ((? channel? notification)
-                      ;; We won the race: we're responsible for starting OBJ
-                      ;; and sending its running value on NOTIFICATION.
-                      (let ((running
-                             (catch #t
-                               (lambda ()
-                                 ;; Make sure the 'start' method writes
-                                 ;; messages to the right port.
-                                 (parameterize ((current-output-port
-                                                 
(%current-service-output-port))
-                                                (current-error-port
-                                                 
(%current-service-output-port)))
-                                   (apply (slot-ref obj 'start) args)))
-                               (lambda (key . args)
-                                 (put-message notification #f)
-                                 (report-exception 'start obj key args)))))
-                        (put-message notification running)
-                        (local-output (if running
-                                         (l10n "Service ~a has been started.")
-                                          (l10n "Service ~a could not be 
started."))
-                                     (canonical-name obj))
-                        running))))))
-
-           running))))
+  (if (service-enabled? obj)
+      ;; It is not running; go ahead and launch it.
+      (let ((problems
+            ;; Resolve all dependencies.
+            (start-in-parallel (required-by obj))))
+        (define running
+         (if (pair? problems)
+              (for-each (lambda (problem)
+                         (local-output (l10n "Service ~a depends on ~a.")
+                                       (canonical-name obj)
+                                       problem))
+                        problems)
+              ;; Start the service itself.
+              (let ((reply (make-channel)))
+                (put-message (service-control obj) `(start ,reply))
+                (match (get-message reply)
+                  (#f
+                   ;; We lost the race: OBJ is already running.
+                  (local-output (l10n "Service ~a is already running.")
+                                (canonical-name obj))
+                   (service-running-value obj))
+                  ((? channel? notification)
+                   ;; We won the race: we're responsible for starting OBJ
+                   ;; and sending its running value on NOTIFICATION.
+                   (let ((running
+                          (catch #t
+                            (lambda ()
+                              ;; Make sure the 'start' method writes
+                              ;; messages to the right port.
+                              (parameterize ((current-output-port
+                                              (%current-service-output-port))
+                                             (current-error-port
+                                              (%current-service-output-port)))
+                                (apply (slot-ref obj 'start) args)))
+                            (lambda (key . args)
+                              (put-message notification #f)
+                              (report-exception 'start obj key args)))))
+                     (put-message notification running)
+                     (local-output (if running
+                                      (l10n "Service ~a has been started.")
+                                       (l10n "Service ~a could not be 
started."))
+                                  (canonical-name obj))
+                     running))))))
+
+        running)
+      (begin
+        (local-output (l10n "Service ~a is currently disabled.")
+                     (canonical-name obj))
+        (service-running-value obj))))
 
 (define (replace-service old-service new-service)
   "Replace OLD-SERVICE with NEW-SERVICE in the services registry.  This



reply via email to

[Prev in Thread] Current Thread [Next in Thread]