               (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))]

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

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


(defroutes main-routes
  (POST "/add_job" request (add-job (read-json (:query (:params request)))))
  (POST "/delete_job" request (delete-job (:id (:params request))))
  (POST "/run_jobs" request (do (send-off job-runner #'schedule-job-run)
                                "OK"))
  (GET "/job_output/:id" [id] (serve-file id))
  (GET "/job_list" [] (render-job-list))
  (route/files "/" [:root "public"])
