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 }))
0 commit comments