2

I am using quartzite, and I need to schedule a job every tuesday morning (say at 10 AM).

I have the code for the job, but I'm not sure how to schedule it.

Edit : Here is what I have so far, but it doesn't work (see comment).

(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)))

Edit2 :

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"

My case is very similar from the case highlighted in the documentation, yet I can't find how to do it with the daily schedules.

walen
  • 7,103
  • 2
  • 37
  • 58
nha
  • 17,623
  • 13
  • 87
  • 133
  • 1
    Have you tried cron triggers? http://quartz-scheduler.org/generated/2.2.1/html/qs-all/index.html#page/Quartz_Scheduler_Documentation_Set%2Fco-trg_crontriggers.html%23. Something `0 0 10 ? * TUE` should describe every tuesday 10am. quartzite doc link - http://clojurequartz.info/articles/triggers.html#using_cron_expression_schedules – Amith George Sep 15 '15 at 19:07
  • @AmithGeorge I am trying that. It looks like it works. I am not sure how to test it though, and I get a `Java.util.Date` instance that seems 2 hours off (I am GMT+2, maybe that's why ?). Thanks for commenting. – nha Sep 15 '15 at 19:32
  • @AmithGeorge also, that could be an answer. As said in my previous comment, I would also like to be able to check that it works. – nha Sep 15 '15 at 20:04
  • 1
    Cron works on local time. The clojure code for scheduling the job will run on the same machine that will execute the job. As long as it is Tuesday 10am on that machine, the job will be triggered. If you need it to run on Tuesday 10am UTC, your options are to have the machine be in UTC timezone, or manually calculate the proper local time, ie Tuesday 12pm prior to scheduling it. ... I hope that explains it, cuz I haven't really understood what the issue is. – Amith George Sep 16 '15 at 21:16
  • @AmithGeorge if you want to put that as an answer, I would accept it. – nha Sep 16 '15 at 22:33
  • @nha would be great if someone documented how to do this without learning crontab syntax. – Petrus Theron May 13 '16 at 09:38

1 Answers1

0

One viable option is to use cron triggers[1][2][3]. Sample code from the documentation [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 schedules use the local timezone of the machine on which it is scheduled. Something like 0 0 10 ? * TUE describes every Tuesday at 10am as per the machines local time. If the time needs to be some specific UTC time, then the preferable option would be to pre-calculate the corresponding local time and use that in the cron trigger description. Or just configure the machine to use UTC timezone.

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

[2] - [DEAD] 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] - Possible replacement for now dead link no 2 - http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-06.html

Amith George
  • 5,806
  • 2
  • 35
  • 53