               (recur)))))))
 
 
-(defn run-jobs []
-  ;; we read the job queue at a moment in time.  Anything added after this will
-  ;; have to wait for the next run.
-  (let [jobs (filter #(= (:status @%) :not-started)
-                     @job-queue)
-        files (into {} (map (fn [job]
+
+(def job-runner (agent []))
+
+(defn run-jobs [jobs]
+  (let [files (into {} (map (fn [job]
                               (let [outfile (file output-dir (str (:id @job) ".txt"))
                                     outfh (MarcXmlWriter.
                                            (FileOutputStream. outfile))]

                                      :fh outfh}]))
                            jobs))]

    (doseq [job jobs] (swap! job assoc :status :running))

    (let [queue (LinkedBlockingQueue. 512)
          workers (doall
                   (map #(run-worker % jobs files queue)

(def job-queue (atom []))
(def output-dir "/var/tmp")
(def worker-threads 2)
(def max-concurrent-jobs 1)

(def controlfield-pattern #"00[0-9]")

