Implementing a cron type scheduler in clojure

前端 未结 3 1077
臣服心动
臣服心动 2021-02-06 03:49

I\'m looking for any way for clojure that can trigger an event at a given time,

For example: I want a particular process to start at 9:30am and then I can trigger anothe

相关标签:
3条回答
  • 2021-02-06 04:05

    java >=5.0 has the ScheduledExecutorService which covers all this and more. there is a clojure project called at-at which wraps this nicely (and is part of one of my personal favorite projects Overtone)

    you will likely find this other SO question helpful Executing code at regularly timed intervals in Clojure

    from the intoduction:

     The schedule methods create tasks with various delays and return a task object that can be     
     used to cancel or check execution. The scheduleAtFixedRate and scheduleWithFixedDelay   
     methods create and execute tasks that run periodically until cancelled.
    

    Getting cron or at type services right is harder than it sounds, so perhaps using an existing implementation has some advantages over rolling your own.

    0 讨论(0)
  • 2021-02-06 04:05

    Some Clojure libraries for scheduling I have come across, in ascending order of complexity:

    • Overtone/at-at, a simple ahead-of-time scheduler
    • Monotony, a scheduler to schedule things in a way that humans find intuitive.
    • Quartzite, a powerful Clojure scheduling library built on top the Java Quartz Scheduler.
    0 讨论(0)
  • 2021-02-06 04:13

    I needed something that

    • started scheduled tasks at whole-second intervals as opposed to whole minute intervals having high system-time accuracy.
    • would spawn as many threads as needed, so that tasks started at earlier intervals could exist along side tasks started at later intervals.

    After doing a bit of source code reading for at-at, Monotony and Quartzite, I felt that they did not fit the requirements that I was after (which was really something more bare bone) so I wrote my own - cronj

    An example of the usage.

    (require '[cronj.core :as cj])
    
    (cj/schedule-task! {:id 0   :desc 0 
                  :handler #(println "job 0:" %) 
                  :tab "/5 * * * * * *"}) ;; every 5 seconds
    (cj/schedule-task! {:id 1   :desc 1 
                  :handler #(println "job 1:" %) 
                  :tab "/3 * * * * * *"}) ;; every 3 seconds
    
    (cj/start!) 
    
    ;; wait for scheduler to do its thing ......
    
    (cj/stop!)
    
    0 讨论(0)
提交回复
热议问题