Skip to content

Commit

Permalink
Add reset-executor! and shutdown-executor! to re-frame.interop
Browse files Browse the repository at this point in the history
  • Loading branch information
lowecg committed Nov 17, 2024
1 parent f654f42 commit c4a9d7d
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/re_frame/interop.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns re-frame.interop
(:import [java.util.concurrent Executor Executors]))
(:import [java.util.concurrent Executor ExecutorService Executors TimeUnit]))

;; The purpose of this file is to provide JVM-runnable implementations of the
;; CLJS equivalents in interop.cljs.
Expand Down Expand Up @@ -94,3 +94,21 @@
(defn reactive?
[]
true)

(defn reset-executor!
"Creates a fresh single-threaded executor when the current one is shutdown. Essential for environments like AWS Lambda
where the executor must be recycled between function invocations."
[]
(when (and executor (.isShutdown ^ExecutorService executor))
(alter-var-root #'executor
(fn [_] (Executors/newSingleThreadExecutor)))))

(defn shutdown-executor!
"Cleanly terminates the executor service and waits for pending tasks to complete.
Required when running from CLI, tests or Lambda functions to prevent the JVM from hanging,
since the executor runs on non-daemon threads. The await-timeout-ms parameter sets how long
to wait for task completion in milliseconds. Call reset-executor! to restart execution capabilities."
[await-timeout-ms]
(.shutdown ^ExecutorService executor)
(.awaitTermination ^ExecutorService executor await-timeout-ms TimeUnit/MILLISECONDS))

0 comments on commit c4a9d7d

Please sign in to comment.