Created
July 23, 2013 19:28
-
-
Save mwmitchell/6065398 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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