cron like wip
This commit is contained in:
parent
b1d80ef709
commit
f81124f542
127
debug.lsp
127
debug.lsp
|
|
@ -1,17 +1,118 @@
|
|||
(include "/usr/local/var/mlisp/terminal.lsp")
|
||||
|
||||
(doc::man "string-regex-list")
|
||||
(doc::man "string-split")
|
||||
(doc::man "string-trim")
|
||||
; TODO better env formating
|
||||
; TODO time parse function
|
||||
|
||||
;; (defn concat-lists (seq1 seq2)
|
||||
;; (do (def l seq1)
|
||||
;; (dotimes i (len seq2)
|
||||
;; (set! l (push l (index seq2 i))))
|
||||
;; ))
|
||||
|
||||
;; (print (concat-lists '(1 2 3) '(4 5 6)))
|
||||
;; (1 2 3 4 5 6)
|
||||
(defn cron::sleep-to-next-minute ()
|
||||
(* 1000 (- 60 (int (date-to-str (get-universal-time) "%S")))))
|
||||
|
||||
|
||||
(defn cron::eval-time-spec (val time_spec)
|
||||
(do
|
||||
;(print "DEBUG" "cron::eval-time-spec" val time_spec)
|
||||
(def r
|
||||
(cond
|
||||
((nil? time_spec)
|
||||
#t) ; no time_spec -> it's true
|
||||
((= time_spec "*")
|
||||
#t)
|
||||
((string-regex? time_spec "^[0-9]{1,4}$")
|
||||
(= val (int time_spec)))
|
||||
((string-regex? time_spec "^[0-9]{1,2}-[0-9]{1,2}$")
|
||||
(def interval (string-split time_spec "-"))
|
||||
(and (>= val (int (first interval))) (<= val (int (second interval)))))
|
||||
(#t
|
||||
nil) ; TODO exception
|
||||
)
|
||||
)
|
||||
(print "val:" val "time_spec:" time_spec "result" r)
|
||||
r
|
||||
))
|
||||
|
||||
|
||||
(defn cron::add-job (name time_spec job)
|
||||
; TODO if already there replace it
|
||||
(set! cron::entries (push cron::entries (list name time_spec job)))
|
||||
)
|
||||
|
||||
|
||||
(defn cron::time-to-run (now time_spec)
|
||||
(do
|
||||
; second, minute, hour, day, month, year, day-of-the-week
|
||||
(def time (decode-universal-time (get-universal-time)))
|
||||
|
||||
(def time_minute (second time))
|
||||
(def time_hour (third time))
|
||||
(def time_day (fourth time))
|
||||
(def time_month (fifth time))
|
||||
(def time_year (sixth time))
|
||||
(def time_weekday (seventh time))
|
||||
|
||||
(def bool (cron::eval-time-spec time_minute (find-val-in-list time_spec "minute")))
|
||||
(set! bool (and bool (cron::eval-time-spec time_hour (find-val-in-list time_spec "hour"))))
|
||||
(set! bool (and bool (cron::eval-time-spec time_weekday (find-val-in-list time_spec "weekday"))))
|
||||
(set! bool (and bool (cron::eval-time-spec time_day (find-val-in-list time_spec "day"))))
|
||||
(set! bool (and bool (cron::eval-time-spec time_month (find-val-in-list time_spec "month"))))
|
||||
(set! bool (and bool (cron::eval-time-spec time_year (find-val-in-list time_spec "year"))))
|
||||
(print "time-to-run test" bool)
|
||||
bool
|
||||
))
|
||||
|
||||
|
||||
(defn cron::start ()
|
||||
(do
|
||||
(def wait_time (cron::sleep-to-next-minute))
|
||||
(print "cron daemon started, waiting" wait_time "miliseconds for first run")
|
||||
; (thread-sleep wait_time)
|
||||
(while (not cron::stop)
|
||||
(def now (get-universal-time))
|
||||
(for e cron::entries
|
||||
(def job_name (first e))
|
||||
(def time_spec (second e))
|
||||
|
||||
; (print "DEBUG" "checking" job_name "time_spec" time_spec)
|
||||
(if (cron::time-to-run now time_spec)
|
||||
(do
|
||||
;(print "running job:" job_name "code:" (third e))
|
||||
(thread-create (eval (third e)))
|
||||
)
|
||||
; (print "not running" job_name)
|
||||
)
|
||||
)
|
||||
|
||||
(thread-sleep (cron::sleep-to-next-minute))
|
||||
)
|
||||
(threads-join)
|
||||
))
|
||||
|
||||
|
||||
(defn cron::stop ()
|
||||
(set! cron::stop #t))
|
||||
|
||||
|
||||
(def cron::entries '()) ; must be here
|
||||
(def cron::stop nil) ; must be here
|
||||
|
||||
|
||||
|
||||
|
||||
(cron::add-job "every_min_print"
|
||||
'(("minute" "*"))
|
||||
'(print "code: every_min_print"))
|
||||
|
||||
(cron::add-job "every_min_between_00_and_05"
|
||||
'(("minute" "00-05"))
|
||||
'(print "code: every_min_between_00_and_05"))
|
||||
|
||||
(cron::add-job "13:20_working_day"
|
||||
'(("weekday" "1-5")("hour" "13")("minute" "20"))
|
||||
'(print "code: 13:20_working_day"))
|
||||
|
||||
(cron::add-job "15:20_working_day"
|
||||
'(("weekday" "1-5")("hour" "15")("minute" "20"))
|
||||
'(print "code: 15:20_working_day"))
|
||||
|
||||
|
||||
(cron::start)
|
||||
|
||||
; pridat example
|
||||
; >>> (doc::man "push")
|
||||
; (push list element) - Add an item to the end of a list
|
||||
Loading…
Reference in New Issue