Skip to content

Commit

Permalink
Support job names as keywords or strings [IMMUTANT-260]
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias committed Apr 17, 2013
1 parent 3e0f7ca commit 3b7eafa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
6 changes: 4 additions & 2 deletions docs/src/org/jobs.org
Expand Up @@ -24,7 +24,7 @@
"*/5 * * * * ?"
:singleton false)

(jobs/schedule "my-at-job-name"
(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
Expand All @@ -38,7 +38,9 @@
+ =name= - the name of the job. If the given name is the same as the
name of previously scheduled job in the application's runtime, the
prior job will be unscheduled and the new job will be scheduled in
its place.
its place. The name can be either a keyword or a string, but
keywords will be converted to strings internally, so =:foo= and
="foo"= will refer to the same job.
+ =f= - the zero argument function that will be invoked each time
the job fires.
+ =spec= - the optional cron-style specification string if you are
Expand Down
7 changes: 7 additions & 0 deletions integration-tests/apps/jobs/test/jobs/at.clj
Expand Up @@ -11,6 +11,13 @@
~@body
(finally (job/unschedule "a-job"))))

(deftest should-accept-a-keyword-name
(let [q (random-queue)]
(try
(job/schedule :job #(msg/publish q "ping"))
(is (= "ping" (msg/receive q :timeout 10000)))
(finally (job/unschedule :job)))))

(deftest an-empty-hash-should-fire-once
(let [q (random-queue)]
(with-job #(msg/publish q "ping") []
Expand Down
7 changes: 7 additions & 0 deletions integration-tests/apps/jobs/test/jobs/cron.clj
Expand Up @@ -15,6 +15,13 @@
(with-job #(msg/publish q "ping")
(is (= ["ping" "ping" "ping"] (take 3 (msg/message-seq q)))))))

(deftest jobs-should-work-with-a-keyword-name
(let [q (random-queue)]
(try
(job/schedule :kw-job #(msg/publish q "ping") "*/1 * * * * ?")
(is (= ["ping" "ping" "ping"] (take 3 (msg/message-seq q))))
(finally (job/unschedule :kw-job)))))

(deftest rescheduling
(let [q1 (random-queue)
q2 (random-queue)]
Expand Down
14 changes: 8 additions & 6 deletions modules/jobs/src/main/clojure/immutant/jobs.clj
Expand Up @@ -28,11 +28,12 @@
(defn unschedule
"Removes the named job from the scheduler"
[name]
(when-let [job (@current-jobs name)]
(log/info "Unscheduling job" name)
(internal/stop-job job)
(swap! current-jobs dissoc name)
true))
(let [name (clojure.core/name name)]
(when-let [job (@current-jobs name)]
(log/info "Unscheduling job" name)
(internal/stop-job job)
(swap! current-jobs dissoc name)
true)))

(def
^{:arglists '([name f spec & {:keys [singleton] :or {singleton true}}]
Expand Down Expand Up @@ -64,7 +65,8 @@ will replace that job."}
schedule
(fn
[name & opts]
(let [{:keys [fn spec] :as opts} (internal/extract-spec opts)]
(let [{:keys [fn spec] :as opts} (internal/extract-spec opts)
name (clojure.core/name name)]
(unschedule name)
(log/info "Scheduling job" name "with" spec)
(letfn [(job [ctx] (binding [*job-execution-context* ctx] (fn)))]
Expand Down

0 comments on commit 3b7eafa

Please sign in to comment.