Commit | Line | Data |
---|---|---|
c311089b | 1 | ;;; GNU Guix --- Functional package management for GNU |
6a7c4636 | 2 | ;;; Copyright © 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
c311089b LC |
3 | ;;; |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
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. | |
10 | ;;; | |
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. | |
15 | ;;; | |
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/>. | |
18 | ||
19 | (define-module (gnu services mcron) | |
20 | #:use-module (gnu services) | |
c311089b | 21 | #:use-module (gnu services shepherd) |
6a7c4636 | 22 | #:use-module (gnu packages guile-xyz) |
84a2de36 | 23 | #:use-module (guix deprecation) |
c311089b LC |
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 | |
30 | mcron-configuration? | |
31 | mcron-configuration-mcron | |
32 | mcron-configuration-jobs | |
33 | ||
34 | mcron-service-type | |
35 | mcron-service)) | |
36 | ||
37 | ;;; Commentary: | |
38 | ;;; | |
39 | ;;; This module implements a service that to run instances of GNU mcron, a | |
40 | ;;; periodic job execution daemon. Example of a service: | |
41 | ;; | |
42 | ;; (service mcron-service-type | |
43 | ;; (mcron-configuration | |
44 | ;; (jobs (list #~(job next-second-from | |
45 | ;; (lambda () | |
46 | ;; (call-with-output-file "/dev/console" | |
47 | ;; (lambda (port) | |
48 | ;; (display "hello!\n" port))))))))) | |
49 | ;;; | |
50 | ;;; Code: | |
51 | ||
52 | (define-record-type* <mcron-configuration> mcron-configuration | |
53 | make-mcron-configuration | |
54 | mcron-configuration? | |
55 | (mcron mcron-configuration-mcron ;package | |
cfbf6de1 | 56 | (default mcron)) |
c311089b LC |
57 | (jobs mcron-configuration-jobs ;list of <mcron-job> |
58 | (default '()))) | |
59 | ||
949672c9 LC |
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)) | |
67 | #~(begin | |
68 | (use-modules (guix build utils)) | |
69 | ||
70 | (call-with-output-file "job" | |
71 | (lambda (port) | |
72 | (write '#$job port))) | |
73 | ||
74 | (invoke #+(file-append mcron "/bin/mcron") | |
75 | "--schedule=20" "job") | |
76 | (copy-file "job" #$output))) | |
77 | #:options '(#:env-vars (("COLUMNS" . "150"))))) | |
78 | ||
79 | (map validated-file jobs)) | |
c311089b | 80 | |
147c5aa5 LC |
81 | (define (shepherd-schedule-action mcron files) |
82 | "Return a Shepherd action that runs MCRON with '--schedule' for the given | |
83 | files." | |
84 | (shepherd-action | |
85 | (name 'schedule) | |
86 | (documentation | |
87 | "Display jobs that are going to be scheduled.") | |
88 | (procedure | |
89 | #~(lambda* (_ #:optional (n "5")) | |
90 | ;; XXX: This is a global side effect. | |
91 | (setenv "GUILE_AUTO_COMPILE" "0") | |
92 | ||
93 | ;; Run 'mcron' in a pipe so we can explicitly redirect its output to | |
94 | ;; 'current-output-port', which at this stage is bound to the client | |
95 | ;; connection. | |
96 | (let ((pipe (open-pipe* OPEN_READ | |
97 | #$(file-append mcron "/bin/mcron") | |
98 | (string-append "--schedule=" n) | |
99 | #$@files))) | |
100 | (let loop () | |
101 | (match (read-line pipe 'concat) | |
102 | ((? eof-object?) | |
103 | (catch 'system-error | |
104 | (lambda () | |
105 | (zero? (close-pipe pipe))) | |
106 | (lambda args | |
19087774 | 107 | ;; There's a race with the SIGCHLD handler, which |
147c5aa5 LC |
108 | ;; could call 'waitpid' before 'close-pipe' above does. If |
109 | ;; we get ECHILD, that means we lost the race, but that's | |
110 | ;; fine. | |
111 | (or (= ECHILD (system-error-errno args)) | |
112 | (apply throw args))))) | |
113 | (line | |
114 | (display line) | |
115 | (loop))))))))) | |
116 | ||
c311089b LC |
117 | (define mcron-shepherd-services |
118 | (match-lambda | |
119 | (($ <mcron-configuration> mcron ()) ;nothing to do! | |
120 | '()) | |
121 | (($ <mcron-configuration> mcron jobs) | |
949672c9 | 122 | (let ((files (job-files mcron jobs))) |
147c5aa5 LC |
123 | (list (shepherd-service |
124 | (provision '(mcron)) | |
125 | (requirement '(user-processes)) | |
126 | (modules `((srfi srfi-1) | |
127 | (srfi srfi-26) | |
128 | (ice-9 popen) ;for the 'schedule' action | |
129 | (ice-9 rdelim) | |
130 | (ice-9 match) | |
131 | ,@%default-modules)) | |
132 | (start #~(make-forkexec-constructor | |
133 | (list (string-append #$mcron "/bin/mcron") #$@files) | |
134 | ||
135 | ;; Disable auto-compilation of the job files and set a | |
136 | ;; sane value for 'PATH'. | |
137 | #:environment-variables | |
138 | (cons* "GUILE_AUTO_COMPILE=0" | |
139 | "PATH=/run/current-system/profile/bin" | |
140 | (remove (cut string-prefix? "PATH=" <>) | |
f8cd07e8 CB |
141 | (environ))) |
142 | ||
143 | #:log-file "/var/log/mcron.log")) | |
147c5aa5 | 144 | (stop #~(make-kill-destructor)) |
c311089b | 145 | |
147c5aa5 LC |
146 | (actions |
147 | (list (shepherd-schedule-action mcron files))))))))) | |
c311089b LC |
148 | |
149 | (define mcron-service-type | |
150 | (service-type (name 'mcron) | |
e7d45693 RV |
151 | (description |
152 | "Run the mcron job scheduling daemon.") | |
c311089b LC |
153 | (extensions |
154 | (list (service-extension shepherd-root-service-type | |
155 | mcron-shepherd-services) | |
156 | (service-extension profile-service-type | |
157 | (compose list | |
158 | mcron-configuration-mcron)))) | |
159 | (compose concatenate) | |
160 | (extend (lambda (config jobs) | |
161 | (mcron-configuration | |
162 | (inherit config) | |
163 | (jobs (append (mcron-configuration-jobs config) | |
3d3c5650 LC |
164 | jobs))))) |
165 | (default-value (mcron-configuration)))) ;empty job list | |
c311089b | 166 | |
84a2de36 LC |
167 | (define-deprecated (mcron-service jobs #:optional (mcron mcron)) |
168 | mcron-service-type | |
c311089b LC |
169 | "Return an mcron service running @var{mcron} that schedules @var{jobs}, a |
170 | list of gexps denoting mcron job specifications. | |
171 | ||
172 | This is a shorthand for: | |
173 | @example | |
174 | (service mcron-service-type | |
175 | (mcron-configuration (mcron mcron) (jobs jobs))) | |
176 | @end example | |
177 | " | |
178 | (service mcron-service-type | |
179 | (mcron-configuration (mcron mcron) (jobs jobs)))) | |
180 | ||
181 | ;;; mcron.scm ends here |