mcron.scm 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016-2020, 2023 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  4. ;;; Copyright © 2023 Bruno Victal <mirai@makinata.eu>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (gnu services mcron)
  21. #:use-module (gnu services)
  22. #:use-module (gnu services shepherd)
  23. #:use-module (gnu packages guile-xyz)
  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. mcron-configuration-log?
  34. mcron-configuration-log-file
  35. mcron-configuration-log-format
  36. mcron-configuration-date-format
  37. mcron-configuration-home-service?
  38. mcron-service-type))
  39. ;;; Commentary:
  40. ;;;
  41. ;;; This module implements a service that to run instances of GNU mcron, a
  42. ;;; periodic job execution daemon. Example of a service:
  43. ;;
  44. ;; (service mcron-service-type
  45. ;; (mcron-configuration
  46. ;; (jobs (list #~(job next-second-from
  47. ;; (lambda ()
  48. ;; (call-with-output-file "/dev/console"
  49. ;; (lambda (port)
  50. ;; (display "hello!\n" port)))))))))
  51. ;;;
  52. ;;; Code:
  53. ;; Configuration of mcron.
  54. ;; XXX: 'define-configuration' cannot be used here due to the need for
  55. ;; 'thunked' and 'innate' fields as well as 'this-mcron-configuration'.
  56. (define-record-type* <mcron-configuration> mcron-configuration
  57. make-mcron-configuration
  58. mcron-configuration?
  59. this-mcron-configuration
  60. (mcron mcron-configuration-mcron ;file-like
  61. (default mcron))
  62. (jobs mcron-configuration-jobs ;list of gexps
  63. (default '()))
  64. (log? mcron-configuration-log? ;Boolean
  65. (default #t))
  66. (log-file mcron-configuration-log-file ;string | gexp
  67. (thunked)
  68. (default
  69. (if (mcron-configuration-home-service?
  70. this-mcron-configuration)
  71. #~(string-append %user-log-dir "/mcron.log")
  72. "/var/log/mcron.log")))
  73. (log-format mcron-configuration-log-format ;string
  74. (default "~1@*~a ~a: ~a~%"))
  75. (date-format mcron-configuration-date-format ;string | #f
  76. (default #f))
  77. (home-service? mcron-configuration-home-service?
  78. (default for-home?) (innate)))
  79. (define (job-files mcron jobs)
  80. "Return a list of file-like object for JOBS, a list of gexps."
  81. (define (validated-file job)
  82. ;; This procedure behaves like 'scheme-file' but it runs 'mcron
  83. ;; --schedule' to detect any error in JOB.
  84. (computed-file "mcron-job"
  85. (with-imported-modules '((guix build utils))
  86. #~(begin
  87. (use-modules (guix build utils))
  88. (call-with-output-file "prologue"
  89. (lambda (port)
  90. ;; This prologue allows 'mcron --schedule' to
  91. ;; proceed no matter what #:user option is passed
  92. ;; to 'job'.
  93. (write '(set! getpw
  94. (const (getpwuid (getuid))))
  95. port)))
  96. (call-with-output-file "job"
  97. (lambda (port)
  98. (write '#$job port)))
  99. (invoke #+(file-append mcron "/bin/mcron")
  100. "--schedule=20" "prologue" "job")
  101. (copy-file "job" #$output)))
  102. #:options '(#:env-vars (("COLUMNS" . "150")))))
  103. (map validated-file jobs))
  104. (define (shepherd-schedule-action mcron files)
  105. "Return a Shepherd action that runs MCRON with '--schedule' for the given
  106. files."
  107. (shepherd-action
  108. (name 'schedule)
  109. (documentation
  110. "Display jobs that are going to be scheduled.")
  111. (procedure
  112. #~(lambda* (_ #:optional (n "5"))
  113. ;; XXX: This is a global side effect.
  114. (setenv "GUILE_AUTO_COMPILE" "0")
  115. ;; Run 'mcron' in a pipe so we can explicitly redirect its output to
  116. ;; 'current-output-port', which at this stage is bound to the client
  117. ;; connection.
  118. (let ((pipe (open-pipe* OPEN_READ
  119. #$(file-append mcron "/bin/mcron")
  120. (string-append "--schedule=" n)
  121. #$@files)))
  122. (let loop ()
  123. (match (read-line pipe 'concat)
  124. ((? eof-object?)
  125. (catch 'system-error
  126. (lambda ()
  127. (zero? (close-pipe pipe)))
  128. (lambda args
  129. ;; There's a race with the SIGCHLD handler, which
  130. ;; could call 'waitpid' before 'close-pipe' above does. If
  131. ;; we get ECHILD, that means we lost the race, but that's
  132. ;; fine.
  133. (or (= ECHILD (system-error-errno args))
  134. (apply throw args)))))
  135. (line
  136. (display line)
  137. (loop)))))))))
  138. (define (mcron-shepherd-services config)
  139. (match-record config <mcron-configuration>
  140. (mcron jobs log? log-file log-format date-format home-service?)
  141. (if (eq? jobs '())
  142. '() ;nothing to do
  143. (let ((files (job-files mcron jobs)))
  144. (list (shepherd-service
  145. (provision '(mcron))
  146. (requirement (if home-service?
  147. '()
  148. '(user-processes)))
  149. (modules `((srfi srfi-1)
  150. (srfi srfi-26)
  151. (ice-9 popen) ;for the 'schedule' action
  152. (ice-9 rdelim)
  153. (ice-9 match)
  154. ((shepherd support) #:select (%user-log-dir))
  155. ,@%default-modules))
  156. (start #~(make-forkexec-constructor
  157. (list #$(file-append mcron "/bin/mcron")
  158. #$@(if log?
  159. `("--log" "--log-format" ,log-format
  160. ,@(if date-format
  161. (list "--date-format"
  162. date-format)
  163. '()))
  164. '())
  165. #$@files)
  166. ;; Disable auto-compilation of the job files and
  167. ;; set a sane value for 'PATH'.
  168. #:environment-variables
  169. (cons* "GUILE_AUTO_COMPILE=0"
  170. "PATH=/run/current-system/profile/bin"
  171. (remove (cut string-prefix? "PATH=" <>)
  172. (environ)))
  173. #:log-file #$log-file))
  174. (stop #~(make-kill-destructor))
  175. (actions
  176. (list (shepherd-schedule-action mcron files)))))))))
  177. (define mcron-service-type
  178. (service-type (name 'mcron)
  179. (description
  180. "Run the mcron job scheduling daemon.")
  181. (extensions
  182. (list (service-extension shepherd-root-service-type
  183. mcron-shepherd-services)
  184. (service-extension profile-service-type
  185. (compose list
  186. mcron-configuration-mcron))))
  187. (compose concatenate)
  188. (extend (lambda (config jobs)
  189. (mcron-configuration
  190. (inherit config)
  191. (home-service?
  192. (mcron-configuration-home-service? config))
  193. (jobs (append (mcron-configuration-jobs config)
  194. jobs)))))
  195. (default-value (mcron-configuration)))) ;empty job list
  196. ;;; mcron.scm ends here