Skip to content

Commit f5bb851

Browse files
committed
add cron scaffolding
1 parent c42a78a commit f5bb851

File tree

11 files changed

+98
-15
lines changed

11 files changed

+98
-15
lines changed

clojure/wheel/.joker

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
{:known-macros [mount.core/defstate
2-
toucan.models/defmodel]}
2+
toucan.models/defmodel
3+
clojurewerkz.quartzite.jobs/defjob]}

clojure/wheel/project.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
[cheshire "5.9.0"]
1414
[toucan "1.14.0"]
1515
[clj-http "3.10.0"]
16-
[com.ibm.mq/com.ibm.mq.allclient "9.1.0.0"]]
16+
[com.ibm.mq/com.ibm.mq.allclient "9.1.0.0"]
17+
[clojurewerkz/quartzite "2.1.0"]]
1718
:main ^:skip-aot wheel.core
1819
:target-path "target/%s"
1920
:profiles {:uberjar {:aot :all}

clojure/wheel/resources/config.edn

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
:qmgr #or [#env "WHEEL_APP_MQ_QMGR" "QM1"]
1212
:user-id #or [#env "WHEEL_APP_MQ_USER_ID" "app"]
1313
:password #or [#env "WHEEL_APP_MQ_PASSWORD" "test123"]}}
14-
:settings {:oms {:ranging-queue-name "DEV.QUEUE.1"}
15-
:channels {"UA" {:channel-name :tata-cliq
16-
:base-url "http://localhost:3000"
17-
:bearer-token "top-secret!"}
18-
"UB" {:channel-name :tata-cliq
19-
:base-url "http://localhost:3000"
20-
:bearer-token "top-secret!"}}}}
14+
:settings {:oms {:ranging-queue-name "DEV.QUEUE.1"}
15+
:channels {"UA" {:channel-name :tata-cliq
16+
:base-url "http://localhost:3000"
17+
:bearer-token "top-secret!"}
18+
"UB" {:channel-name :tata-cliq
19+
:base-url "http://localhost:3000"
20+
:bearer-token "top-secret!"}}
21+
:cron-jobs [{:type :allocate-order
22+
:channel-id "UA"
23+
:expression "4 0 * ? * * *"}]}}

clojure/wheel/src/wheel/infra/config.clj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
(defn oms-settings []
2222
(get-in root [:settings :oms]))
2323

24-
(defn get-channel-cofig [channel-id]
24+
(defn get-channel-config [channel-id]
2525
(get-in root [:settings :channels channel-id]))
2626

27+
(defn get-all-cron-jobs []
28+
(get-in root [:settings :cron-jobs]))
29+
2730
(comment
2831
(mount/start)
29-
(get-channel-cofig "UA")
3032
(mount/stop))

clojure/wheel/src/wheel/infra/core.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[clojure.spec.alpha :as s]
55
[wheel.infra.config :as config]
66
[wheel.infra.database :as db]
7+
[wheel.infra.cron.core :as cron]
78
[wheel.infra.ibmmq :as ibmmq]
89
[wheel.infra.oms :as oms]
910
[wheel.middleware.ranging :as ranging]
@@ -15,7 +16,8 @@
1516
([check-asserts]
1617
(log/init)
1718
(s/check-asserts check-asserts)
18-
(mount/start)))
19+
(mount/start)
20+
(cron/init)))
1921

2022
(defn stop-app []
2123
(mount/stop))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(ns wheel.infra.cron.core
2+
(:require [clojurewerkz.quartzite.scheduler :as qs]
3+
[wheel.infra.cron.job.allocate-order]
4+
[wheel.infra.cron.job.core :as job]
5+
[wheel.infra.config :as config]
6+
[mount.core :as mount]))
7+
8+
(mount/defstate scheduler
9+
:start (qs/start (qs/initialize))
10+
:stop (qs/shutdown scheduler))
11+
12+
(defn init []
13+
(for [cron-job-config (config/get-all-cron-jobs)]
14+
(job/schedule scheduler cron-job-config)))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(ns wheel.infra.cron.job.allocate-order
2+
(:require [wheel.infra.cron.job.core :as job]
3+
[clojurewerkz.quartzite.jobs :as qj]
4+
[wheel.marketplace.channel :as channel]))
5+
6+
(qj/defjob AllocateOrderJob [ctx]
7+
(job/handle channel/allocate-order ctx))
8+
9+
(defmethod job/jobtype :allocate-order [_]
10+
AllocateOrderJob)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
(ns wheel.infra.cron.job.core
2+
(:require [clojurewerkz.quartzite.jobs :as qj]
3+
[clojurewerkz.quartzite.conversion :as qc]
4+
[clojurewerkz.quartzite.scheduler :as qs]
5+
[clojurewerkz.quartzite.schedule.cron :as qsc]
6+
[clojurewerkz.quartzite.triggers :as qt]
7+
[wheel.infra.config :as config]))
8+
9+
(defmulti jobtype :type)
10+
11+
(defn- identifier [{:keys [channel-id type]}]
12+
(keyword channel-id (name type)))
13+
14+
(defn- create-job [channel-config cron-job-config]
15+
(qj/build
16+
(qj/of-type (jobtype cron-job-config))
17+
(qj/using-job-data {:channel-config channel-config
18+
:cron-job-config cron-job-config})
19+
(qj/with-identity (qj/key (identifier cron-job-config)))))
20+
21+
(defn- create-trigger [{:keys [expression]
22+
:as cron-job-config}]
23+
(qt/build
24+
(qt/with-identity (qt/key (identifier cron-job-config)))
25+
(qt/start-now)
26+
(qt/with-schedule (qsc/schedule
27+
(qsc/cron-schedule expression)))))
28+
29+
(defn create [{:keys [channel-id]
30+
:as cron-job-config}]
31+
(when-let [channel-config (config/get-channel-config channel-id)]
32+
(create-job channel-config cron-job-config)))
33+
34+
(defn handle [channel-fn ctx]
35+
(channel-fn (qc/from-job-data ctx) nil))
36+
37+
(defn schedule [scheduler cron-job-config]
38+
(when-let [job (create cron-job-config)]
39+
(qs/schedule scheduler job (create-trigger cron-job-config))))
40+
41+
(comment
42+
(key {:channel-id "UA"
43+
:type :allocate-order}))

clojure/wheel/src/wheel/marketplace/channel.clj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
(:require [clojure.spec.alpha :as s]))
33

44
(s/def ::id (complement clojure.string/blank?))
5-
(s/def ::name #{:tata-cliq :amazon :flipkart})
5+
(s/def ::name #{:tata-cliq :amazon :flipkart})
6+
7+
(defmulti allocate-order (fn [channel-id channel-config]
8+
(:name channel-config)))

clojure/wheel/src/wheel/marketplace/tata_cliq/core.clj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns wheel.marketplace.tata-cliq.core
22
(:require [wheel.marketplace.tata-cliq.api :as tata-cliq]
3+
[wheel.marketplace.channel :as channel]
34
[wheel.middleware.ranging :as ranging]
45
[wheel.oms.message :as oms-message]
56
[wheel.middleware.event :as event]
@@ -20,4 +21,7 @@
2021
(map #(set/rename-keys % {:id :sku}) items))
2122
(event/ranging-succeeded id channel-id channel-name items)
2223
(catch Throwable ex
23-
(event/processing-failed ex id :ranging channel-id channel-name))))
24+
(event/processing-failed ex id :ranging channel-id channel-name))))
25+
26+
(defmethod channel/allocate-order :tata-cliq [x _]
27+
(prn "~~>" x))

0 commit comments

Comments
 (0)