Skip to content

Instantly share code, notes, and snippets.

@mwmitchell
Created July 23, 2013 19:28
Show Gist options
  • Save mwmitchell/6065398 to your computer and use it in GitHub Desktop.
Save mwmitchell/6065398 to your computer and use it in GitHub Desktop.
(ns fetcher
(:require [clojure.tools.logging :as log]))
(def completed? (promise))
(def status (atom 0))
(defmacro make-worker [& forms]
`(do
(swap! status inc)
(try
(do ~@forms)
(catch Throwable e# (log/info "Error" e#)))
(swap! status dec)))
(defn make-request [url]
(log/info "calling " url)
(when (re-find #"yahoo" url)
(throw (Exception. (str "b00m on" url))))
(Thread/sleep 1000))
(def urls ["http://google.com"
"http://www.roomkey.com"
"http://yahoo.com"])
(def work (map (fn [url]
(fn []
(make-worker (make-request url)))) urls))
(add-watch status :done? (fn [_ _ o n] (when (= n 0) (deliver completed? true))))
(log/info "starting...")
(doseq [w work]
(future (w)))
@completed?
(log/info "done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment