Although a while loop does work here, it's just a hack to get the desired behavior. Have you considered using a scheduled thread pool as your controller?

For example:

(def ^{:private true} pool (atom nil))
(def ^{:private true} num-threads 3)

(defn- thread-pool []
  (swap! pool (fn [p] (or p (ScheduledThreadPoolExecutor. num-threads)))))

(defn schedule-periodic
  "Schedules function f to run every 'delay' milliseconds after an
  initial delay of 'initial-delay'."
  [f initial-delay delay]
  (.scheduleWithFixedDelay (thread-pool)
               (protect-from-exceptions f)
                           initial-delay delay TimeUnit/MILLISECONDS))


(defn run-always []
  (schedule-periodic dosomething 0 60))

This is a simple example that lacks general robustness around error handling, but provides a clean way to continuously run things. You can also provide a clean shutdown a la:

(defn shutdown
  "Terminates all periodic tasks."
  []
  (swap! pool (fn [p] (when p (.shutdown p)))))

Hope this helps,

Aaron Bedra
--
Clojure/core
http://clojure.com


On 02/21/2011 06:35 PM, Brent Millare wrote:
Seems to work for me. Are you putting something valid in (do
something)?

Also if you want to watch files in a directory, there is JNotify,
which might be useful to you.

On Feb 21, 5:19 pm, clj123<ariela2...@gmail.com>  wrote:
Hello,

I'm looking for an on-going service that runs and processes files in a
certain location. This function should be contiously running.

The function that I've used, it always running and when files are
place it does something with it.

(defn run-always []
     (while true (dosomething)))

However, it throws an exception:
Expression failed: (run-always)

I'm not sure why it's throwing it, and is there a better way to handle
this kind of service?

Thanks.


--
Cheers,

Aaron Bedra
--
Clojure/core
http://clojure.com

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to