guix-devel
[Top][All Lists]
Advanced

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

How to invoke shepherd action from shepherd action?


From: Tomas Volf
Subject: How to invoke shepherd action from shepherd action?
Date: Fri, 5 Jul 2024 17:24:08 +0200

Hello,

I am currently in the process of writing a new service, and I have run into bit
of a wall.  I need to invoke shepherd action after my one-shot service finishes.

The code (relevant bits) for my service is pretty simple:

(define (acme-client-shepherd-services config)
  (let* ((config-file (serialize-acme-client-configuration config))
         (package (acme-client-configuration-package config))
         (reload-hook (acme-client-configuration-reload-hook config))
         (requirement (acme-client-configuration-requirement config))
         (handles (map acme-client-domain-handle
                       (acme-client-configuration-domains config))))
    (list
     (shepherd-service
      (provision '(acme-client-initial))
      (requirement requirement)
      (documentation "Invoke right away to provision certificates immediately.")
      (one-shot? #t)
      (start #~(lambda _
                 (let* ((renew-cert #$(renew-cert config))
                        (renew-res
                         (map
                          (lambda (handle)
                            (or (renew-cert handle)
                                (begin (sleep 15)
                                       (renew-cert handle))
                                (begin (sleep 15)
                                       (renew-cert handle))))
                          '#$handles)))
                   (when (memq 'change (pk renew-res))
                     (pk (#$reload-hook)))
                   ((@ (srfi srfi-1) every) identity renew-res))))
      (actions (list (shepherd-configuration-action config-file)))))))

Now the problem is with the `reload-hook'.  I tried two approaches (the
following is a snippet from define-configuration/no-serialization for
acme-client-configuration):

1. with-shepherd-action

  (reload-hook
   (gexp (with-imported-modules '((gnu services herd))
           #~(begin
               ((@ (gnu services herd) with-shepherd-action)
                'nginx ('reload) result result))))
   "Hook to invoke after certificate change.  The default is to reload nginx.")

This just hangs the shepherd for ever.  Even `herd status' no longer works.
Only recovery I found was hard reboot.

2. invoke

  (reload-hook
   (gexp (with-imported-modules '((guix build utils))
           #~((@ (guix build utils) invoke)
              ;; There probably is more elegant way to get the current shepherd.
              #$(file-append (shepherd-configuration-shepherd
                              (shepherd-configuration))
                             "/bin/herd") "reload" "nginx")))

This does work, but the round-trip via separate binary (and the way to figure it
out) is hardly elegant.

Could someone advice me on how the typical pattern for this should look?
Preferably while staying inside Guile (so no invoke).

Thanks and have a nice day,
Tomas Volf

--
There are only two hard things in Computer Science:
cache invalidation, naming things and off-by-one errors.

Attachment: signature.asc
Description: PGP signature


reply via email to

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