-
Notifications
You must be signed in to change notification settings - Fork 11
/
core.clj
35 lines (32 loc) · 945 Bytes
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(ns framework.scheduler.core
(:require
[clojure.core.async :as async :refer [<! chan timeout close! go-loop]]
[clojure.core.async.impl.protocols :refer [closed?]]
[clojure.tools.logging :as logging])
(:import
(java.lang
AutoCloseable)))
(defrecord Closeable-channels-atom
[channels]
AutoCloseable
(close [this]
(doseq [c @(:channels this)]
(swap! (:channels this) disj c)
(close! c))))
(defonce channels
(atom #{}))
(defn start
[deps action interval-msecs]
(let [chan (chan)]
(go-loop [chan chan]
(async/<! (timeout interval-msecs))
(logging/debugf "Executing scheduled action %s" action)
(if (or
(nil? chan)
(closed? chan))
(logging/debugf "Stop %s execution" action)
(do
(action deps)
(recur chan))))
(swap! channels conj chan)
(assoc deps :scheduled-jobs (->Closeable-channels-atom channels))))