[bug#77041,v2,05/16] services: fstrim: Turn into a Shepherd timer.

Message ID 3e855021731ed7877824270ff0faed6a523965ca.1742642743.git.ludo@gnu.org
State New
Headers
Series Replacing mcron jobs by Shepherd timers |

Commit Message

Ludovic Courtès March 22, 2025, 11:36 a.m. UTC
  * gnu/services/linux.scm (mcron-time?): Remove.
(shepherd-calendar-event?): New procedure.
(fstrim-configuration)[schedule]: Change type to
‘shepherd-calendar-event’ and update docstring.
(fstrim-mcron-job): Rename to…
(fstrim-shepherd-services): … this.  Return a list of Shepherd services.
(fstrim-service-type): Adjust accordingly.
* doc/guix.texi (Linux Services): Update.

Change-Id: I9a7433cb15a1f4600470a915769d612e6e644dd8
---
 doc/guix.texi          | 14 +++++++-----
 gnu/services/linux.scm | 50 +++++++++++++++++++++++-------------------
 2 files changed, 36 insertions(+), 28 deletions(-)
  

Comments

pelzflorian (Florian Pelz) March 22, 2025, 1:25 p.m. UTC | #1
Ludovic Courtès <ludo@gnu.org> writes:
> +@item @code{schedule} (default: @code{"0 0 * * 0"}) (type: shepherd-calendar-event)
> +Schedule for launching @command{fstrim}, expressed as a string in
> +traditional cron syntax or as a gexp evaluating to a Shepherd calendar
> +event (@pxref{Timers,,, shepherd,The GNU Shepherd Manual}).  By default
> +this is set to run weekly on Sunday at 00:00.

Florian Pelz <pelzflorian@pelzflorian.de> writes:
> For fstrim doc/guix.texi,
> (type: shepherd-calendar-event) in the documentation might be better
> expressed as “type: Shepherd calendar event”, as there is no
> shepherd-calendar-event.

I still would prefer the type in the docs to be different.  Maybe just
calendar-event?  Because there is no shepherd-calendar-event.

This detail aside, this is a great and welcome patch series
(nonetheless, I haven’t tested it).  Thank you for addressing everyone’s
comments and writing documentation.

Regards,
Florian
  
Ludovic Courtès March 23, 2025, 6:28 p.m. UTC | #2
Hi,

"pelzflorian (Florian Pelz)" <pelzflorian@pelzflorian.de> skribis:

> Ludovic Courtès <ludo@gnu.org> writes:
>> +@item @code{schedule} (default: @code{"0 0 * * 0"}) (type: shepherd-calendar-event)
>> +Schedule for launching @command{fstrim}, expressed as a string in
>> +traditional cron syntax or as a gexp evaluating to a Shepherd calendar
>> +event (@pxref{Timers,,, shepherd,The GNU Shepherd Manual}).  By default
>> +this is set to run weekly on Sunday at 00:00.
>
> Florian Pelz <pelzflorian@pelzflorian.de> writes:
>> For fstrim doc/guix.texi,
>> (type: shepherd-calendar-event) in the documentation might be better
>> expressed as “type: Shepherd calendar event”, as there is no
>> shepherd-calendar-event.
>
> I still would prefer the type in the docs to be different.  Maybe just
> calendar-event?  Because there is no shepherd-calendar-event.

Oh right, I made this change.  Unfortunately it will be undoed next time
somebody generates the doc.

Thanks for your feedback,
Ludo’.
  

Patch

diff --git a/doc/guix.texi b/doc/guix.texi
index eb0a18eff4..e194919a9d 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -42118,6 +42118,7 @@  Linux Services
 @end defvar
 
 @c %start of fragment
+
 @deftp {Data Type} fstrim-configuration
 Available @code{fstrim-configuration} fields are:
 
@@ -42125,11 +42126,11 @@  Linux Services
 @item @code{package} (default: @code{util-linux}) (type: file-like)
 The package providing the @command{fstrim} command.
 
-@item @code{schedule} (default: @code{"0 0 * * 0"}) (type: mcron-time)
-Schedule for launching @command{fstrim}.  This can be a procedure, a
-list or a string.  For additional information, see @ref{Guile
-Syntax,,Job specification,mcron,the mcron manual}.  By default this is
-set to run weekly on Sunday at 00:00.
+@item @code{schedule} (default: @code{"0 0 * * 0"}) (type: shepherd-calendar-event)
+Schedule for launching @command{fstrim}, expressed as a string in
+traditional cron syntax or as a gexp evaluating to a Shepherd calendar
+event (@pxref{Timers,,, shepherd,The GNU Shepherd Manual}).  By default
+this is set to run weekly on Sunday at 00:00.
 
 @item @code{listed-in} (default: @code{'("/etc/fstab" "/proc/self/mountinfo")}) (type: maybe-list-of-strings)
 List of files in fstab or kernel mountinfo format.  All missing or empty
@@ -42148,7 +42149,10 @@  Linux Services
 more information).
 
 @end table
+
 @end deftp
+
+
 @c %end of fragment
 
 @cindex modprobe
diff --git a/gnu/services/linux.scm b/gnu/services/linux.scm
index b22dbeefa0..d7aee1b82e 100644
--- a/gnu/services/linux.scm
+++ b/gnu/services/linux.scm
@@ -34,7 +34,6 @@  (define-module (gnu services linux)
   #:use-module (gnu services admin)
   #:use-module (gnu services base)
   #:use-module (gnu services configuration)
-  #:use-module (gnu services mcron)
   #:use-module (gnu services shepherd)
   #:use-module (gnu packages linux)
   #:use-module (srfi srfi-1)
@@ -196,8 +195,8 @@  (define earlyoom-service-type
 ;;; fstrim
 ;;;
 
-(define (mcron-time? x)
-  (or (procedure? x) (string? x) (list? x)))
+(define (shepherd-calendar-event? x)
+  (or (string? x) (gexp? x)))
 
 (define-maybe list-of-strings (prefix fstrim-))
 
@@ -216,11 +215,11 @@  (define-configuration fstrim-configuration
     "The package providing the @command{fstrim} command."
     empty-serializer)
   (schedule
-   (mcron-time "0 0 * * 0")
-   "Schedule for launching @command{fstrim}.  This can be a procedure, a list
-or a string.  For additional information, see @ref{Guile Syntax,,
-Job specification, mcron, the mcron manual}.  By default this is set to run
-weekly on Sunday at 00:00."
+   (shepherd-calendar-event "0 0 * * 0")
+   "Schedule for launching @command{fstrim}, expressed as a string in
+traditional cron syntax or as a gexp evaluating to a Shepherd calendar
+event (@pxref{Timers,,, shepherd, The GNU Shepherd Manual}).  By default this
+is set to run weekly on Sunday at 00:00."
    empty-serializer)
   ;; The following are fstrim-related options.
   (listed-in
@@ -251,26 +250,31 @@  (define (serialize-fstrim-configuration config)
                   rcons
                   fstrim-configuration-fields))
 
-(define (fstrim-mcron-job config)
-  (match-record config <fstrim-configuration> (package schedule)
-    #~(job
-       ;; Note: The “if” below is to ensure that
-       ;; lists are ungexp'd correctly since @var{schedule}
-       ;; can be either a procedure, a string or a list.
-       #$(if (list? schedule)
-             #~'(#$@schedule)
-             schedule)
-       (lambda ()
-         (system* #$(file-append package "/sbin/fstrim")
-                  #$@(serialize-fstrim-configuration config)))
-       "fstrim")))
+(define (fstrim-shepherd-services config)
+  (match-record config <fstrim-configuration>
+    (package schedule)
+    (list (shepherd-service
+           (provision '(fstrim))
+           (requirement '(user-processes))
+           (modules '((shepherd service timer)))
+           (start #~(make-timer-constructor
+                     #$(if (string? schedule)
+                           #~(cron-string->calendar-event #$schedule)
+                           schedule)
+                     (command
+                      (list #$(file-append package "/sbin/fstrim")
+                            #$@(serialize-fstrim-configuration config)))
+                     #:wait-for-termination? #t))
+           (stop #~(make-timer-destructor))
+           (documentation "Periodically run the 'fstrim' command.")
+           (actions (list shepherd-trigger-action))))))
 
 (define fstrim-service-type
   (service-type
    (name 'fstrim)
    (extensions
-    (list (service-extension mcron-service-type
-                             (compose list fstrim-mcron-job))))
+    (list (service-extension shepherd-root-service-type
+                             fstrim-shepherd-services)))
    (description "Discard unused blocks from file systems.")
    (default-value (fstrim-configuration))))