Skip to content

Instantly share code, notes, and snippets.

@zahardzhan
Created August 3, 2010 05:41
Show Gist options
  • Save zahardzhan/505901 to your computer and use it in GitHub Desktop.
Save zahardzhan/505901 to your computer and use it in GitHub Desktop.
(defmacro defhook [name qualifier target args & body]
(let [key# (keyword name)]
`(add-hook ~qualifier ~target ~key# (fn ~args ~@body))))
(defn run [{:as a :keys [strategy run-atom]}]
(when (run? a) (throw (Exception. "Cannot run agent while it is running.")))
(when (dead? a) (throw (Exception. "Cannot run dead agent.")))
(try (reset! run-atom true)
(let [action (strategy a)]
(action a))
(finally (reset! run-atom false))))
(defhook log :around #'run [run a]
(debug (str "Run " (download-agent-to-str a)))
(try (run a)
(catch Throwable t
(error (str "Fail " (download-agent-to-str a) ": " t))
(throw t))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment