summaryrefslogtreecommitdiff
path: root/src/scheduled_agent.clj
diff options
context:
space:
mode:
authorsostler <sbostler@gmail.com>2010-04-16 13:41:02 -0400
committersostler <sbostler@gmail.com>2010-04-16 13:41:02 -0400
commit21922b749a5696a2ea4274e0968ad2d3d20dcc9d (patch)
tree49c1fdd33fffe318109e6609ad1b5db343d3d0ed /src/scheduled_agent.clj
parentff63970f9d785672f146d82cf1da0935db4b9b5e (diff)
Intermediate commit from muting admin
Diffstat (limited to 'src/scheduled_agent.clj')
-rw-r--r--src/scheduled_agent.clj24
1 files changed, 15 insertions, 9 deletions
diff --git a/src/scheduled_agent.clj b/src/scheduled_agent.clj
index 702b314..b42bb57 100644
--- a/src/scheduled_agent.clj
+++ b/src/scheduled_agent.clj
@@ -9,23 +9,29 @@
(defn scheduled-agent
[func period init]
(let [pool (Executors/newScheduledThreadPool 1)
- r (ref init)
+ data (ref init)
pfunc (runnable-proxy (fn []
(try
(dosync
- (ref-set r (func (ensure r))))
+ (ref-set data (func (ensure data))))
(catch Exception e
(print-stack-trace e 5)))))
future (.scheduleWithFixedDelay pool pfunc 0 period TimeUnit/SECONDS)]
- {:pool pool
- :data r
+ {:pool pool
+ :data data
:future future
- :func pfunc
+ :func func
:period period
- :init init}))
-
-(defn cancel [{f :future}]
- (.cancel f false))
+ :init init}))
(defn poll [{d :data}]
+ "Return current contents of agent."
@d)
+
+(defn cancel! [{f :future}]
+ "Cancel automatic updating of agent data. Cannot be restarted."
+ (.cancel f false))
+
+(defn update! [{func :func data :data}]
+ "Synchronously update contents of agent."
+ (dosync (ref-set data (func (ensure data))))) \ No newline at end of file