     [cider-ci.builder.issues :refer [create-issue]]
 
     [cider-ci.utils.core :refer [deep-merge]]
+    [cider-ci.utils.daemon :refer [defdaemon]]
     [cider-ci.utils.http :as http]
     [cider-ci.utils.include-exclude :as include-exclude]
     [cider-ci.utils.jdbc :refer [insert-or-update]]
     [cider-ci.utils.map :refer [convert-to-array]]
-    [cider-ci.utils.messaging :as messaging]
     [cider-ci.utils.rdbms :as rdbms]
 
     [clj-yaml.core :as yaml]

                      (sql/from :jobs)
                      (sql/merge-where [:= :tree_id tree-id])
                      (sql/merge-where [:= :key (:job_key trigger)])
                      (sql/merge-where [:in :state (:states trigger)])
                      (sql/limit 1)) sql/format) ]
    (logging/debug query)
    (->> query


;##############################################################################

(defn- trigger-jobs [tree-id]
  (locking tree-id
    (catcher/snatch
      {:return-fn (fn [e] (create-issue "tree" tree-id e))}
      (I>> identity-with-logging
           (project-configuration/get-project-configuration tree-id)
           :jobs
           convert-to-array
           (filter #(-> % :run_when))
           (filter #(some-job-trigger-fulfilled? tree-id %))
           (map #(assoc % :tree_id tree-id))
           (filter jobs.dependencies/fulfilled?)
           (map jobs/create)
           doall)))
  tree-id)

;##############################################################################

(defn evaluate-tree-id-notifications []
  (I>> identity-with-logging
       "SELECT * FROM tree_id_notifications ORDER BY created_at ASC LIMIT 100"
       (jdbc/query (rdbms/get-ds))
       (map (fn [row]
              (future (trigger-jobs (:tree_id row))
                      row)))
       (map deref)
       (map :id)
       (map #(jdbc/delete! (rdbms/get-ds) :tree_id_notifications ["id = ?" %]))
       doall))

(defdaemon "evaluate-tree-id-notifications" 1 (evaluate-tree-id-notifications))


;### initialize ###############################################################

(defn initialize []
  (start-evaluate-tree-id-notifications))


;### Debug ####################################################################
