Hi,
Tomas Volf <[email protected]> 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’.