mcron.scm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  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. (define-module (gnu services mcron)
  19. #:use-module (gnu services)
  20. #:use-module (gnu services shepherd)
  21. #:use-module (gnu packages guile-xyz)
  22. #:use-module (guix deprecation)
  23. #:use-module (guix records)
  24. #:use-module (guix gexp)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (ice-9 match)
  27. #:use-module (ice-9 vlist)
  28. #:export (mcron-configuration
  29. mcron-configuration?
  30. mcron-configuration-mcron
  31. mcron-configuration-jobs
  32. mcron-service-type))
  33. ;;; Commentary:
  34. ;;;
  35. ;;; This module implements a service that to run instances of GNU mcron, a
  36. ;;; periodic job execution daemon. Example of a service:
  37. ;;
  38. ;; (service mcron-service-type
  39. ;; (mcron-configuration
  40. ;; (jobs (list #~(job next-second-from
  41. ;; (lambda ()
  42. ;; (call-with-output-file "/dev/console"
  43. ;; (lambda (port)
  44. ;; (display "hello!\n" port)))))))))
  45. ;;;
  46. ;;; Code:
  47. (define-record-type* <mcron-configuration> mcron-configuration
  48. make-mcron-configuration
  49. mcron-configuration?
  50. (mcron mcron-configuration-mcron ;file-like
  51. (default mcron))
  52. (jobs mcron-configuration-jobs ;list of <mcron-job>
  53. (default '())))
  54. (define (job-files mcron jobs)
  55. "Return a list of file-like object for JOBS, a list of gexps."
  56. (define (validated-file job)
  57. ;; This procedure behaves like 'scheme-file' but it runs 'mcron
  58. ;; --schedule' to detect any error in JOB.
  59. (computed-file "mcron-job"
  60. (with-imported-modules '((guix build utils))
  61. #~(begin
  62. (use-modules (guix build utils))
  63. (call-with-output-file "prologue"
  64. (lambda (port)
  65. ;; This prologue allows 'mcron --schedule' to
  66. ;; proceed no matter what #:user option is passed
  67. ;; to 'job'.
  68. (write '(set! getpw
  69. (const (getpwuid (getuid))))
  70. port)))
  71. (call-with-output-file "job"
  72. (lambda (port)
  73. (write '#$job port)))
  74. (invoke #+(file-append mcron "/bin/mcron")
  75. "--schedule=20" "prologue" "job")
  76. (copy-file "job" #$output)))
  77. #:options '(#:env-vars (("COLUMNS" . "150")))))
  78. (map validated-file jobs))
  79. (define (shepherd-schedule-action mcron files)
  80. "Return a Shepherd action that runs MCRON with '--schedule' for the given
  81. files."
  82. (shepherd-action
  83. (name 'schedule)
  84. (documentation
  85. "Display jobs that are going to be scheduled.")
  86. (procedure
  87. #~(lambda* (_ #:optional (n "5"))
  88. ;; XXX: This is a global side effect.
  89. (setenv "GUILE_AUTO_COMPILE" "0")
  90. ;; Run 'mcron' in a pipe so we can explicitly redirect its output to
  91. ;; 'current-output-port', which at this stage is bound to the client
  92. ;; connection.
  93. (let ((pipe (open-pipe* OPEN_READ
  94. #$(file-append mcron "/bin/mcron")
  95. (string-append "--schedule=" n)
  96. #$@files)))
  97. (let loop ()
  98. (match (read-line pipe 'concat)
  99. ((? eof-object?)
  100. (catch 'system-error
  101. (lambda ()
  102. (zero? (close-pipe pipe)))
  103. (lambda args
  104. ;; There's a race with the SIGCHLD handler, which
  105. ;; could call 'waitpid' before 'close-pipe' above does. If
  106. ;; we get ECHILD, that means we lost the race, but that's
  107. ;; fine.
  108. (or (= ECHILD (system-error-errno args))
  109. (apply throw args)))))
  110. (line
  111. (display line)
  112. (loop)))))))))
  113. (define mcron-shepherd-services
  114. (match-lambda
  115. (($ <mcron-configuration> mcron ()) ;nothing to do!
  116. '())
  117. (($ <mcron-configuration> mcron jobs)
  118. (let ((files (job-files mcron jobs)))
  119. (list (shepherd-service
  120. (provision '(mcron))
  121. (requirement '(user-processes))
  122. (modules `((srfi srfi-1)
  123. (srfi srfi-26)
  124. (ice-9 popen) ;for the 'schedule' action
  125. (ice-9 rdelim)
  126. (ice-9 match)
  127. ,@%default-modules))
  128. (start #~(make-forkexec-constructor
  129. (list (string-append #$mcron "/bin/mcron") #$@files)
  130. ;; Disable auto-compilation of the job files and set a
  131. ;; sane value for 'PATH'.
  132. #:environment-variables
  133. (cons* "GUILE_AUTO_COMPILE=0"
  134. "PATH=/run/current-system/profile/bin"
  135. (remove (cut string-prefix? "PATH=" <>)
  136. (environ)))
  137. #:log-file "/var/log/mcron.log"))
  138. (stop #~(make-kill-destructor))
  139. (actions
  140. (list (shepherd-schedule-action mcron files)))))))))
  141. (define mcron-service-type
  142. (service-type (name 'mcron)
  143. (description
  144. "Run the mcron job scheduling daemon.")
  145. (extensions
  146. (list (service-extension shepherd-root-service-type
  147. mcron-shepherd-services)
  148. (service-extension profile-service-type
  149. (compose list
  150. mcron-configuration-mcron))))
  151. (compose concatenate)
  152. (extend (lambda (config jobs)
  153. (mcron-configuration
  154. (inherit config)
  155. (jobs (append (mcron-configuration-jobs config)
  156. jobs)))))
  157. (default-value (mcron-configuration)))) ;empty job list
  158. ;;; mcron.scm ends here