1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (gnu services mcron)
20 #:use-module (gnu services)
21 #:use-module (gnu services shepherd)
22 #:use-module (gnu packages guile-xyz)
23 #:use-module (guix deprecation)
24 #:use-module (guix records)
25 #:use-module (guix gexp)
26 #:use-module (srfi srfi-1)
27 #:use-module (ice-9 match)
28 #:use-module (ice-9 vlist)
29 #:export (mcron-configuration
31 mcron-configuration-mcron
32 mcron-configuration-jobs
39 ;;; This module implements a service that to run instances of GNU mcron, a
40 ;;; periodic job execution daemon. Example of a service:
42 ;; (service mcron-service-type
43 ;; (mcron-configuration
44 ;; (jobs (list #~(job next-second-from
46 ;; (call-with-output-file "/dev/console"
48 ;; (display "hello!\n" port)))))))))
52 (define-record-type* <mcron-configuration> mcron-configuration
53 make-mcron-configuration
55 (mcron mcron-configuration-mcron ;package
57 (jobs mcron-configuration-jobs ;list of <mcron-job>
60 (define (job-files mcron jobs)
61 "Return a list of file-like object for JOBS, a list of gexps."
62 (define (validated-file job)
63 ;; This procedure behaves like 'scheme-file' but it runs 'mcron
64 ;; --schedule' to detect any error in JOB.
65 (computed-file "mcron-job"
66 (with-imported-modules '((guix build utils))
68 (use-modules (guix build utils))
70 (call-with-output-file "prologue"
72 ;; This prologue allows 'mcron --schedule' to
73 ;; proceed no matter what #:user option is passed
76 (const (getpwuid (getuid))))
79 (call-with-output-file "job"
83 (invoke #+(file-append mcron "/bin/mcron")
84 "--schedule=20" "prologue" "job")
85 (copy-file "job" #$output)))
86 #:options '(#:env-vars (("COLUMNS" . "150")))))
88 (map validated-file jobs))
90 (define (shepherd-schedule-action mcron files)
91 "Return a Shepherd action that runs MCRON with '--schedule' for the given
96 "Display jobs that are going to be scheduled.")
98 #~(lambda* (_ #:optional (n "5"))
99 ;; XXX: This is a global side effect.
100 (setenv "GUILE_AUTO_COMPILE" "0")
102 ;; Run 'mcron' in a pipe so we can explicitly redirect its output to
103 ;; 'current-output-port', which at this stage is bound to the client
105 (let ((pipe (open-pipe* OPEN_READ
106 #$(file-append mcron "/bin/mcron")
107 (string-append "--schedule=" n)
110 (match (read-line pipe 'concat)
114 (zero? (close-pipe pipe)))
116 ;; There's a race with the SIGCHLD handler, which
117 ;; could call 'waitpid' before 'close-pipe' above does. If
118 ;; we get ECHILD, that means we lost the race, but that's
120 (or (= ECHILD (system-error-errno args))
121 (apply throw args)))))
126 (define mcron-shepherd-services
128 (($ <mcron-configuration> mcron ()) ;nothing to do!
130 (($ <mcron-configuration> mcron jobs)
131 (let ((files (job-files mcron jobs)))
132 (list (shepherd-service
134 (requirement '(user-processes))
135 (modules `((srfi srfi-1)
137 (ice-9 popen) ;for the 'schedule' action
141 (start #~(make-forkexec-constructor
142 (list (string-append #$mcron "/bin/mcron") #$@files)
144 ;; Disable auto-compilation of the job files and set a
145 ;; sane value for 'PATH'.
146 #:environment-variables
147 (cons* "GUILE_AUTO_COMPILE=0"
148 "PATH=/run/current-system/profile/bin"
149 (remove (cut string-prefix? "PATH=" <>)
152 #:log-file "/var/log/mcron.log"))
153 (stop #~(make-kill-destructor))
156 (list (shepherd-schedule-action mcron files)))))))))
158 (define mcron-service-type
159 (service-type (name 'mcron)
161 "Run the mcron job scheduling daemon.")
163 (list (service-extension shepherd-root-service-type
164 mcron-shepherd-services)
165 (service-extension profile-service-type
167 mcron-configuration-mcron))))
168 (compose concatenate)
169 (extend (lambda (config jobs)
172 (jobs (append (mcron-configuration-jobs config)
174 (default-value (mcron-configuration)))) ;empty job list
176 (define-deprecated (mcron-service jobs #:optional (mcron mcron))
178 "Return an mcron service running @var{mcron} that schedules @var{jobs}, a
179 list of gexps denoting mcron job specifications.
181 This is a shorthand for:
183 (service mcron-service-type
184 (mcron-configuration (mcron mcron) (jobs jobs)))
187 (service mcron-service-type
188 (mcron-configuration (mcron mcron) (jobs jobs))))
190 ;;; mcron.scm ends here