0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-21 22:22:43 -05:00

Expose ::wrk/executor as ExecutorService instance

Instead of a plain Executor instance
This commit is contained in:
Andrey Antukh 2024-10-26 14:38:53 +02:00
parent 302ff92b31
commit 4299fd28f0

View file

@ -17,12 +17,11 @@
[integrant.core :as ig] [integrant.core :as ig]
[promesa.exec :as px]) [promesa.exec :as px])
(:import (:import
java.util.concurrent.Executor
java.util.concurrent.ThreadPoolExecutor)) java.util.concurrent.ThreadPoolExecutor))
(set! *warn-on-reflection* true) (set! *warn-on-reflection* true)
(s/def ::wrk/executor #(instance? Executor %)) (s/def ::wrk/executor #(instance? ThreadPoolExecutor %))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; EXECUTOR ;; EXECUTOR
@ -36,30 +35,22 @@
(let [factory (px/thread-factory :prefix "penpot/default/") (let [factory (px/thread-factory :prefix "penpot/default/")
executor (px/cached-executor :factory factory :keepalive 60000)] executor (px/cached-executor :factory factory :keepalive 60000)]
(l/inf :hint "executor started") (l/inf :hint "executor started")
(reify executor))
java.lang.AutoCloseable
(close [_]
(l/inf :hint "stoping executor")
(px/shutdown! executor))
clojure.lang.IDeref
(deref [_]
{:active (.getPoolSize ^ThreadPoolExecutor executor)
:running (.getActiveCount ^ThreadPoolExecutor executor)
:completed (.getCompletedTaskCount ^ThreadPoolExecutor executor)})
Executor
(execute [_ runnable]
(.execute ^Executor executor ^Runnable runnable)))))
(defmethod ig/halt-key! ::wrk/executor (defmethod ig/halt-key! ::wrk/executor
[_ instance] [_ instance]
(.close ^java.lang.AutoCloseable instance)) (px/shutdown! instance))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; MONITOR ;; MONITOR
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- get-stats
[^ThreadPoolExecutor executor]
{:active (.getPoolSize ^ThreadPoolExecutor executor)
:running (.getActiveCount ^ThreadPoolExecutor executor)
:completed (.getCompletedTaskCount ^ThreadPoolExecutor executor)})
(s/def ::name ::us/keyword) (s/def ::name ::us/keyword)
(defmethod ig/pre-init-spec ::wrk/monitor [_] (defmethod ig/pre-init-spec ::wrk/monitor [_]
@ -74,7 +65,7 @@
[_ {:keys [::wrk/executor ::mtx/metrics ::interval ::wrk/name]}] [_ {:keys [::wrk/executor ::mtx/metrics ::interval ::wrk/name]}]
(letfn [(monitor! [executor prev-completed] (letfn [(monitor! [executor prev-completed]
(let [labels (into-array String [(d/name name)]) (let [labels (into-array String [(d/name name)])
stats (deref executor) stats (get-stats executor)
completed (:completed stats) completed (:completed stats)
completed-inc (- completed prev-completed) completed-inc (- completed prev-completed)