 (def configuration nil)
 (def mq-addr "vm://localhost?jms.prefetchPolicy.all=1&create=false")
 (def mq-endpoint "com.puppetlabs.puppetdb.commands")
+(def send-command! (partial command/enqueue-command! mq-addr mq-endpoint))
 
 (defn load-from-mq
   "Process commands from the indicated endpoint on the supplied message queue.

;;   that come in via REST are relayed to the message queue. Read-only
;;   requests are serviced synchronously.
;;
;; * Database compactor
;;
;;   As catalogs are modified, unused records may accumulate and stale
;;   data may linger in the database. We periodically compact the
;;   database to maintain acceptable performance.
;;
(ns com.puppetlabs.puppetdb.cli.services
  (:require [com.puppetlabs.puppetdb.scf.storage :as scf-store]

   (with-open [conn (mq/connect! mq)]
     (command/process-commands! conn mq-endpoint discard-dir {:db db}))))

(defn sweep-database!
  "Sweep the indicated database every `interval` minutes.

  This function doesn't terminate. If we encounter an exception during
  compaction, the operation will be retried after `interval` millis.

  This function will perform database garbage collection, and will also
  deactivate nodes more stale than `node-ttl-days`"
  [db interval node-ttl-days]
  (let [sleep #(Thread/sleep (* 60 1000 interval))]
    (pl-utils/keep-going
     (fn [exception]
       (log/error exception "Error during database sweep")
       (sleep))

     (pl-utils/demarcate
      "database garbage collection"
      (with-transacted-connection db
        (scf-store/garbage-collect!)))

     (when (pos? node-ttl-days)
       (pl-utils/demarcate
        (format "sweep of stale nodes (%s day threshold)" node-ttl-days)
        (with-transacted-connection db
          (doseq [node (scf-store/stale-nodes (time/ago (time/days node-ttl-days)))]
            (send-command! "deactivate node" 1 node)))))

     (sleep))))

