Reputation: 61
I need to execute a function in Clojure web application periodically every day at given time. I tried the Quartzite library but it did't go well. I put the Quartzite code into an init function, that is called after application deployment but the scheduled job did not execute. When I tried different scheduling (ie. every 200 milliseconds) the job executed few times at the beginning and then it stops.
I am probably doing something obviously wrong but I can't see it. Can someone help me with this?
I am using the Luminus framework. The code is as follows:
(j/defjob import-logs [ctx]
(print "something")
)
(defn init
"init will be called once when
app is deployed as a servlet on
an app server such as Tomcat
put any initialization code here"
[]
(qs/initialize)
(qs/start)
(let [job (j/build
(j/of-type import-logs)
(j/with-identity (j/key "jobs.import.1")))
trigger (t/build
(t/with-identity (t/key "triggers.1"))
(t/start-now)
(t/with-schedule (schedule
(with-repeat-count 10)
(with-interval-in-milliseconds 1000)
)))]
(qs/schedule job trigger)))
Upvotes: 1
Views: 731
Reputation: 602
You did not initialize it correctly. You have to create an instance of scheduler and pass it to qs/schedule
function
(defn init
"init will be called once when
app is deployed as a servlet on
an app server such as Tomcat
put any initialization code here"
[]
(let [s (qs/start (qs/initialize))
job (j/build
(j/of-type import-logs)
(j/with-identity (j/key "jobs.import.1")))
trigger (t/build
(t/with-identity (t/key "triggers.1"))
(t/start-now)
(t/with-schedule (schedule
(with-repeat-count 10)
(with-interval-in-milliseconds 1000)
)))]
(qs/schedule s job trigger)))
Upvotes: 1
Reputation: 14187
If you are using Luminus as a base, you could use immutant's scheduling.
Scheduling is integrated behind the scene, and here's the quickest way to use it:
(ns your.ns
(:require [immutant.jobs :as jobs]))
(jobs/schedule :my-at-job-name
#(println "I fire 4 times with a 10ms delay between each, starting in 500ms.")
:in 500 ; ms
:every 10 ; ms
:repeat 3
:singleton false)
Upvotes: 0