 
 
 (defn build-update-error-handler
-  "Returns a clojure that can be called in case there is an update error. The closure gives access to
+  "Returns a closure that can be called in case there is an update error. The closure gives access to
   the queue so votes can be requeued"
-  [queue conv-actor]
+  [conv-man queue conv]
   (fn [votes start-time opts update-error]
-    (let [zid-str (str "zid=" (:zid @conv-actor))]
+    (let [zid-str (str "zid=" (:zid conv))]
       (log/error "Failed conversation update for" zid-str)
       (.printStackTrace update-error)
       ; Try requeing the votes that failed so that if we get more, they'll get replayed

                (flatten))))))


(defn new-conv-actor [init-fn & opts]
  "Create a new conv actor which responds to snd. Messages should look like [<t> [...]], where <t> is either :votes or
  :moderation, depending on what kind of batch is being sent/processed. Implements deref for state retrieval."
  (let [msgbox (chan Long/MAX_VALUE) ; we want this to be as big as possible, since backpressure doesn't really work
        conv (atom (init-fn)) ; keep track of conv state in atom so it can be dereffed
        ca (ConvActor. msgbox conv)
        err-handler (build-update-error-handler msgbox ca)]
    ; Initialize the go loop which watches the mailbox, and runs updates all all pending messages when they
    ; arrive
    (go-loop []
      (try
        (let [first-msg (<! msgbox) ; do this so we park efficiently
              msgs      (take-all! msgbox)
              msgs      (concat [first-msg] msgs)
              {votes :votes mods :moderation}
                        (split-batches msgs)]
          (when mods
            (swap! conv conv/mod-update mods))
          (swap! conv update-fn (or votes []) err-handler))
        (catch Exception e
          (log/error "Excpetion not handler by err-handler:" e)
          (.printStackTrace e)))
      (recur))
    ca))



        ; Format and upload main results
        (doseq [[col-name prep-fn] [["main" prep-main] ; main math results, for client
                                    ["bidtopid" prep-bidToPid]]] ; bidtopid mapping, for server
          ;; XXX Hmmm... format-for-mongo should be abstracted so that it always get's called, and the prep
          ;; function gets taken care of separately; don't need to conplect these
          (->> updated-conv
               prep-fn
               (mongo/zid-upsert mongo (mongo/collection-name mongo col-name))))
        (log/info "Finished uploading mongo results for zid" zid)
        ; Return the updated conv
        updated-conv)
