TL;DR
how to reduce below repeated code, like create two job / trigger from job-inventory
, instead of repeat twice and create terms
;; deps in project.clj
;; [clojurewerkz/quartzite "2.1.0"]
(ns hello.scheduler
(: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 :as cron])
(:use clojure.tools.logging)
(:gen-class))
(def job-inventory
[{:name "add" :task '(+ 1 1) :cron "0/5 * * ? * *"}
{:name "multiply" :task '(* 4 5) :cron "0/3 * * ? * *"}])
(defjob add [ctx] (info "add called, return" (+ 1 1)))
(defjob multiply [ctx] (info "multiply called, return" (* 2 3)))
(defn auto
[]
(let [s (-> (qs/initialize) qs/start)
_ (qs/clear! s)
job (j/build
(j/of-type add)
(j/with-identity (j/key "job.add")))
trigger (t/build
(t/with-identity (t/key "trigger.add"))
(t/start-now)
(t/with-schedule (cron/schedule
(cron/cron-schedule "0/5 * * ? * *"))))
_ (qs/schedule s job trigger)
job (j/build
(j/of-type multiply)
(j/with-identity (j/key "job.multiply")))
trigger (t/build
(t/with-identity (t/key "trigger.multiply"))
(t/start-now)
(t/with-schedule (cron/schedule
(cron/cron-schedule "0/3 * * ? * *"))))
_ (qs/schedule s job trigger)
]
))
similar to what's described in http://clojurequartz.info/articles/getting_started.html , I have block of code to create jobs and hooks them for execution
quesetion is, when I get more and more of them, I wonder if I could have a better way of manage them, like create / spawn from that job-inventory
, instead of actually creating varaibles like add
or multiply
so, asking for one more layers of looping are there ways to utilize function programming, and avoid create new names ( in traditional language says python qt, if I had a sets of button, I could just smash into a giant dictionary, and loop over to create / disable, instead actually create each name as a top level varible )
I tried macro but it says unable to resolve class add, so guess I used it wrong