Sunday, September 14, 2008

With Retry

I've been using Clojure for work and play for some time now. It is awesome language. It revived my interest and believe in JVM as a platform.

I am writing a script to upload all my pictures to Flickr. I have a pretty big collection of photos starting from 1997 when I've got my first digital camera. I've tried many Flickr uploaders to do the task, but it was too much manual work. I am a programmer and hate manual steps, that is what programs for.

I've tried repeatedly over the years in my little spare time to write the script. Every year I would try in different language because I would loose the script or the interest in it. That was a major cause of my failure to finish that script. And my photo album stayed in Gallery and never was migrated to Flickr.

The title of this blog is with retry and that is exactly what I am doing. This is were Clojure comes to the rescue. There are plenty of Java APIs for Flickr. They are well documented, stable and easy to use. Making Clojure to drive one of these APIs flickrj is a pleasure. I will publish the script, which have a little GUI component, just because it so easy to do in Clojure :)

For now to keep this post a little bit more technical here is a little macro that I wrote to satisfy script need to handle restart on error in the script.

(defmacro with-retry [times & body]
  `(let [done# (ref false)]
     (loop [time# 0 result# nil]
       (if (and (not @done#) (< time# ~times)) 
         (recur (+ 1 time#)
                (try (let [result# (do ~@body)]
                          (dosync (ref-set done# true))
                          result#)
                     (catch Exception ex#
                       (println "Retrying:" time# "time" ex#))))
         result#))))

There is got to be a better way to write it, any suggestions are welcome.
Of cause the point of this post is that it was easy for me to write and to use this macro in Clojure.
Clojure is an excellent language for scripting!

1 comment:

Vishnu said...

Hi Dima,
This is Vishnu here.
Good to see you blogs.

Keep posting,will read regurally.