Created
October 22, 2025 14:59
-
-
Save andradefil/8aa1ffb77b595d7a5647a5a81c35e7c2 to your computer and use it in GitHub Desktop.
Long Running Task
This file contains hidden or 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
| (ns system.process) | |
| ;; Define a Long Running Task | |
| (defprotocol LongRunningProcessLifecycle | |
| (start [this] "Start a long running task") | |
| (finish [this] "Stop the runnig task")) | |
| ;; Implements lifecycle protocol | |
| (defrecord LongRunningProcess | |
| [worker] | |
| LongRunningProcessLifecycle | |
| (start [this] | |
| (reset! | |
| worker | |
| (Thread. | |
| (fn [] | |
| (try | |
| (loop [] | |
| (println "dispatches op") | |
| (println "sleeping 1000ms") | |
| (Thread/sleep 1000) | |
| (recur)) | |
| (catch InterruptedException _))))) | |
| (.start @worker)) | |
| (finish [this] | |
| (.interrupt @worker) | |
| (reset! worker nil))) | |
| (defn create-process | |
| [] | |
| (->LongRunningProcess (atom nil))) | |
| (comment | |
| (require '[system.process :as p]) | |
| (def proc (p/create-process)) | |
| (p/start proc) | |
| (p/finish proc)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment