   [coll]
   (= coll (sort coll)))
 
-()
+(defn- write-completed-tasks
+  "Writes completed tasks between start-time and current-time to the temp-out-trace-file."
+  [mesos-db time-ms-between-incremental-output temp-out-trace-file simulation-start-time start-time current-time]
+  (log/info "Starting temporary output iteration" start-time "to" current-time)
+  (let [start-date (Date. ^long start-time)
+        end-date (Date. ^long current-time)
+        completed-task-ents (pull-all-task-ents-completed-in-time-range mesos-db start-date end-date)]
+    (if (seq completed-task-ents)
+      (do
+        (log/info "Writing" (count completed-task-ents) "tasks to temporary output for simulation time" current-time)
+        (dump-jobs-to-csv completed-task-ents temp-out-trace-file
+                          :add-headers (= start-time simulation-start-time)
+                          :append true))
+      (log/info "No completed tasks in last" time-ms-between-incremental-output "ms"))))
 
 ;; TODO need way of setting share
 (defn simulate

              :where
              [?i :instance/task-id _]]
            mesos-db)
       (map (partial d/entity mesos-db))))

(defn submit-job

   The start simulation time is the min submit time in the trace.

   Returns a list of the task entities run"
  [mesos-hosts trace cycle-step-ms config temp-out-trace-file]
  (let [simulation-time (-> trace first :submit-time-ms)
        mesos-datomic-conn (restore-fresh-database! (get config :datomic-url "datomic:mem://mock-mesos"))
        offer-trigger-chan (async/chan)
