Scheduled Tasks

Biff uses chime to execute functions on a recurring schedule. For each task, you must provide a function to run, a zero-argument schedule function which will return a list of times at which to execute the task function, and an (optional) error handler. The schedule can be an infinite sequence. For example, here's a task that prints out the number of users every 60 seconds:

(require '[com.biffweb :as biff :refer [q]])

(defn print-usage [{:keys [biff/db]}]
  (let [n-users (first (q db
                          '{:find (count user)
                            :where [[user :user/email]]}))]
    (println "There are" n-users "users.")))

(defn every-minute []
  (iterate #(biff/add-seconds % 60) (java.util.Date.)))

(defn error-handler [error]
  (println "Uh oh!"))

(def module
  {:tasks [{:task #'print-usage
            :schedule every-minute
            :error-handler error-handler}]})

See also:

Have a question? Join the #biff channel on Clojurians Slack, or ask on GitHub.

Sign up for Biff: The Newsletter
Announcements, blog posts, et cetera et cetera.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.