mcron.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017 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) (mcron2)
  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 mcron2))
  53. (jobs mcron-configuration-jobs ;list of <mcron-job>
  54. (default '())))
  55. (define (job-file job)
  56. (scheme-file "mcron-job" job))
  57. (define mcron-shepherd-services
  58. (match-lambda
  59. (($ <mcron-configuration> mcron ()) ;nothing to do!
  60. '())
  61. (($ <mcron-configuration> mcron jobs)
  62. (list (shepherd-service
  63. (provision '(mcron))
  64. (requirement '(user-processes))
  65. (modules `((srfi srfi-1)
  66. (srfi srfi-26)
  67. ,@%default-modules))
  68. (start #~(make-forkexec-constructor
  69. (list (string-append #$mcron "/bin/mcron")
  70. #$@(map job-file jobs))
  71. ;; Disable auto-compilation of the job files and set a
  72. ;; sane value for 'PATH'.
  73. #:environment-variables
  74. (cons* "GUILE_AUTO_COMPILE=0"
  75. "PATH=/run/current-system/profile/bin"
  76. (remove (cut string-prefix? "PATH=" <>)
  77. (environ)))))
  78. (stop #~(make-kill-destructor)))))))
  79. (define mcron-service-type
  80. (service-type (name 'mcron)
  81. (extensions
  82. (list (service-extension shepherd-root-service-type
  83. mcron-shepherd-services)
  84. (service-extension profile-service-type
  85. (compose list
  86. mcron-configuration-mcron))))
  87. (compose concatenate)
  88. (extend (lambda (config jobs)
  89. (mcron-configuration
  90. (inherit config)
  91. (jobs (append (mcron-configuration-jobs config)
  92. jobs)))))
  93. (default-value (mcron-configuration)))) ;empty job list
  94. (define* (mcron-service jobs #:optional (mcron mcron2))
  95. "Return an mcron service running @var{mcron} that schedules @var{jobs}, a
  96. list of gexps denoting mcron job specifications.
  97. This is a shorthand for:
  98. @example
  99. (service mcron-service-type
  100. (mcron-configuration (mcron mcron) (jobs jobs)))
  101. @end example
  102. "
  103. (service mcron-service-type
  104. (mcron-configuration (mcron mcron) (jobs jobs))))
  105. ;;; mcron.scm ends here