mcron.scm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu home services mcron)
  20. #:use-module (gnu packages guile-xyz)
  21. #:use-module (gnu home services)
  22. #:use-module (gnu services shepherd)
  23. #:use-module (gnu home services shepherd)
  24. #:use-module (guix records)
  25. #:use-module (guix gexp)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (ice-9 match)
  28. #:export (home-mcron-configuration
  29. home-mcron-service-type))
  30. ;;; Commentary:
  31. ;;
  32. ;; Service for the GNU mcron cron job manager.
  33. ;;
  34. ;; Example configuration, the first job runs mbsync once every ten
  35. ;; minutes, the second one writes "Mcron service" to ~/mcron-file once
  36. ;; every minute.
  37. ;;
  38. ;; (service home-mcron-service-type
  39. ;; (home-mcron-configuration
  40. ;; (jobs (list #~(job '(next-minute
  41. ;; (range 0 60 10))
  42. ;; (lambda ()
  43. ;; (system* "mbsync" "--all")))
  44. ;; #~(job next-minute-from
  45. ;; (lambda ()
  46. ;; (call-with-output-file (string-append (getenv "HOME")
  47. ;; "/mcron-file")
  48. ;; (lambda (port)
  49. ;; (display "Mcron service" port)))))))))
  50. ;;
  51. ;;; Code:
  52. (define-record-type* <home-mcron-configuration> home-mcron-configuration
  53. make-home-mcron-configuration
  54. home-mcron-configuration?
  55. (package home-mcron-configuration-package ; package
  56. (default mcron))
  57. (jobs home-mcron-configuration-jobs ; list of jobs
  58. (default '())))
  59. (define job-files (@@ (gnu services mcron) job-files))
  60. (define shepherd-schedule-action
  61. (@@ (gnu services mcron) shepherd-schedule-action))
  62. (define home-mcron-shepherd-services
  63. (match-lambda
  64. (($ <home-mcron-configuration> mcron '()) ; no jobs to run
  65. '())
  66. (($ <home-mcron-configuration> mcron jobs)
  67. (let ((files (job-files mcron jobs)))
  68. (list (shepherd-service
  69. (documentation "User cron jobs.")
  70. (provision '(mcron))
  71. (modules `((srfi srfi-1)
  72. (srfi srfi-26)
  73. (ice-9 popen) ; for the 'schedule' action
  74. (ice-9 rdelim)
  75. (ice-9 match)
  76. ,@%default-modules))
  77. (start #~(make-forkexec-constructor
  78. (list #$(file-append mcron "/bin/mcron") #$@files)
  79. #:log-file (string-append
  80. (or (getenv "XDG_LOG_HOME")
  81. (format #f "~a/.local/var/log"
  82. (getenv "HOME")))
  83. "/mcron.log")))
  84. (stop #~(make-kill-destructor))
  85. (actions
  86. (list (shepherd-schedule-action mcron files)))))))))
  87. (define home-mcron-profile (compose list home-mcron-configuration-package))
  88. (define (home-mcron-extend config jobs)
  89. (home-mcron-configuration
  90. (inherit config)
  91. (jobs (append (home-mcron-configuration-jobs config)
  92. jobs))))
  93. (define home-mcron-service-type
  94. (service-type (name 'home-mcron)
  95. (extensions
  96. (list (service-extension
  97. home-shepherd-service-type
  98. home-mcron-shepherd-services)
  99. (service-extension
  100. home-profile-service-type
  101. home-mcron-profile)))
  102. (compose concatenate)
  103. (extend home-mcron-extend)
  104. (default-value (home-mcron-configuration))
  105. (description
  106. "Install and configure the GNU mcron cron job manager.")))