mcron.scm 6.1 KB

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