Skip to content

Instantly share code, notes, and snippets.

@andradefil
Created October 22, 2025 14:59
Show Gist options
  • Select an option

  • Save andradefil/8aa1ffb77b595d7a5647a5a81c35e7c2 to your computer and use it in GitHub Desktop.

Select an option

Save andradefil/8aa1ffb77b595d7a5647a5a81c35e7c2 to your computer and use it in GitHub Desktop.
Long Running Task
(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