Created
April 25, 2014 16:38
-
-
Save micha/11295651 to your computer and use it in GitHub Desktop.
Boot tasks: run tasks in the background, etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def bgs | |
"List of tasks running in other threads that will need to be cleaned up before | |
boot can exit." | |
(atom ())) | |
;; cleanup background tasks on shutdown | |
(-> (Runtime/getRuntime) | |
(.addShutdownHook (Thread. #(doseq [job @bgs] (future-cancel job))))) | |
(deftask once | |
"Evaluate the `task` only once. Subsequent evaluations will pass through." | |
[task] | |
(let [ran? (atom false) | |
run? (partial compare-and-set! ran? @ran?)] | |
(fn [continue] | |
(let [task (task continue)] | |
#(continue ((if (run? true) task identity) %)))))) | |
(deftask wait | |
"Wait for msec milliseconds before calling its continuation." | |
[msec] | |
(with-pre-wrap (Thread/sleep msec))) | |
(deftask bg | |
"Run the given `task` once, in a separate thread." | |
[task] | |
(once | |
(with-pre-wrap | |
(swap! bgs conj (future ((task identity) *event*)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment