Last active
August 29, 2015 14:04
-
-
Save shaunr0b/a30d3235df3624034732 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 example.runner | |
(:require [taoensso.timbre :as timbre]))) | |
(timbre/refer-timbre) | |
(defn run-service | |
"Apply each member of a collection to a function. | |
Collect and return simple profiling info, | |
success and failures." | |
[coll run-fn run-key] | |
(let [now #(java.util.Date.) | |
elapsed (fn [t1 t2] (- (.getTime t1) (.getTime t2))) | |
since #(elapsed (now) %) | |
start-time (now) | |
run-times (atom nil) | |
status (atom nil) | |
successes (atom []) | |
failures (atom []) | |
coll-cnt (count coll) | |
current (atom 0) | |
futures (atom [])] | |
(info "Starting" run-key :count coll-cnt ) | |
(doseq [c coll] | |
(swap! futures conj | |
(future | |
(info :service run-key :running (str "(" (swap! current inc) " of " coll-cnt ")")) | |
(let [c-start-time (java.util.Date.)] | |
(try | |
(let [result (run-fn c)] | |
(swap! run-times conj (since c-start-time)) | |
(info :service run-key :success ) | |
(swap! successes conj result)) | |
(catch Throwable t t | |
(warn :service run-key :error (.getMessage t)) | |
(swap! failures conj {:error (.getMessage t)}))))))) | |
(doall (map deref @futures)) ; wait till all jobs complete | |
(let [output | |
{:run-time (since start-time) | |
:run-times @run-times | |
:successes @successes | |
:failures @failures}] | |
(println output) | |
output))) | |
; (run-service [1 2 3 0] #(do (Thread/sleep 500) (/ 3 %)) :inc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment