mcron.scm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 Andrew Tropin <andrew@trop.in>
  3. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  4. ;;; Copyright © 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  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 home services mcron)
  21. #:use-module (gnu packages guile-xyz)
  22. #:use-module (gnu home services)
  23. #:use-module (gnu services configuration)
  24. #:use-module (gnu services shepherd)
  25. #:use-module (gnu home services shepherd)
  26. #:use-module (guix records)
  27. #:use-module (guix gexp)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (ice-9 match)
  30. #:export (home-mcron-configuration
  31. home-mcron-service-type))
  32. ;;; Commentary:
  33. ;;
  34. ;; Service for the GNU mcron cron job manager.
  35. ;;
  36. ;; Example configuration, the first job runs mbsync once every ten
  37. ;; minutes, the second one writes "Mcron service" to ~/mcron-file once
  38. ;; every minute.
  39. ;;
  40. ;; (service home-mcron-service-type
  41. ;; (home-mcron-configuration
  42. ;; (jobs (list #~(job '(next-minute
  43. ;; (range 0 60 10))
  44. ;; (lambda ()
  45. ;; (system* "mbsync" "--all")))
  46. ;; #~(job next-minute-from
  47. ;; (lambda ()
  48. ;; (call-with-output-file (string-append (getenv "HOME")
  49. ;; "/mcron-file")
  50. ;; (lambda (port)
  51. ;; (display "Mcron service" port)))))))))
  52. ;;
  53. ;;; Code:
  54. (define list-of-gexps?
  55. (list-of gexp?))
  56. (define-configuration/no-serialization home-mcron-configuration
  57. (mcron (file-like mcron) "The mcron package to use.")
  58. (jobs
  59. (list-of-gexps '())
  60. "This is a list of gexps (@pxref{G-Expressions}), where each gexp
  61. corresponds to an mcron job specification (@pxref{Syntax, mcron job
  62. specifications,, mcron, GNU@tie{}mcron}).")
  63. (log? (boolean #t) "Log messages to standard output.")
  64. (log-format
  65. (string "~1@*~a ~a: ~a~%")
  66. "@code{(ice-9 format)} format string for log messages. The default value
  67. produces messages like \"@samp{@var{pid} @var{name}:
  68. @var{message}\"} (@pxref{Invoking mcron, Invoking,, mcron, GNU@tie{}mcron}).
  69. Each message is also prefixed by a timestamp by GNU Shepherd."))
  70. (define job-files (@@ (gnu services mcron) job-files))
  71. (define shepherd-schedule-action
  72. (@@ (gnu services mcron) shepherd-schedule-action))
  73. (define (home-mcron-shepherd-services config)
  74. (match-record config <home-mcron-configuration>
  75. (mcron jobs log? log-format)
  76. (if (null? jobs)
  77. '() ;no jobs to run
  78. (let ((files (job-files mcron jobs)))
  79. (list (shepherd-service
  80. (documentation "User cron jobs.")
  81. (provision '(mcron))
  82. (modules `((srfi srfi-1)
  83. (srfi srfi-26)
  84. (ice-9 popen) ;for the 'schedule' action
  85. (ice-9 rdelim)
  86. (ice-9 match)
  87. ,@%default-modules))
  88. (start #~(make-forkexec-constructor
  89. (list (string-append #$mcron "/bin/mcron")
  90. #$@(if log?
  91. #~("--log" "--log-format" #$log-format)
  92. #~())
  93. #$@files)
  94. #:log-file (string-append
  95. (or (getenv "XDG_LOG_HOME")
  96. (format #f "~a/.local/var/log"
  97. (getenv "HOME")))
  98. "/mcron.log")))
  99. (stop #~(make-kill-destructor))
  100. (actions
  101. (list (shepherd-schedule-action mcron files)))))))))
  102. (define home-mcron-profile (compose list home-mcron-configuration-mcron))
  103. (define (home-mcron-extend config jobs)
  104. (home-mcron-configuration
  105. (inherit config)
  106. (jobs (append (home-mcron-configuration-jobs config)
  107. jobs))))
  108. (define home-mcron-service-type
  109. (service-type (name 'home-mcron)
  110. (extensions
  111. (list (service-extension
  112. home-shepherd-service-type
  113. home-mcron-shepherd-services)
  114. (service-extension
  115. home-profile-service-type
  116. home-mcron-profile)))
  117. (compose concatenate)
  118. (extend home-mcron-extend)
  119. (default-value (home-mcron-configuration))
  120. (description
  121. "Install and configure the GNU mcron cron job manager.")))
  122. ;;;
  123. ;;; Generate documentation.
  124. ;;;
  125. (define (generate-doc)
  126. (configuration->documentation 'home-mcron-configuration))
  127. ;;; mcron.scm ends here