Clojure Web应用程序中的Quartzite作业调度

时间:2014-07-17 14:58:33

标签: web-applications clojure scheduled-tasks

我需要在给定时间每天定期在Clojure Web应用程序中执行一个函数。我尝试了Quartzite库,但它并不顺利。我将Quartzite代码放入init函数中,该函数在应用程序部署后调用,但调度的作业未执行。当我尝试不同的调度(即每200毫秒)时,作业在开始时执行了几次然后停止。

我可能做了一些明显错误的事情,但我看不到它。有人可以帮我这个吗?

我正在使用Luminus框架。代码如下:

(j/defjob import-logs [ctx]
  (print "something")
)

(defn init
  "init will be called once when
   app is deployed as a servlet on
   an app server such as Tomcat
   put any initialization code here"
  []

  (qs/initialize)
  (qs/start)
  (let [job (j/build
              (j/of-type import-logs)
              (j/with-identity (j/key "jobs.import.1")))
        trigger (t/build
                 (t/with-identity (t/key "triggers.1"))
                 (t/start-now)
                 (t/with-schedule (schedule
                                     (with-repeat-count 10)
                                     (with-interval-in-milliseconds 1000)
                                   )))]
    (qs/schedule job trigger)))

2 个答案:

答案 0 :(得分:0)

如果您使用Luminus作为基础,则可以使用immutant's scheduling

调度集成在场景后面,这是使用它的最快方式:

 (ns your.ns
    (:require [immutant.jobs :as jobs]))

 (jobs/schedule :my-at-job-name  
           #(println "I fire 4 times with a 10ms delay between each, starting in 500ms.")
           :in 500   ; ms
           :every 10 ; ms
           :repeat 3
           :singleton false)

答案 1 :(得分:0)

您没有正确初始化它。您必须创建一个调度程序实例并将其传递给qs/schedule函数

(defn init
  "init will be called once when
   app is deployed as a servlet on
   an app server such as Tomcat
   put any initialization code here"
  []
  (let [s (qs/start (qs/initialize))
        job (j/build
             (j/of-type import-logs)
             (j/with-identity (j/key "jobs.import.1")))
        trigger (t/build
                 (t/with-identity (t/key "triggers.1"))
                 (t/start-now)
                 (t/with-schedule (schedule
                                   (with-repeat-count 10)
                                   (with-interval-in-milliseconds 1000)
                                   )))]
    (qs/schedule s job trigger)))