unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: Bruno Victal <mirai@makinata.eu>
To: 62465@debbugs.gnu.org
Cc: Bruno Victal <mirai@makinata.eu>, maxim.cournoyer@gmail.com
Subject: [bug#62465] [PATCH v2 2/2] services: mcron: Add instance name support for mcron.
Date: Wed, 29 Mar 2023 14:54:34 +0100	[thread overview]
Message-ID: <79a56d4be6ad19853bbae7dd3e6767b5b78806c2.1680098053.git.mirai@makinata.eu> (raw)
In-Reply-To: <26ad3fb85ba517dc1a500c287f07f20a00cec625.1680097409.git.mirai@makinata.eu>

Allow running more than one mcron instance.
Make log-file robust against operator error when provisioning
an additional mcron instance but log-file is left at its default value.

Follow-up to <https://issues.guix.gnu.org/62169>.

* gnu/services/mcron.scm (mcron-configuration)[instance-name]: New field.
[log-file]: Update description.
(mcron-shepherd-services): Implement instance-name support. Make log-file
robust against multiple instances writing to the same default log-file.
* doc/guix.texi (Scheduled Job Execution): Update it.
---
 doc/guix.texi          | 30 +++++++++++++++++++++++++++++-
 gnu/services/mcron.scm | 34 +++++++++++++++++++++++++++++-----
 2 files changed, 58 insertions(+), 6 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 5a597cf122..ba80b9660c 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -19383,6 +19383,10 @@ Scheduled Job Execution
 This is a list of symbols naming Shepherd services that this service
 will depend on.
 
+@item @code{instance} (type: maybe-symbol)
+Set the shepherd service name to @code{mcron-@var{instance}}.  This is
+useful when you want to have more than one mcron instance.
+
 @item @code{jobs} (default: @code{()}) (type: list-of-gexps)
 This is a list of gexps (@pxref{G-Expressions}), where each gexp
 corresponds to an mcron job specification (@pxref{Syntax, mcron job
@@ -19392,7 +19396,9 @@ Scheduled Job Execution
 Log messages to standard output.
 
 @item @code{log-file} (default: @code{"/var/log/mcron.log"}) (type: string)
-Log file location.
+Log file location.  When @var{instance} is set and @var{log-file} is
+kept at its @emph{default value}, it will log to
+@file{/var/log/mcron-@var{instance}.log}.
 
 @item @code{log-format} (default: @code{"~1@@*~a ~a: ~a~%"}) (type: string)
 @code{(ice-9 format)} format string for log messages.  The default value
@@ -19407,6 +19413,28 @@ Scheduled Job Execution
 @end deftp
 @c %end of fragment
 
+Occasionally, it is desirable to run another mcron instance to separate
+some of the jobs from the main mcron instance for clarity purposes, or if
+the jobs are very chatty and frequent. (for example, a heartbeat check
+every 30 seconds)
+
+The example below shows how an extra mcron service can be defined.
+@lisp
+(use (guix)
+     (guix records)
+     (gnu services)
+     (gnu services mcron))
+
+(define secondary-mcron-service-type
+  (service-type
+   (inherit mcron-service-type)
+   (name 'mcron-secondary)
+   (default-value
+     (mcron-configuration
+       (instance 'secondary)
+       (log-file "/var/log/mcron-secondary.log")))))
+@end lisp
+
 @node Log Rotation
 @subsection Log Rotation
 
diff --git a/gnu/services/mcron.scm b/gnu/services/mcron.scm
index 99eb0edd60..4d8ab4af2c 100644
--- a/gnu/services/mcron.scm
+++ b/gnu/services/mcron.scm
@@ -27,12 +27,14 @@ (define-module (gnu services mcron)
   #:use-module (guix records)
   #:use-module (guix gexp)
   #:use-module (srfi srfi-1)
+  #:use-module (ice-9 format)
   #:use-module (ice-9 match)
   #:use-module (ice-9 vlist)
   #:export (mcron-configuration
             mcron-configuration?
             mcron-configuration-mcron
             mcron-configuration-shepherd-requirement
+            mcron-configuration-instance
             mcron-configuration-jobs
             mcron-configuration-log?
             mcron-configuration-log-file
@@ -63,6 +65,7 @@ (define list-of-symbols?
   (list-of symbol?))
 
 (define-maybe/no-serialization string)
+(define-maybe/no-serialization symbol)
 
 (define-configuration/no-serialization mcron-configuration
   (mcron
@@ -74,6 +77,11 @@ (define-configuration/no-serialization mcron-configuration
    "This is a list of symbols naming Shepherd services that this service
 will depend on.")
 
+  (instance
+   maybe-symbol
+   "Set the shepherd service name to @code{mcron-@var{instance}}.
+This is useful when you want to have more than one mcron instance.")
+
   (jobs
    (list-of-gexps '())
    "This is a list of gexps (@pxref{G-Expressions}), where each gexp
@@ -86,7 +94,9 @@ (define-configuration/no-serialization mcron-configuration
 
   (log-file
    (string "/var/log/mcron.log")
-   "Log file location.")
+   "Log file location.  When @var{instance} is set and @var{log-file}
+is kept at its @emph{default value}, it will log to
+@file{/var/log/mcron-@var{instance}.log}.")
 
   (log-format
    (string "~1@*~a ~a: ~a~%")
@@ -167,12 +177,26 @@ (define (shepherd-schedule-action mcron files)
 
 (define (mcron-shepherd-services config)
   (match-record config <mcron-configuration>
-    (mcron shepherd-requirement jobs log? log-file log-format date-format)
+    (mcron shepherd-requirement instance
+     jobs log? log-file log-format date-format)
     (if (eq? jobs '())
         '()                             ;nothing to do
-        (let ((files (job-files mcron jobs)))
+        (let* ((files (job-files mcron jobs))
+               (instance-name
+                (format #f "mcron~@[-~a~]" (maybe-value instance)))
+               (service-name (string->symbol instance-name))
+               ;; Make log-file robust against multiple instances
+               ;; attempting to write to the same file when
+               ;; left at its default value.
+               (default-log-file (mcron-configuration-log-file
+                                       (mcron-configuration)))
+               (log-file* (if (and (maybe-value-set? instance)
+                                   (equal? log-file default-log-file))
+                              (string-append (dirname default-log-file)
+                                             "/" instance-name ".log")
+                              log-file)))
           (list (shepherd-service
-                 (provision '(mcron))
+                 (provision (list service-name))
                  (requirement `(user-processes ,@shepherd-requirement))
                  (modules `((srfi srfi-1)
                             (srfi srfi-26)
@@ -199,7 +223,7 @@ (define (mcron-shepherd-services config)
                                   (remove (cut string-prefix? "PATH=" <>)
                                           (environ)))
 
-                           #:log-file #$log-file))
+                           #:log-file #$log-file*))
                  (stop #~(make-kill-destructor))
                  (actions
                   (list (shepherd-schedule-action mcron files)))))))))
-- 
2.39.1





  reply	other threads:[~2023-03-29 13:55 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-26 18:18 [bug#62465] [PATCH] services: mcron: Add instance name support for mcron Bruno Victal
2023-03-29 13:44 ` [bug#62465] [PATCH v2 1/2] services: mcron: Add 'shepherd-requirement' field Bruno Victal
2023-03-29 13:54   ` Bruno Victal [this message]
2023-03-30 14:15 ` [bug#62465] [PATCH v3 " Bruno Victal
2023-03-30 14:15   ` [bug#62465] [PATCH v3 2/2] services: mcron: Add instance name support for mcron Bruno Victal
2023-04-01 17:35 ` [bug#62465] [PATCH v4 1/3] services: mcron: Add 'shepherd-requirement' field Bruno Victal
2023-04-01 17:35   ` [bug#62465] [PATCH v4 2/3] services: mcron: Add instance name support for mcron Bruno Victal
2023-04-01 17:35   ` [bug#62465] [PATCH v4 3/3] services: mcron: Add user-name, user-group and supplementary-groups fields Bruno Victal

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=79a56d4be6ad19853bbae7dd3e6767b5b78806c2.1680098053.git.mirai@makinata.eu \
    --to=mirai@makinata.eu \
    --cc=62465@debbugs.gnu.org \
    --cc=maxim.cournoyer@gmail.com \
    /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).