unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: ludo@gnu.org (Ludovic Courtès)
To: Alex Sassmannshausen <alex.sassmannshausen@gmail.com>
Cc: guix-devel <guix-devel@gnu.org>
Subject: Re: dmd: Unload one or all services at runtime.
Date: Thu, 27 Feb 2014 22:50:06 +0100	[thread overview]
Message-ID: <87a9dcrzsh.fsf@gnu.org> (raw)
In-Reply-To: <87d2ib1u01.fsf@gmail.com> (Alex Sassmannshausen's message of "Tue, 25 Feb 2014 09:22:54 +0100")

Hi!

Alex Sassmannshausen <alex.sassmannshausen@gmail.com> skribis:

> The attached patch is a first step towards this aim. It allows you to
> unload individual services (by their name) or all known user
> services. It even allows you to unload the special service dmd itself,
> which is the same as sending the stop command to dmd.
>
> For example:
> $: dmd rm dmd apache // Unload the apache server
> $: dmd rm dmd web-server // Unload the service providing
>                          // a web server if there is only one.

What happens here if there’s a service running that depends on
‘web-server’?  I’m guessing it should be stopped.

> $: dmd rm dmd all // Unload all user services.
>
> You can then reload the relevant service's definition (or, if you ran
> 'dmd rm dmd all', you can reload your dmd.d/init.scm).
>
> In future this might provide the foundation for a 'reload' action for
> dmd.

Cool!

> From 1b4ec0f2261e1231ff21c5486dc6e75466c5829e Mon Sep 17 00:00:00 2001
> From: Alex Sassmannshausen <alex.sassmannshausen@gmail.com>
> Date: Sun, 23 Feb 2014 11:06:14 +0100
> Subject: [PATCH] dmd: Add dmd action rm: remove known services.
>
> * modules/dmd/service.scm (deregister-services): New procedure.
>   (dmd-service): Add new action: rm.
> * dmd.texi (The 'dmd' and 'unknown' services): Document 'rm'.

There are 3 terms used here: remove, unload, and deregister.  For
consistency, I would stick to just one, perhaps ‘unload’.  WDYT?

> +@item rm @var{service-name}
> +Attempt to remove the service identified by @var{service-name}.
> +@command{dmd} will first stop the service, if necessary, and then
> +remove it from the list of registered services.  If @var{service-name}
> +simply does not exist, output a warning and do nothing.  If it exists,
> +but is provided by several services, output a warning and do nothing.

Mention what happens to services depending on it.

> +This latter case might occur for instance with the fictional service
> +web-server, which might be provided by both apache and nginx.  If

Should be @code{web-server}, @code{apache}, and @code{nginx}.

> +(define (deregister-service service-name)
> +  "For each string in SERVICE-NAME, stop the associated service if
> +necessary and remove it from the services table.  If SERVICE-NAME is
> +the special string 'all', remove all services except for dmd.
> +
> +This will remove a service either if it is identified by its canonical
> +name, or if it is the only service providing the service that is
> +requested to be removed."
> +  (define (deregister service)
> +    (if (running? service)
> +        (stop service))
> +    ;; Remove services provided by service from the hash table.
> +    (for-each
> +     (lambda (name)
> +       (let ((old (lookup-services name)))
> +         (if (= 1 (length old))
> +             ;; Only service provides this service, ergo:
> +             (begin
> +               ;; Reduce provided services count
> +               (set! services-cnt (1- services-cnt))
> +               ;; Remove service entry from services.
> +               (hashq-remove! services name))
> +             ;; ELSE: remove service from providing services.
> +             (hashq-set! services name
> +                         (remove
> +                          (lambda (lk-service)
> +                            (eq? (canonical-name service)
> +                                 (canonical-name lk-service)))
> +                          old)))))
> +     (provided-by service)))
> +  (define (service-pairs)
> +    "Return '(name . service) of all user-registered services."
> +    (filter (lambda (service-pair) (if service-pair #t #f))
> +            (hash-map->list
> +             (lambda (key value)
> +               (let ((can-name (canonical-name (car value))))
> +                 (if (and (null? (cdr value))
> +                          (eq? key can-name)
> +                          (not (eq? can-name 'dmd)))
> +                     (cons key (car value)) #f)))

Note that the two arms of ‘if’ should always be aligned.
Also, (ice-9 match) can help here IMO:

  (lambda (key value)
    (match value
      ((service)    ; only one service associated with KEY
       (and (eq? key (canonical-name service))
            (not (eq? key 'dmd))
            (cons key service)))
      (_ #f)))      ; two or more services associated with KEY

> +           ;; Removing only one service.
> +           (let ((services (lookup-services name)))
> +             (cond ((null? services)
> +                    (local-output "'~a' is an uknown service." name))
> +                   ((= 1 (length services))
> +                    ;; Are we removing a user service…
> +                    (if (eq? (canonical-name (car services)) name)
> +                        (local-output "Removing service '~a'..."
> +                                      name)
> +                        ;; or a virtual service?
> +                        (local-output
> +                         (string-append "Removing service '~a' "
> +                                        "providing '~a'...")
> +                         (canonical-name (car services)) name))
> +                    (deregister (car services))
> +                    (local-output "Done."))
> +                   (else
> +                    ;; Service name to ambiguous
> +                    (local-output
> +                     (string-append "'~a' identifies more than one "
> +                                    "service to be stopped: '~a'.")
> +                     name (map canonical-name services)))))))))

Likewise:

  (match (lookup-services name)
    (()             ; unknown service
     ...)
    ((service)      ; only SERVICE provides NAME
     ...)
    ((services ...) ; ambiguous NAME
     ...))

What about adding a test case, as a shell script (since that’s what we
have currently)?  I’m thinking of something running dmd with a
configuration file that contains a service definition, then running
‘deco unload foo’, and then making sure that ‘deco status dmd’ doesn’t
list it any longer.

Bonus point if there’s another service depending on the one we’re
unloading.

WDYT?  Could you send an updated patch?

Thanks!

Ludo’.

  reply	other threads:[~2014-02-27 21:50 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-25  8:22 dmd: Unload one or all services at runtime Alex Sassmannshausen
2014-02-27 21:50 ` Ludovic Courtès [this message]
2014-03-10 17:39   ` [PATCH 1/2] dmd: Add dmd action unload: unload known services Alex Sassmannshausen
2014-03-10 17:39     ` [PATCH 2/2] dmd: Add dmd action 'reload': unload all; load Alex Sassmannshausen
2014-03-12 17:49       ` Ludovic Courtès
2014-03-19 14:25         ` dmd: Unload one or all services at runtime Alex Sassmannshausen
2014-03-19 14:25           ` [PATCH] dmd: Add dmd action 'reload': unload all; load Alex Sassmannshausen
2014-03-25 20:33             ` Ludovic Courtès
2014-03-25 20:30           ` dmd: Unload one or all services at runtime Ludovic Courtès
2014-03-12 17:45     ` [PATCH 1/2] dmd: Add dmd action unload: unload known services Ludovic Courtès

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

  List information: https://guix.gnu.org/

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

  git send-email \
    --in-reply-to=87a9dcrzsh.fsf@gnu.org \
    --to=ludo@gnu.org \
    --cc=alex.sassmannshausen@gmail.com \
    --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 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).