Clojure - 每周二安排工作

Clojure - schedule a job every Tuesday

我正在使用 quartzite,我需要在每个星期二早上安排工作(比如上午 10 点)。

我有作业代码,但不确定如何安排。

编辑 :这是我目前所做的,但它不起作用(见评论)。

(defn start-weekly-email-job []
  (let [job  (job/build
              (job/of-type AlertMail)
              (job/with-identity (job/key "jobs.weekly.1")))
        trigger (trigger/build
                 (trigger/with-identity (trigger/key "triggers.1"))
                 (trigger/start-at (time-of-day 10 00 00))
                                        ; at 10 AM, fails with exception
                                        ; IllegalArgumentException No implementation of method: :to-date of protocol: #'clojurewerkz.quartzite.conversion/DateConversion found for class: org.quartz.TimeOfDay  clojure.core/-cache-protocol-fn (core_deftype.clj:554)
                 (trigger/with-schedule (daily/schedule
                                         (daily/on-days-of-the-week #{(int 2)}))))] ;; ;; start Tuesday
    (qs/schedule s job trigger)))

编辑2 :

Using daily interval schedules Daily interval schedules make it easy to define schedules like

  • "Monday through Friday from 9 to 17"
  • "Every weekend at 3 in the morning"
  • "Every Friday at noon"
  • "Every day at 13:45"
  • "Every hour on Thursdays but not later than 15:00, up to 400 times total"

我的案例与文档中突出显示的案例非常相似,但我找不到如何使用每日时间表。

一个可行的选择是使用 cron 触发器[1][2][3]。文档 [1] -

中的示例代码
(ns my.service
  (:require [clojurewerkz.quartzite.scheduler :as qs]
            [clojurewerkz.quartzite.triggers :as t]
            [clojurewerkz.quartzite.jobs :as j]
        [clojurewerkz.quartzite.jobs :refer [defjob]]
            [clojurewerkz.quartzite.schedule.cron :refer [schedule cron-schedule]]))

(defjob NoOpJob
  [ctx]
  (comment "Does nothing"))

(defn -main
  [& m]
  (let [s   (-> (qs/initialize) qs/start)
       job  (j/build
              (j/of-type NoOpJob)
              (j/with-identity (j/key "jobs.noop.1")))
        trigger (t/build
                  (t/with-identity (t/key "triggers.1"))
                  (t/start-now)
                  (t/with-schedule (schedule
                                     (cron-schedule "0 0 15 ? * 5"))))]
  (qs/schedule s job trigger)))

Cron 计划使用计划它的机器的本地时区。 0 0 10 ? * TUE 之类的内容根据机器当地时间描述了 every Tuesday at 10am。如果时间需要某个特定的 UTC 时间,那么更好的选择是预先计算相应的本地时间并在 cron 触发器描述中使用它。或者只是将机器配置为使用 UTC 时区。

[1] - http://clojurequartz.info/articles/triggers.html#using_cron_expression_schedules

[2] - [死亡] http://quartz-scheduler.org/generated/2.2.1/html/qs-all/index.html#page/Quartz_Scheduler_Documentation_Set%2Fco-trg_crontriggers.html%23

[3] - http://linux.die.net/man/5/crontab

[4] - 可能替代现已死亡的 link 否 2 - http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-06.html