123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- (define-module (gnu services mcron)
- #:use-module (gnu services)
- #:use-module (gnu services shepherd)
- #:use-module (gnu packages guile-xyz)
- #:use-module (guix deprecation)
- #:use-module (guix records)
- #:use-module (guix gexp)
- #:use-module (srfi srfi-1)
- #:use-module (ice-9 match)
- #:use-module (ice-9 vlist)
- #:export (mcron-configuration
- mcron-configuration?
- mcron-configuration-mcron
- mcron-configuration-jobs
- mcron-service-type))
- (define-record-type* <mcron-configuration> mcron-configuration
- make-mcron-configuration
- mcron-configuration?
- (mcron mcron-configuration-mcron
- (default mcron))
- (jobs mcron-configuration-jobs
- (default '())))
- (define (job-files mcron jobs)
- "Return a list of file-like object for JOBS, a list of gexps."
- (define (validated-file job)
-
-
- (computed-file "mcron-job"
- (with-imported-modules '((guix build utils))
- #~(begin
- (use-modules (guix build utils))
- (call-with-output-file "prologue"
- (lambda (port)
-
-
-
- (write '(set! getpw
- (const (getpwuid (getuid))))
- port)))
- (call-with-output-file "job"
- (lambda (port)
- (write '#$job port)))
- (invoke #+(file-append mcron "/bin/mcron")
- "--schedule=20" "prologue" "job")
- (copy-file "job" #$output)))
- #:options '(#:env-vars (("COLUMNS" . "150")))))
- (map validated-file jobs))
- (define (shepherd-schedule-action mcron files)
- "Return a Shepherd action that runs MCRON with '--schedule' for the given
- files."
- (shepherd-action
- (name 'schedule)
- (documentation
- "Display jobs that are going to be scheduled.")
- (procedure
- #~(lambda* (_ #:optional (n "5"))
-
- (setenv "GUILE_AUTO_COMPILE" "0")
-
-
-
- (let ((pipe (open-pipe* OPEN_READ
- #$(file-append mcron "/bin/mcron")
- (string-append "--schedule=" n)
- #$@files)))
- (let loop ()
- (match (read-line pipe 'concat)
- ((? eof-object?)
- (catch 'system-error
- (lambda ()
- (zero? (close-pipe pipe)))
- (lambda args
-
-
-
-
- (or (= ECHILD (system-error-errno args))
- (apply throw args)))))
- (line
- (display line)
- (loop)))))))))
- (define mcron-shepherd-services
- (match-lambda
- (($ <mcron-configuration> mcron ())
- '())
- (($ <mcron-configuration> mcron jobs)
- (let ((files (job-files mcron jobs)))
- (list (shepherd-service
- (provision '(mcron))
- (requirement '(user-processes))
- (modules `((srfi srfi-1)
- (srfi srfi-26)
- (ice-9 popen)
- (ice-9 rdelim)
- (ice-9 match)
- ,@%default-modules))
- (start #~(make-forkexec-constructor
- (list (string-append #$mcron "/bin/mcron") #$@files)
-
-
- #:environment-variables
- (cons* "GUILE_AUTO_COMPILE=0"
- "PATH=/run/current-system/profile/bin"
- (remove (cut string-prefix? "PATH=" <>)
- (environ)))
- #:log-file "/var/log/mcron.log"))
- (stop #~(make-kill-destructor))
- (actions
- (list (shepherd-schedule-action mcron files)))))))))
- (define mcron-service-type
- (service-type (name 'mcron)
- (description
- "Run the mcron job scheduling daemon.")
- (extensions
- (list (service-extension shepherd-root-service-type
- mcron-shepherd-services)
- (service-extension profile-service-type
- (compose list
- mcron-configuration-mcron))))
- (compose concatenate)
- (extend (lambda (config jobs)
- (mcron-configuration
- (inherit config)
- (jobs (append (mcron-configuration-jobs config)
- jobs)))))
- (default-value (mcron-configuration))))
|