Skip to content

Instantly share code, notes, and snippets.

@micha
Created April 25, 2014 16:38
Show Gist options
  • Save micha/11295651 to your computer and use it in GitHub Desktop.
Save micha/11295651 to your computer and use it in GitHub Desktop.
Boot tasks: run tasks in the background, etc.
(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