[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[shepherd] 13/13: herd: Report startup failure.
From: |
Ludovic Courtès |
Subject: |
[shepherd] 13/13: herd: Report startup failure. |
Date: |
Sun, 16 Apr 2023 17:38:37 -0400 (EDT) |
civodul pushed a commit to branch master
in repository shepherd.
commit fbca4e20a9e14a3024a02ee9d3bf6292e3fb99c2
Author: Ludovic Courtès <ludo@gnu.org>
AuthorDate: Sun Apr 16 22:48:45 2023 +0200
herd: Report startup failure.
* modules/shepherd/scripts/herd.scm (display-service-status): Check
'startup-failures'. When the service is stopped and STARTUP-FAILURES is
a pair, mark it as failing and report the startup failure time.
* tests/startup-failure.sh: New file.
* Makefile.am (TESTS): Add it.
---
Makefile.am | 1 +
modules/shepherd/scripts/herd.scm | 16 ++++++++--
tests/startup-failure.sh | 61 +++++++++++++++++++++++++++++++++++++++
3 files changed, 75 insertions(+), 3 deletions(-)
diff --git a/Makefile.am b/Makefile.am
index 6eba8d9..c6f0c70 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -242,6 +242,7 @@ TESTS = \
tests/basic.sh \
tests/starting-status.sh \
tests/stopping-status.sh \
+ tests/startup-failure.sh \
tests/replacement.sh \
tests/respawn.sh \
tests/respawn-throttling.sh \
diff --git a/modules/shepherd/scripts/herd.scm
b/modules/shepherd/scripts/herd.scm
index cc82f61..998b5d7 100644
--- a/modules/shepherd/scripts/herd.scm
+++ b/modules/shepherd/scripts/herd.scm
@@ -91,7 +91,8 @@ of pairs."
(match service
(('service ('version 0 _ ...) properties ...)
(alist-let* properties (provides requires status running respawn? enabled?
- last-respawns one-shot? transient?)
+ last-respawns startup-failures
+ one-shot? transient?)
(format #t (l10n "Status of ~a:~%") (first provides))
;; Note: Shepherd up to 0.9.x included did not provide 'status', hence
@@ -109,7 +110,9 @@ of pairs."
('stopped
(if one-shot?
(format #t (l10n " It is stopped (one-shot).~%"))
- (format #t (l10n " It is stopped.~%"))))
+ (if (pair? startup-failures)
+ (format #t (l10n " It is stopped (failing).~%"))
+ (format #t (l10n " It is stopped.~%")))))
('starting
(format #t (l10n " It is starting.~%")))
('stopping
@@ -130,7 +133,14 @@ of pairs."
(format #t (l10n " Last respawned on ~a.~%")
(date->string
(time-utc->date (make-time time-utc 0 time)))))
- (_ #t))))))
+ (_ #t))
+ (when (or (eq? status 'stopped) (not running))
+ (match startup-failures
+ ((time _ ...)
+ (format #t (l10n " Failed to start at ~a.~%")
+ (date->string
+ (time-utc->date (make-time time-utc 0 time)))))
+ (_ #t)))))))
(define root-service?
;; XXX: This procedure is written in a surprising way to work around a
diff --git a/tests/startup-failure.sh b/tests/startup-failure.sh
new file mode 100644
index 0000000..9351ca8
--- /dev/null
+++ b/tests/startup-failure.sh
@@ -0,0 +1,61 @@
+# GNU Shepherd --- Check service startup failure reporting
+# Copyright © 2023 Ludovic Courtès <ludo@gnu.org>
+#
+# This file is part of the GNU Shepherd.
+#
+# The GNU Shepherd is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or (at
+# your option) any later version.
+#
+# The GNU Shepherd is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with the GNU Shepherd. If not, see <http://www.gnu.org/licenses/>.
+
+shepherd --version
+herd --version
+
+socket="t-socket-$$"
+conf="t-conf-$$"
+log="t-log-$$"
+pid="t-pid-$$"
+stamp="t-stamp-$$"
+
+herd="herd -s $socket"
+
+trap "rm -f $socket $conf $log $stamp;
+ test -f $pid && kill \`cat $pid\` || true; rm -f $pid" EXIT
+
+cat > "$conf" <<EOF
+(register-services
+ (service
+ '(may-fail)
+ #:start (lambda _
+ (file-exists? "$PWD/$stamp"))
+ #:respawn? #f))
+EOF
+
+rm -f "$pid" "$stamp" "$socket"
+shepherd -I -s "$socket" -c "$conf" --pid="$pid" --log="$log" &
+
+while ! test -f "$pid"; do sleep 0.5 ; done
+
+# When the service fails to start, 'herd status' should display that.
+if $herd start may-fail; then false; else true; fi
+$herd status may-fail | grep stopped
+$herd status may-fail | grep "Failed to start"
+
+touch "$stamp"
+$herd start may-fail
+$herd status may-fail | grep started
+$herd status may-fail | grep -v "Failed to start"
+
+# Once the service has been stopped gracefully, the "Failed to start" message
+# should not appear any longer.
+$herd stop may-fail
+$herd status may-fail | grep stopped
+$herd status may-fail | grep -v "Failed to start"
- [shepherd] branch master updated (353a91b -> fbca4e2), Ludovic Courtès, 2023/04/16
- [shepherd] 08/13: shepherd: Factorize command message gathering., Ludovic Courtès, 2023/04/16
- [shepherd] 02/13: monitoring: Log registered service names., Ludovic Courtès, 2023/04/16
- [shepherd] 03/13: service: Turn 'doc' method into a procedure., Ludovic Courtès, 2023/04/16
- [shepherd] 04/13: service: Turn 'action' method into a procedure., Ludovic Courtès, 2023/04/16
- [shepherd] 11/13: service: Record time of last startup failures., Ludovic Courtès, 2023/04/16
- [shepherd] 09/13: comm: Capture the client command protocol version., Ludovic Courtès, 2023/04/16
- [shepherd] 07/13: service: 'stop-service' returns the list of stopped services, not names., Ludovic Courtès, 2023/04/16
- [shepherd] 12/13: tests: Remove reference to non-existent file., Ludovic Courtès, 2023/04/16
- [shepherd] 01/13: service: Use 'lookup-service' instead of 'lookup-services'., Ludovic Courtès, 2023/04/16
- [shepherd] 13/13: herd: Report startup failure.,
Ludovic Courtès <=
- [shepherd] 06/13: service: Turn 'stop' method into a procedure., Ludovic Courtès, 2023/04/16
- [shepherd] 05/13: service: Turn 'start' method into a procedure., Ludovic Courtès, 2023/04/16
- [shepherd] 10/13: Add missing 'l10n' calls., Ludovic Courtès, 2023/04/16