mirror of
https://github.com/penpot/penpot.git
synced 2025-05-14 12:06:40 +02:00
✨ Add several improvements to internal worker impl
Mainly for make the cron jobs do not block the scheduled executor and offload all work to a separate threads
This commit is contained in:
parent
cad1851e95
commit
04b321caae
2 changed files with 64 additions and 65 deletions
|
@ -297,10 +297,10 @@
|
||||||
::wrk/executor (ig/ref ::wrk/executor)
|
::wrk/executor (ig/ref ::wrk/executor)
|
||||||
::session/manager (ig/ref ::session/manager)}
|
::session/manager (ig/ref ::session/manager)}
|
||||||
|
|
||||||
:app.http.websocket/routes
|
::http.ws/routes
|
||||||
{::db/pool (ig/ref ::db/pool)
|
{::db/pool (ig/ref ::db/pool)
|
||||||
::mtx/metrics (ig/ref ::mtx/metrics)
|
::mtx/metrics (ig/ref ::mtx/metrics)
|
||||||
::mbus/msgbus (ig/ref :app.msgbus/msgbus)
|
::mbus/msgbus (ig/ref ::mbus/msgbus)
|
||||||
::session/manager (ig/ref ::session/manager)}
|
::session/manager (ig/ref ::session/manager)}
|
||||||
|
|
||||||
:app.http.assets/routes
|
:app.http.assets/routes
|
||||||
|
|
|
@ -27,8 +27,7 @@
|
||||||
(:import
|
(:import
|
||||||
java.util.concurrent.ExecutorService
|
java.util.concurrent.ExecutorService
|
||||||
java.util.concurrent.ForkJoinPool
|
java.util.concurrent.ForkJoinPool
|
||||||
java.util.concurrent.Future
|
java.util.concurrent.Future))
|
||||||
java.util.concurrent.ScheduledExecutorService))
|
|
||||||
|
|
||||||
(set! *warn-on-reflection* true)
|
(set! *warn-on-reflection* true)
|
||||||
|
|
||||||
|
@ -133,7 +132,7 @@
|
||||||
steals))]
|
steals))]
|
||||||
|
|
||||||
(px/thread
|
(px/thread
|
||||||
{:name "penpot/executors-monitor"}
|
{:name "penpot/executors-monitor" :virtual true}
|
||||||
(l/info :hint "monitor: started" :name name)
|
(l/info :hint "monitor: started" :name name)
|
||||||
(try
|
(try
|
||||||
(loop [steals 0]
|
(loop [steals 0]
|
||||||
|
@ -206,26 +205,12 @@
|
||||||
:queued res)))
|
:queued res)))
|
||||||
|
|
||||||
(run-batch! [rconn]
|
(run-batch! [rconn]
|
||||||
|
(try
|
||||||
(db/with-atomic [conn pool]
|
(db/with-atomic [conn pool]
|
||||||
(when-let [tasks (get-tasks conn)]
|
(if-let [tasks (get-tasks conn)]
|
||||||
(->> (group-by :queue tasks)
|
(->> (group-by :queue tasks)
|
||||||
(run! (partial push-tasks! conn rconn)))
|
(run! (partial push-tasks! conn rconn)))
|
||||||
true)))]
|
(px/sleep (::wait-duration cfg))))
|
||||||
|
|
||||||
(if (db/read-only? pool)
|
|
||||||
(l/warn :hint "dispatcher: not started (db is read-only)")
|
|
||||||
(px/thread
|
|
||||||
{:name "penpot/worker-dispatcher"}
|
|
||||||
(l/info :hint "dispatcher: started")
|
|
||||||
(try
|
|
||||||
(dm/with-open [rconn (rds/connect redis)]
|
|
||||||
(loop []
|
|
||||||
(when (px/interrupted?)
|
|
||||||
(throw (InterruptedException. "interrumpted")))
|
|
||||||
|
|
||||||
(try
|
|
||||||
(when-not (run-batch! rconn)
|
|
||||||
(px/sleep (::wait-duration cfg)))
|
|
||||||
(catch InterruptedException cause
|
(catch InterruptedException cause
|
||||||
(throw cause))
|
(throw cause))
|
||||||
(catch Exception cause
|
(catch Exception cause
|
||||||
|
@ -243,16 +228,29 @@
|
||||||
:else
|
:else
|
||||||
(do
|
(do
|
||||||
(l/error :hint "dispatcher: unhandled exception (will retry in an instant)" :cause cause)
|
(l/error :hint "dispatcher: unhandled exception (will retry in an instant)" :cause cause)
|
||||||
(px/sleep (::rds/timeout rconn))))))
|
(px/sleep (::rds/timeout rconn)))))))
|
||||||
|
|
||||||
|
(dispatcher []
|
||||||
|
(l/info :hint "dispatcher: started")
|
||||||
|
(try
|
||||||
|
(dm/with-open [rconn (rds/connect redis)]
|
||||||
|
(loop []
|
||||||
|
(run-batch! rconn)
|
||||||
(recur)))
|
(recur)))
|
||||||
|
|
||||||
(catch InterruptedException _
|
(catch InterruptedException _
|
||||||
(l/debug :hint "dispatcher: interrupted"))
|
(l/trace :hint "dispatcher: interrupted"))
|
||||||
(catch Throwable cause
|
(catch Throwable cause
|
||||||
(l/error :hint "dispatcher: unexpected exception" :cause cause))
|
(l/error :hint "dispatcher: unexpected exception" :cause cause))
|
||||||
(finally
|
(finally
|
||||||
(l/info :hint "dispatcher: terminated")))))))
|
(l/info :hint "dispatcher: terminated"))))]
|
||||||
|
|
||||||
|
(if (db/read-only? pool)
|
||||||
|
(l/warn :hint "dispatcher: not started (db is read-only)")
|
||||||
|
|
||||||
|
;; FIXME: we don't use virtual threads here until JDBC is uptaded to >= 42.6.0
|
||||||
|
;; bacause it has the necessary fixes fro make the JDBC driver properly compatible
|
||||||
|
;; with Virtual Threads.
|
||||||
|
(px/fn->thread dispatcher :name "penpot/worker/dispatcher" :virtual false))))
|
||||||
|
|
||||||
(defmethod ig/halt-key! ::dispatcher
|
(defmethod ig/halt-key! ::dispatcher
|
||||||
[_ thread]
|
[_ thread]
|
||||||
|
@ -297,7 +295,7 @@
|
||||||
(defn- start-worker!
|
(defn- start-worker!
|
||||||
[{:keys [::rds/redis ::worker-id ::queue] :as cfg}]
|
[{:keys [::rds/redis ::worker-id ::queue] :as cfg}]
|
||||||
(px/thread
|
(px/thread
|
||||||
{:name (format "penpot/worker/%s" worker-id)}
|
{:name (format "penpot/worker/runner:%s" worker-id)}
|
||||||
(l/info :hint "worker: started" :worker-id worker-id :queue queue)
|
(l/info :hint "worker: started" :worker-id worker-id :queue queue)
|
||||||
(try
|
(try
|
||||||
(dm/with-open [rconn (rds/connect redis)]
|
(dm/with-open [rconn (rds/connect redis)]
|
||||||
|
@ -584,13 +582,14 @@
|
||||||
|
|
||||||
(defn- execute-cron-task
|
(defn- execute-cron-task
|
||||||
[{:keys [::db/pool] :as cfg} {:keys [id] :as task}]
|
[{:keys [::db/pool] :as cfg} {:keys [id] :as task}]
|
||||||
|
(px/thread
|
||||||
|
{:name (str "penpot/cront-task/" id)}
|
||||||
(try
|
(try
|
||||||
(db/with-atomic [conn pool]
|
(db/with-atomic [conn pool]
|
||||||
(when (db/exec-one! conn [sql:lock-cron-task (d/name id)])
|
(when (db/exec-one! conn [sql:lock-cron-task (d/name id)])
|
||||||
(l/trace :hint "cron: execute task" :task-id id)
|
(l/trace :hint "cron: execute task" :task-id id)
|
||||||
((:fn task) task)))
|
((:fn task) task)))
|
||||||
(catch InterruptedException _
|
(catch InterruptedException _
|
||||||
(px/interrupt! (px/current-thread))
|
|
||||||
(l/debug :hint "cron: task interrupted" :task-id id))
|
(l/debug :hint "cron: task interrupted" :task-id id))
|
||||||
(catch Throwable cause
|
(catch Throwable cause
|
||||||
(l/error :hint "cron: unhandled exception on running task"
|
(l/error :hint "cron: unhandled exception on running task"
|
||||||
|
@ -599,7 +598,7 @@
|
||||||
:cause cause))
|
:cause cause))
|
||||||
(finally
|
(finally
|
||||||
(when-not (px/interrupted? :current)
|
(when-not (px/interrupted? :current)
|
||||||
(schedule-cron-task cfg task)))))
|
(schedule-cron-task cfg task))))))
|
||||||
|
|
||||||
(defn- ms-until-valid
|
(defn- ms-until-valid
|
||||||
[cron]
|
[cron]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue