diff options
| author | sostler <sbostler@gmail.com> | 2010-06-05 03:22:44 -0400 |
|---|---|---|
| committer | sostler <sbostler@gmail.com> | 2010-06-05 03:22:44 -0400 |
| commit | dcc43bef85f4ae2da747a14c7e94de035d01d8bb (patch) | |
| tree | 0df25da8e2b5d389b2b4495d6eeffb33ddd87dfb /src/scheduled_agent.clj | |
| parent | f9024ac302e7387cccc821174ec4c68e25251af3 (diff) | |
scheduled-agents must be started now
Diffstat (limited to 'src/scheduled_agent.clj')
| -rw-r--r-- | src/scheduled_agent.clj | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/src/scheduled_agent.clj b/src/scheduled_agent.clj index 2d1588f..d2e1454 100644 --- a/src/scheduled_agent.clj +++ b/src/scheduled_agent.clj @@ -1,8 +1,10 @@ -(ns scheduled-agent ++(ns scheduled-agent (:import java.util.concurrent.Executors java.util.concurrent.TimeUnit) (:use clojure.stacktrace)) +; This is starting to get less agent-like + (defn- runnable-proxy [f] (proxy [Runnable] [] (run [] (f)))) @@ -15,12 +17,12 @@ (let [val (func)] (dosync (ref-set data val))) (catch Exception e - (print-stack-trace e 5))))) - future (.scheduleWithFixedDelay pool pfunc 0 period TimeUnit/SECONDS)] + (print-stack-trace e 5)))))] {:pool pool :data data - :future future + :future (ref nil) :func func + :pfunc pfunc :period period :init init})) @@ -28,9 +30,15 @@ "Return current contents of agent." @d) +(defn start! [{f :future pfunc :pfunc period :period pool :pool}] + (dosync + (if (nil? (ensure f)) + (ref-set f (.scheduleWithFixedDelay pool pfunc 0 period TimeUnit/SECONDS))))) + (defn cancel! [{f :future}] "Cancel automatic updating of agent data. Cannot be restarted." - (.cancel f false)) + (if @f + (.cancel @f false))) (defn update! [{func :func data :data}] "Synchronously update contents of agent." |
