Skip to content

Commit 42a6cda

Browse files
authored
Merge pull request #1 from factorhouse/update-opts
Update OPTS
2 parents 893be0e + 244520b commit 42a6cda

File tree

8 files changed

+178
-172
lines changed

8 files changed

+178
-172
lines changed

README.md

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
# Summary
77

8-
[Cronut-Javax](https://github.com/factorhouse/cronut-javax) provides a data-first [Clojure](https://clojure.org/) wrapper
9-
for [Quartz Scheduler](https://github.com/quartz-scheduler/quartz) version `2.4.0`, compatible
8+
[Cronut-Javax](https://github.com/factorhouse/cronut-javax) provides a data-first [Clojure](https://clojure.org/)
9+
wrapper for [Quartz Scheduler](https://github.com/quartz-scheduler/quartz) version `2.4.0`, compatible
1010
with [Javax](https://jakarta.ee/blogs/javax-jakartaee-namespace-ecosystem-progress/).
1111

1212
Cronut supports **in-memory** scheduling of jobs within a single JVM. JDBC and distributed jobstore are not supported.
@@ -15,7 +15,7 @@ Cronut supports **in-memory** scheduling of jobs within a single JVM. JDBC and d
1515

1616
| Project | Desription | Clojars Project |
1717
|---------------------------------------------------------------------|---------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------|
18-
| [cronut](https://github.com/factorhouse/cronut) | Cronut with [Jakarta](https://en.wikipedia.org/wiki/Jakarta_EE) support | [![Clojars Project](https://img.shields.io/clojars/v/io.factorhouse/cronut.svg)](https://clojars.org/io.factorhouse/cronut) |
18+
| [cronut](https://github.com/factorhouse/cronut) | Cronut with [Jakarta](https://en.wikipedia.org/wiki/Jakarta_EE) support | [![Clojars Project](https://img.shields.io/clojars/v/io.factorhouse/cronut.svg)](https://clojars.org/io.factorhouse/cronut) |
1919
| [cronut-integrant](https://github.com/factorhouse/cronut-integrant) | [Integrant](https://github.com/weavejester/integrant) bindings for Cronut | [![Clojars Project](https://img.shields.io/clojars/v/io.factorhouse/cronut-integrant.svg)](https://clojars.org/io.factorhouse/cronut-integrant) |
2020

2121
# Contents
@@ -27,9 +27,9 @@ Cronut supports **in-memory** scheduling of jobs within a single JVM. JDBC and d
2727
* [Jobs](#jobs)
2828
+ [Job example](#job-example)
2929
* [Triggers](#triggers)
30-
- [`cronut.trigger/cron`: Simple Cron Scheduling](#cronuttriggercron-simple-cron-scheduling)
31-
- [`cronut.trigger/interval`: Simple Interval Scheduling](#cronuttriggerinterval-simple-interval-scheduling)
32-
- [`cronut.trigger/builder`: Full trigger definition](#cronuttriggerbuilder-full-trigger-definition)
30+
- [`cronut.trigger/cron`: Simple Cron Scheduling](#cronuttriggercron-simple-cron-scheduling)
31+
- [`cronut.trigger/interval`: Simple Interval Scheduling](#cronuttriggerinterval-simple-interval-scheduling)
32+
- [`cronut.trigger/builder`: Full trigger definition](#cronuttriggerbuilder-full-trigger-definition)
3333
* [Concurrent execution](#concurrent-execution)
3434
+ [Global concurrent execution](#global-concurrent-execution)
3535
+ [Job-specific concurrent execution](#job-specific-concurrent-execution)
@@ -47,12 +47,12 @@ Cronut provides access to the Quartz Scheduler, exposed via the `cronut/schedule
4747

4848
Create a scheduler with the following configuration:
4949

50-
1. `:concurrent-execution-disallowed?`: (optional, default false) - run all jobs with @DisableConcurrentExecution
51-
2. `:update-check?`: (optional, default false) - check for Quartz updates on system startup.
50+
1. `:concurrent-execution-disallowed?`: run all jobs with `@DisableConcurrentExecution`
51+
2. `:update-check?`: check for Quartz updates on system startup.
5252

5353
````clojure
54-
(cronut/scheduler {:concurrent-execution-disallowed? true
55-
:update-check? false})
54+
(cronut/scheduler {:concurrent-execution-disallowed? true ;; default false
55+
:update-check? false}) ;; default false
5656
````
5757

5858
### Scheduler lifecycle
@@ -84,12 +84,8 @@ To schedule jobs, you can
8484

8585
Each cronut job must implement the `org.quartz.Job` interface.
8686

87-
The expectation being that every job will reify that interface either directly via `reify` or by returning a `defrecord`
88-
that implements the interface.
89-
90-
Cronut supports further Quartz configuration of jobs (identity, description, recovery, and durability) by expecting
91-
those values to be assoc'd onto your job. You do not have to set them (in fact in most cases you can likely ignore
92-
them), however if you do want that control you will likely use the `defrecord` approach as opposed to `reify`.
87+
Cronut supports further Quartz configuration of jobs (identity, description, recovery, and durability) by passing an
88+
`opts` map when scheduling your job.
9389

9490
Concurrent execution can be controlled on a per-job bases with the `disallow-concurrent-execution?` flag.
9591

@@ -104,22 +100,31 @@ Concurrent execution can be controlled on a per-job bases with the `disallow-con
104100

105101
(let [scheduler (cronut/scheduler {:concurrent-execution-disallowed? true
106102
:update-check? false})
107-
defrecord-job (map->TestDefrecordJobImpl {:identity ["name1" "group2"]
108-
:description "test job"
109-
:recover? true
110-
:durable? false})
103+
defrecord-job (map->TestDefrecordJobImpl {})
111104
reify-job (reify Job
112105
(execute [_this _job-context]
113106
(let [rand-id (str (UUID/randomUUID))]
114107
(log/info rand-id "Reified Impl"))))]
115108

116-
(cronut/schedule-job scheduler (trigger/interval 1000) defrecord-job)
109+
(cronut/schedule-job scheduler
110+
(trigger/interval 1000)
111+
defrecord-job
112+
{:name "name1"
113+
:group "group1"
114+
:description "test job 1"
115+
:recover? true
116+
:durable? false})
117117

118118
(cronut/schedule-job scheduler
119119
(trigger/builder {:type :cron
120120
:cron "*/5 * * * * ?"
121121
:misfire :do-nothing})
122-
reify-job))
122+
reify-job
123+
{:name "name2"
124+
:group "group1"
125+
:description "test job 2"
126+
:recover? false
127+
:durable? true}))
123128
````
124129

125130
## Triggers
@@ -153,20 +158,22 @@ The `cronut.trigger/builder` function supports the full set of Quartz configurat
153158

154159
````clojure
155160
;; interval
156-
(cronut.trigger/builder {:type :simple
161+
(cronut.trigger/builder {:name "trigger1"
162+
:group "group3"
163+
:type :simple
157164
:interval 3000
158165
:repeat :forever
159-
:identity ["trigger-two" "test"]
160166
:description "sample simple trigger"
161167
:start #inst "2019-01-01T00:00:00.000-00:00"
162168
:end #inst "2019-02-01T00:00:00.000-00:00"
163169
:misfire :ignore
164170
:priority 5})
165171

166172
;;cron
167-
(cronut.trigger/builder {:type :cron
173+
(cronut.trigger/builder {:name "trigger2"
174+
:group "group3"
175+
:type :cron
168176
:cron "*/6 * * * * ?"
169-
:identity ["trigger-five" "test"]
170177
:description "sample cron trigger"
171178
:start #inst "2018-01-01T00:00:00.000-00:00"
172179
:end #inst "2029-02-01T00:00:00.000-00:00"
@@ -207,7 +214,7 @@ See: integration test source: [test/cronut/integration-test.clj](test/cronut/int
207214
(:import (java.util UUID)
208215
(org.quartz Job)))
209216

210-
(defrecord TestDefrecordJobImpl [identity description recover? durable? test-dep disallowConcurrentExecution?]
217+
(defrecord TestDefrecordJobImpl []
211218
Job
212219
(execute [this _job-context]
213220
(log/info "Defrecord Impl:" this)))
@@ -231,10 +238,12 @@ See: integration test source: [test/cronut/integration-test.clj](test/cronut/int
231238
(log/info "scheduling defrecord job on 1s interval")
232239
(cronut/schedule-job scheduler
233240
(trigger/interval 1000)
234-
(map->TestDefrecordJobImpl {:identity ["name1" "group2"]
235-
:description "test job"
236-
:recover? true
237-
:durable? false}))
241+
(map->TestDefrecordJobImpl {})
242+
{:name "name1"
243+
:group "group2"
244+
:description "test job"
245+
:recover? true
246+
:durable? false})
238247

239248
;; demonstrate scheduler can start with jobs, and jobs can start after scheduler
240249
(cronut/start scheduler)
@@ -247,7 +256,12 @@ See: integration test source: [test/cronut/integration-test.clj](test/cronut/int
247256
(trigger/builder {:type :cron
248257
:cron "*/5 * * * * ?"
249258
:misfire :do-nothing})
250-
reify-job)
259+
reify-job
260+
{:name "name2"
261+
:group "group2"
262+
:description "test job 2"
263+
:recover? false
264+
:durable? true})
251265

252266
(async/<!! (async/timeout 15000))
253267

src/cronut.clj

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
(:require [clojure.tools.logging :as log]
44
[cronut.job :as job]
55
[cronut.trigger :as trigger])
6-
(:import (org.quartz JobDetail JobKey Scheduler Trigger TriggerBuilder)
6+
(:import (org.quartz JobDetail Scheduler Trigger TriggerBuilder)
77
(org.quartz.impl StdSchedulerFactory)))
88

99
(defn scheduler
1010
"Create a new Quartz scheduler:
1111
:concurrent-execution-disallowed? - run all jobs with @DisableConcurrentExecution (default false)
1212
:update-check? - check for Quartz updates on system startup (default: false)"
13-
[{:keys [update-check? concurrent-execution-disallowed?]}]
13+
^Scheduler [{:keys [update-check? concurrent-execution-disallowed?]}]
1414
(log/infof "initializing scheduler")
1515
(when-not update-check?
1616
(System/setProperty "org.terracotta.quartz.skipUpdateCheck" "true")
@@ -29,31 +29,29 @@
2929
[^Scheduler scheduler]
3030
(= "true" (get (.getContext scheduler) "concurrentExecutionDisallowed?")))
3131

32-
(defn get-detail
33-
"Get the job detail for a key"
34-
[^Scheduler scheduler ^JobKey key]
35-
(.getJobDetail scheduler key))
36-
3732
(defn schedule-job
38-
^Trigger [^Scheduler scheduler ^TriggerBuilder trigger job]
39-
(let [detail ^JobDetail (job/detail job (concurrent-execution-disallowed? scheduler))]
40-
(if-let [^JobDetail previously-scheduled (get-detail scheduler (.getKey detail))]
41-
(let [built (.build (.forJob trigger previously-scheduled))]
42-
(log/info "scheduling new trigger for existing job" built previously-scheduled)
43-
(.scheduleJob scheduler built)
44-
built)
45-
(let [built (.build trigger)]
46-
(log/info "scheduling new job" built detail)
47-
(.scheduleJob scheduler detail built)
48-
built))))
33+
(^Trigger [^Scheduler scheduler ^TriggerBuilder trigger job]
34+
(schedule-job scheduler trigger job nil))
35+
(^Trigger [^Scheduler scheduler ^TriggerBuilder trigger job opts]
36+
(let [cce-disallowed? (concurrent-execution-disallowed? scheduler)
37+
detail (job/detail job (update opts :disallow-concurrent-execution? #(or cce-disallowed? %1)))]
38+
(if-let [^JobDetail previously-scheduled (.getJobDetail scheduler (.getKey detail))]
39+
(let [built (.build (.forJob trigger previously-scheduled))]
40+
(log/info "scheduling new trigger for existing job" (str (.getKey previously-scheduled)) opts)
41+
(.scheduleJob scheduler built)
42+
built)
43+
(let [built (.build trigger)]
44+
(log/info "scheduling new job" (str (.getKey detail)) opts)
45+
(.scheduleJob scheduler detail built)
46+
built)))))
4947

5048
(defn schedule-jobs
51-
[^Scheduler scheduler jobs]
52-
(log/infof "scheduling [%s] jobs" (count jobs))
53-
(loop [schedule jobs
49+
[^Scheduler scheduler schedule]
50+
(log/infof "scheduling [%s] jobs" (count schedule))
51+
(loop [schedule schedule
5452
triggers []]
55-
(if-let [{:keys [^TriggerBuilder trigger job]} (first schedule)]
56-
(recur (rest schedule) (conj triggers (schedule-job scheduler trigger job)))
53+
(if-let [{:keys [^TriggerBuilder trigger job opts]} (first schedule)]
54+
(recur (rest schedule) (conj triggers (schedule-job scheduler trigger job opts)))
5755
triggers)))
5856

5957
(defn pause-job

src/cronut/job.clj

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns cronut.job
22
(:refer-clojure :exclude [key])
3-
(:import (org.quartz DisallowConcurrentExecution Job JobBuilder JobDataMap JobExecutionException JobKey)
3+
(:import (org.quartz DisallowConcurrentExecution Job JobBuilder JobDataMap JobDetail JobExecutionException JobKey)
44
(org.quartz.spi JobFactory TriggerFiredBundle)))
55

66
(defrecord ^{DisallowConcurrentExecution true} SerialProxyJob [proxied-job]
@@ -36,13 +36,11 @@
3636
(->ProxyJob job))))))
3737

3838
(defn detail
39-
[job concurrent-execution-disallowed?]
40-
(let [{:keys [identity description recover? durable? disallow-concurrent-execution?]} job]
41-
(.build (cond-> (-> (JobBuilder/newJob (if (or concurrent-execution-disallowed? ;; global concurrency disallowed flag
42-
disallow-concurrent-execution?) ;; job specific concurrency disallowed flag
43-
SerialProxyJob ProxyJob))
39+
^JobDetail [job opts]
40+
(let [{:keys [name group description recover? durable? disallow-concurrent-execution?]} opts]
41+
(.build (cond-> (-> (JobBuilder/newJob (if disallow-concurrent-execution? SerialProxyJob ProxyJob))
4442
(.setJobData (JobDataMap. {"job-impl" job})))
45-
(seq identity) (.withIdentity (first identity) (second identity))
43+
name (.withIdentity name group)
4644
description (.withDescription description)
4745
(boolean? recover?) (.requestRecovery recover?)
4846
(boolean? durable?) (.storeDurably durable?)))))

src/cronut/trigger.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
(defn base-builder
77
"Provide a base trigger-builder from configuration"
8-
[{:keys [identity description start end priority]}]
8+
[{:keys [name group description start end priority]}]
99
(cond-> (TriggerBuilder/newTrigger)
10-
(seq identity) (.withIdentity (first identity) (second identity))
10+
name (.withIdentity name group)
1111
description (.withDescription description)
1212
start (.startAt start)
1313
(nil? start) (.startNow)

test/cronut/integration_test.clj

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
(:import (java.util UUID)
77
(org.quartz Job)))
88

9-
(defrecord TestDefrecordJobImpl [identity description recover? durable? test-dep disallowConcurrentExecution?]
9+
(defrecord TestDefrecordJobImpl []
1010
Job
1111
(execute [this _job-context]
1212
(log/info "Defrecord Impl:" this)))
@@ -30,10 +30,12 @@
3030
(log/info "scheduling defrecord job on 1s interval")
3131
(cronut/schedule-job scheduler
3232
(trigger/interval 1000)
33-
(map->TestDefrecordJobImpl {:identity ["name1" "group2"]
34-
:description "test job"
35-
:recover? true
36-
:durable? false}))
33+
(map->TestDefrecordJobImpl {})
34+
{:name "name1"
35+
:group "group2"
36+
:description "test job"
37+
:recover? true
38+
:durable? false})
3739

3840
;; demonstrate scheduler can start with jobs, and jobs can start after scheduler
3941
(cronut/start scheduler)
@@ -46,7 +48,12 @@
4648
(trigger/builder {:type :cron
4749
:cron "*/5 * * * * ?"
4850
:misfire :do-nothing})
49-
reify-job)
51+
reify-job
52+
{:name "name2"
53+
:group "group2"
54+
:description "test job 2"
55+
:recover? false
56+
:durable? true})
5057

5158
(async/<!! (async/timeout 15000))
5259

0 commit comments

Comments
 (0)