* [bug#50717] [PATCH] Shepherd: Add respawn-limit paramter to service class.
@ 2021-09-21 7:42 Ryan Sundberg via Guix-patches via
2021-10-01 10:11 ` Mathieu Othacehe
2024-01-02 22:31 ` bug#50717: " Ludovic Courtès
0 siblings, 2 replies; 3+ messages in thread
From: Ryan Sundberg via Guix-patches via @ 2021-09-21 7:42 UTC (permalink / raw)
To: 50717
[-- Attachment #1.1.1: Type: text/plain, Size: 441 bytes --]
Hello Guix devs,
This patch to GNU Shepherd removes the hard-coded respawn limit and
makes it a configurable option.
The respawn limit works generally the same way as before, allowing N
respawns in M seconds:
#:respawn-limit '(N . M)
We can disable respawns by setting N to 0, which will abort the service
after the first crash. Likewise, we can always respawn by setting
#:respawn-limit #f
Sincerely,
Ryan Sundberg
[-- Attachment #1.1.2: 0001-service-Add-respawn-limit-paramter-to-the-service-cl.patch --]
[-- Type: text/x-patch, Size: 5942 bytes --]
From 94ad8057c6f9a020f12efd78d482b0cf4fe160ec Mon Sep 17 00:00:00 2001
From: Ryan Sundberg <ryan@arctype.co>
Date: Sun, 11 Jul 2021 13:54:04 -0700
Subject: [PATCH] service: Add respawn-limit paramter to the service class.
Makes respawn-limit a configurable parameter on service. In addition, we
allow the limit to be set to #f to indicate no respawn limit.
* modules/shepherd/service.scm: Add respawn-limit
---
modules/shepherd/service.scm | 86 +++++++++++++++++++-----------------
1 file changed, 46 insertions(+), 40 deletions(-)
diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
index ad8608b..d741e0b 100644
--- a/modules/shepherd/service.scm
+++ b/modules/shepherd/service.scm
@@ -134,30 +134,6 @@
((_)
'())))
-;; Respawning CAR times in CDR seconds will disable the service.
-;;
-;; XXX: The terrible hack in (shepherd) using SIGALRM to work around
-;; unreliable SIGCHLD delivery means that it might take up to 1 second for
-;; SIGCHLD to be delivered. Thus, arrange for the car to be lower than the
-;; cdr.
-(define respawn-limit '(5 . 7))
-
-(define (respawn-limit-hit? respawns times seconds)
- "Return true of RESPAWNS, the list of times at which a given service was
-respawned, shows that it has been respawned more than TIMES in SECONDS."
- (define now (current-time))
-
- ;; Note: This is O(TIMES), but TIMES is typically small.
- (let loop ((times times)
- (respawns respawns))
- (match respawns
- (()
- #f)
- ((last-respawn rest ...)
- (or (zero? times)
- (and (> (+ last-respawn seconds) now)
- (loop (- times 1) rest)))))))
-
(define-class <service> ()
;; List of provided service-symbols. The first one is also called
;; the `canonical name' and must be unique to this service.
@@ -221,7 +197,16 @@ respawned, shows that it has been respawned more than TIMES in SECONDS."
(last-respawns #:init-form '())
;; A replacement for when this service is stopped.
(replacement #:init-keyword #:replacement
- #:init-value #f))
+ #:init-value #f)
+ ;; Respawning CAR times in CDR seconds will disable the service.
+ ;;
+ ;; Respawn limit (times, seconds). Set to #f to disable respawn limits.
+ ;; XXX: The terrible hack in (shepherd) using SIGALRM to work around
+ ;; unreliable SIGCHLD delivery means that it might take up to 1 second
for
+ ;; SIGCHLD to be delivered. Thus, arrange for the car to be lower than the
+ ;; cdr.
+ (respawn-limit #:init-keyword #:respawn-limit
+ #:init-value '(5 . 7)))
(define (service? obj)
"Return true if OBJ is a service."
@@ -587,8 +572,6 @@ clients."
(define-method (depends-resolved? (obj <service>))
(every lookup-running (required-by obj)))
-\f
-
(define (launch-service name proc args)
"Try to start (with PROC) a service providing NAME; return #f on failure.
Used by `start' and `enforce'."
@@ -648,8 +631,24 @@ results."
(apply action service the-action args))
which-services))))
-
-\f
+(define-method (respawn-limit-hit? (serv <service>))
+ "Return true if service SERV shows that it has been respawned more than it's
+respawn-limit TIMES in SECONDS. If the respawn-limit is #f, apply no limit."
+ (match (slot-ref serv 'respawn-limit)
+ (#f #f)
+ ((times . seconds)
+ (let* ((now (current-time))
+ (respawns (slot-ref serv 'last-respawns)))
+ ;; Note: This is O(TIMES), but TIMES is typically small.
+ (let loop ((times times)
+ (respawns respawns))
+ (match respawns
+ (()
+ #f)
+ ((last-respawn rest ...)
+ (or (zero? times)
+ (and (> (+ last-respawn seconds) now)
+ (loop (- times 1) rest))))))))))
;; Handling of unprovided service-symbols. This can be called in
;; either of the following ways (i.e. with either three or four
@@ -1140,18 +1139,25 @@ attempted to respawn the service a number of times already and it keeps dying,
then disable it."
(slot-set! serv 'running #f)
(if (and (respawn? serv)
- (not (respawn-limit-hit? (slot-ref serv 'last-respawns)
- (car respawn-limit)
- (cdr respawn-limit))))
+ (not (respawn-limit-hit? serv)))
(if (not (slot-ref serv 'waiting-for-termination?))
- (begin
- ;; Everything is okay, start it.
- (local-output (l10n "Respawning ~a.")
- (canonical-name serv))
- (slot-set! serv 'last-respawns
- (cons (current-time)
- (slot-ref serv 'last-respawns)))
- (start serv))
+ (match (slot-ref serv 'respawn-limit)
+ (#f
+ (begin
+ (local-output (l10n "Respawning ~a.")
+ (canonical-name serv))
+ (start serv)))
+ ((respawn-limit-times . _)
+ (let ((last-respawns (slot-ref serv 'last-respawns)))
+ ;; Everything is okay, start it.
+ (local-output (l10n "Respawning ~a.")
+ (canonical-name serv))
+ (slot-set! serv 'last-respawns
+ (cons (current-time)
+ ;; Only take the last n times here to prevent unbounded
+ ;; list growth
+ (take last-respawns (min (length last-respawns) respawn-limit-times))))
+ (start serv))))
;; We have just been waiting for the
;; termination. The `running' slot has already
;; been set to `#f' by `stop'.
--
2.31.1
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [bug#50717] [PATCH] Shepherd: Add respawn-limit paramter to service class.
2021-09-21 7:42 [bug#50717] [PATCH] Shepherd: Add respawn-limit paramter to service class Ryan Sundberg via Guix-patches via
@ 2021-10-01 10:11 ` Mathieu Othacehe
2024-01-02 22:31 ` bug#50717: " Ludovic Courtès
1 sibling, 0 replies; 3+ messages in thread
From: Mathieu Othacehe @ 2021-10-01 10:11 UTC (permalink / raw)
To: Ryan Sundberg; +Cc: 50717
Hello Ryan,
> We can disable respawns by setting N to 0, which will abort the service
> after the first crash. Likewise, we can always respawn by setting
>
> #:respawn-limit #f
Looks nice, thanks for the patch. I have the following error while
applying it though:
--8<---------------cut here---------------start------------->8---
Applying: service: Add respawn-limit paramter to the service class.
error: corrupt patch at line 52
--8<---------------cut here---------------end--------------->8---
Could you try to send it with git-send-email?
It would also be nice to add unit tests dedicated to this new feature and
update the documentation.
Thanks,
Mathieu
^ permalink raw reply [flat|nested] 3+ messages in thread
* bug#50717: [PATCH] Shepherd: Add respawn-limit paramter to service class.
2021-09-21 7:42 [bug#50717] [PATCH] Shepherd: Add respawn-limit paramter to service class Ryan Sundberg via Guix-patches via
2021-10-01 10:11 ` Mathieu Othacehe
@ 2024-01-02 22:31 ` Ludovic Courtès
1 sibling, 0 replies; 3+ messages in thread
From: Ludovic Courtès @ 2024-01-02 22:31 UTC (permalink / raw)
To: Ryan Sundberg; +Cc: 50717-done
Hi Ryan,
Ryan Sundberg <ryan@arctype.co> skribis:
> This patch to GNU Shepherd removes the hard-coded respawn limit and
> makes it a configurable option.
> The respawn limit works generally the same way as before, allowing N
> respawns in M seconds:
>
> #:respawn-limit '(N . M)
This is was eventually implemented (independently, oops!) in commit
93baa23baee1e0c62d46d9fd041fc1dabc081f26 (June 2023), which made it in
0.10.2.
Thanks,
Ludo’.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-01-02 22:33 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-21 7:42 [bug#50717] [PATCH] Shepherd: Add respawn-limit paramter to service class Ryan Sundberg via Guix-patches via
2021-10-01 10:11 ` Mathieu Othacehe
2024-01-02 22:31 ` bug#50717: " Ludovic Courtès
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/guix.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).