all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Ludovic Courtès" <ludo@gnu.org>
To: guix-devel@gnu.org
Subject: Re: How to invoke shepherd action from shepherd action?
Date: Thu, 11 Jul 2024 11:55:27 +0200	[thread overview]
Message-ID: <87ed802qy8.fsf@gnu.org> (raw)
In-Reply-To: <ZogQGD295M23M8Wn@ws> (Tomas Volf's message of "Fri, 5 Jul 2024 17:24:08 +0200")

Hi,

Tomas Volf <~@wolfsden.cz> skribis:

> 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.

You have to always keep in mind where code is running.  ‘start’, ‘stop’,
and actions are running within the shepherd, in PID 1.

Conversely, the (gnu services herd) module provides utilities to
*connect* to a running shepherd *as a client* (a separate process), very
much like what the ‘herd’ command does.

From shepherd itself, you probably don’t want to connect to shepherd to
perform actions.  Instead, you can use the Shepherd’s API:

  https://www.gnu.org/software/shepherd/manual/html_node/Services.html

In this case, perhaps you’ll want to call ‘perform-service-action’.

(Aside: why did your attempt hang?  While overkill, it should be fine to
connect to shepherd from within shepherd, right?  The problem here is
that ‘with-shepherd-action’ creates a socket *without* O_NONBLOCK.
Thus, the entire shepherd process is blocked when trying to read from
that socket.)

I hope this helps!  Let me know if you think of ways to improve
documentation in this area.

Ludo’.


      reply	other threads:[~2024-07-11  9:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-05 15:24 How to invoke shepherd action from shepherd action? Tomas Volf
2024-07-11  9:55 ` Ludovic Courtès [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ed802qy8.fsf@gnu.org \
    --to=ludo@gnu.org \
    --cc=guix-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.