mcron.scm 7.5 KB

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