Skip to content

Instantly share code, notes, and snippets.

@ostronom
Last active August 29, 2015 14:11
Show Gist options
  • Save ostronom/0c263a45f0e2f69308b7 to your computer and use it in GitHub Desktop.
Save ostronom/0c263a45f0e2f69308b7 to your computer and use it in GitHub Desktop.
(defn calculate-deposit* [ch]
(let [control (chan 256)
users (udb/get-users)
total (count users)]
(doseq [g (partition-all (quot total 50) users)]
(future
(doseq [user g]
(let [data (calculate-user-deposit-row user)]
(if data (put! ch (str data "\n")))
(put! control 1)))))
(go
(<!
(go-loop [i 1]
(if (and (<! control) (< i total)) (recur (inc i)))))
(close! ch))))
(defn calculate-deposit [req]
(let [headers {"Content-Type" "text/csv"
"Content-Disposition" "attachment; filename=\"deposit.csv\""}
res-ch (chan 256)]
(with-channel req channel
(send! channel {:headers headers :status 200} false)
(calculate-deposit* res-ch)
(go
(<! (go-loop []
(when-let [e (<! res-ch)]
(send! channel e false)
(recur))))
(close channel)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment