♻️ Refactor internal organization of system initialization

Add the ability to suspend and add nrepl to the whole system

AI-assisted-by: qwen3.7-plus
This commit is contained in:
Andrey Antukh 2026-07-21 16:41:43 +02:00
parent e1f976aa2f
commit 018d840bab
10 changed files with 227 additions and 118 deletions

View File

@ -104,24 +104,20 @@
[] []
(try (try
(main/start) (main/start)
:started
(catch Throwable cause (catch Throwable cause
(ex/print-throwable cause)))) (ex/print-throwable cause))))
(defn- stop (defn- stop
[] []
(main/stop) (main/stop))
:stopped)
(defn restart (defn restart
[] []
(stop) (main/restart))
(repl/refresh :after 'user/start))
(defn restart-all (defn restart-all
[] []
(stop) (main/restart-all))
(repl/refresh-all :after 'user/start))
;; (defn compression-bench ;; (defn compression-bench
;; [data] ;; [data]

View File

@ -42,6 +42,7 @@ export PENPOT_FLAGS="\
enable-smtp \ enable-smtp \
enable-prepl-server \ enable-prepl-server \
enable-urepl-server \ enable-urepl-server \
enable-nrepl-server \
enable-rpc-climit \ enable-rpc-climit \
enable-rpc-rlimit \ enable-rpc-rlimit \
enable-quotes \ enable-quotes \

View File

@ -253,6 +253,8 @@
[:urepl-port {:optional true} ::sm/int] [:urepl-port {:optional true} ::sm/int]
[:prepl-host {:optional true} :string] [:prepl-host {:optional true} :string]
[:prepl-port {:optional true} ::sm/int] [:prepl-port {:optional true} ::sm/int]
[:nrepl-host {:optional true} :string]
[:nrepl-port {:optional true} ::sm/int]
[:file-data-backend {:optional true} [:enum "db" "legacy-db" "storage"]] [:file-data-backend {:optional true} [:enum "db" "legacy-db" "storage"]]

View File

@ -38,6 +38,7 @@
[app.storage.gc-deleted :as-alias sto.gc-deleted] [app.storage.gc-deleted :as-alias sto.gc-deleted]
[app.storage.gc-touched :as-alias sto.gc-touched] [app.storage.gc-touched :as-alias sto.gc-touched]
[app.storage.s3 :as-alias sto.s3] [app.storage.s3 :as-alias sto.s3]
[app.system :as sys]
[app.util.cron] [app.util.cron]
[app.worker :as-alias wrk] [app.worker :as-alias wrk]
[app.worker.executor] [app.worker.executor]
@ -45,10 +46,13 @@
[clojure.tools.namespace.repl :as repl] [clojure.tools.namespace.repl :as repl]
[cuerdas.core :as str] [cuerdas.core :as str]
[integrant.core :as ig] [integrant.core :as ig]
[nrepl.server :as nrepl]
[promesa.exec :as px]) [promesa.exec :as px])
(:gen-class)) (:gen-class))
(repl/disable-reload! (find-ns 'integrant.core))
(repl/disable-reload! (find-ns 'app.system))
(repl/disable-reload! (find-ns 'app.common.debug))
(def default-metrics (def default-metrics
{:update-file-changes {:update-file-changes
{::mdef/name "penpot_rpc_update_file_changes_total" {::mdef/name "penpot_rpc_update_file_changes_total"
@ -444,13 +448,17 @@
::http.client/client (ig/ref ::http.client/client) ::http.client/client (ig/ref ::http.client/client)
::setup/props (ig/ref ::setup/props)} ::setup/props (ig/ref ::setup/props)}
[::srepl/urepl ::srepl/server] ::srepl/urepl
{::srepl/port (cf/get :urepl-port 6062) {:port (cf/get :urepl-port 6062)
::srepl/host (cf/get :urepl-host "localhost")} :host (cf/get :urepl-host "localhost")}
[::srepl/prepl ::srepl/server] ::srepl/prepl
{::srepl/port (cf/get :prepl-port 6063) {:port (cf/get :prepl-port 6063)
::srepl/host (cf/get :prepl-host "localhost")} :host (cf/get :prepl-host "localhost")}
::srepl/nrepl
{:port (cf/get :nrepl-port 6064)
:host (cf/get :nrepl-host "localhost")}
::setup/templates {} ::setup/templates {}
@ -584,42 +592,70 @@
::db/pool (ig/ref ::db/pool)}}) ::db/pool (ig/ref ::db/pool)}})
(def system nil)
(defn start (defn start
[] []
(cf/validate!) (cf/validate!)
(ig/load-namespaces (merge system-config worker-config)) (ig/load-namespaces (merge system-config worker-config))
(alter-var-root #'system (fn [sys] (alter-var-root #'app.system/system
(when sys (ig/halt! sys)) (fn [sys]
(-> system-config (some-> sys not-empty ig/halt!)
(cond-> (contains? cf/flags :backend-worker) (-> system-config
(merge worker-config)) (cond-> (contains? cf/flags :backend-worker)
(ig/expand) (merge worker-config))
(ig/init)))) (ig/expand)
(ig/init))))
(l/inf :hint "welcome to penpot" (l/inf :hint "welcome to penpot"
:flags (str/join "," (map name cf/flags)) :flags (str/join "," (map name cf/flags))
:worker? (contains? cf/flags :backend-worker) :worker? (contains? cf/flags :backend-worker)
:version (:full cf/version))) :version (:full cf/version))
:start)
(defn resume
[]
(cf/validate!)
(ig/load-namespaces (merge system-config worker-config))
(alter-var-root #'app.system/system
(fn [sys]
(let [config (-> system-config
(cond-> (contains? cf/flags :backend-worker)
(merge worker-config))
(ig/expand))]
(if-let [sys (not-empty sys)]
(ig/resume config sys)
(ig/init config)))))
:resume)
(defn start-custom (defn start-custom
[config] [config]
(ig/load-namespaces config) (ig/load-namespaces config)
(alter-var-root #'system (fn [sys] (alter-var-root #'app.system/system
(when sys (ig/halt! sys)) (fn [sys]
(-> config (some-> sys not-empty ig/halt!)
(ig/expand) (-> config
(ig/init))))) (ig/expand)
(ig/init)))))
(defn stop (defn stop
[] []
(alter-var-root #'system (fn [sys] (alter-var-root #'app.system/system
(when sys (ig/halt! sys)) (fn [sys]
nil))) (some-> sys not-empty ig/halt!)
{}))
:stop)
(defn suspend
[]
(alter-var-root #'app.system/system
(fn [sys]
(some-> sys not-empty ig/suspend!)
sys))
:suspend)
(defn restart (defn restart
[] []
(stop) (suspend)
(repl/refresh :after 'app.main/start)) (repl/refresh :after 'app.main/resume))
(defn restart-all (defn restart-all
[] []
@ -644,17 +680,12 @@
(if-let [sns (namespace o)] (if-let [sns (namespace o)]
(do (require (symbol sns)) (do (require (symbol sns))
(test/test-vars [(resolve o)])) (test/test-vars [(resolve o)]))
(test/test-ns o))))) (test/test-ns o)))))
(repl/disable-reload! (find-ns 'integrant.core))
(defn -main (defn -main
[& _args] [& _args]
(try (try
(let [p (promise)] (let [p (promise)]
(l/inf :hint "start nrepl server" :port 6064)
(nrepl/start-server :bind "0.0.0.0" :port 6064)
(start) (start)
(deref p)) (deref p))
(catch Throwable cause (catch Throwable cause

View File

@ -19,7 +19,8 @@
[clojure.core :as c] [clojure.core :as c]
[clojure.core.server :as ccs] [clojure.core.server :as ccs]
[clojure.main :as cm] [clojure.main :as cm]
[integrant.core :as ig])) [integrant.core :as ig]
[nrepl.server :as nrepl]))
(defn- repl-init (defn- repl-init
[] []
@ -107,37 +108,106 @@
(finally (finally
(remove-tap tapfn)))))) (remove-tap tapfn))))))
;; --- State initialization ;; --- UREPL
(defmethod ig/assert-key ::server (defmethod ig/assert-key ::urepl
[_ params] [_ params]
(assert (int? (::port params)) "expected valid port") (assert (int? (:port params)) "expected valid port")
(assert (string? (::host params)) "expected valid host")) (assert (string? (:host params)) "expected valid host"))
(defmethod ig/expand-key ::server (defmethod ig/init-key ::urepl
[[type :as k] v] [_ {:keys [:port :host] :as cfg}]
{k (assoc v ::flag (keyword (str (name type) "-server")))}) (when (contains? cf/flags :urepl-server)
(defmethod ig/init-key ::server (l/inf :hint "init urepl server" :host host :port port)
[[type _] {:keys [::flag ::port ::host] :as cfg}] (let [accept 'app.srepl/user-repl
(when (contains? cf/flags flag)
(l/inf :hint "initializing repl server"
:name (name type)
:port port
:host host)
(let [accept (case type
::prepl 'app.srepl/json-repl
::urepl 'app.srepl/user-repl)
params {:address host params {:address host
:port port :port port
:name (name type) :name "urepl"
:accept accept}] :accept accept}]
(ccs/start-server params) (ccs/start-server params)
(assoc params :type type)))) "urepl")))
(defmethod ig/halt-key! ::server (defmethod ig/halt-key! ::urepl
[_ name]
(some-> name ccs/stop-server))
(defmethod ig/resume-key ::urepl
[key opts _ old-name]
(if old-name
(do
(l/inf :hint "keep urepl server")
old-name)
(ig/init-key key opts)))
(defmethod ig/suspend-key! ::urepl
[_ name]
(l/inf :hint "keep urepl server"))
;; --- PREPL
(defmethod ig/assert-key ::prepl
[_ params] [_ params]
(some-> params :name ccs/stop-server)) (assert (int? (:port params)) "expected valid port")
(assert (string? (:host params)) "expected valid host"))
(defmethod ig/init-key ::prepl
[_ {:keys [:port :host] :as cfg}]
(when (contains? cf/flags :prepl-server)
(l/inf :hint "init prepl server" :host host :port port)
(let [accept 'app.srepl/json-repl
params {:address host
:port port
:name "prepl"
:accept accept}]
(ccs/start-server params)
"prepl")))
(defmethod ig/halt-key! ::prepl
[_ name]
(some-> name ccs/stop-server))
(defmethod ig/resume-key ::prepl
[key opts _ old-name]
(if old-name
(do
(l/inf :hint "keep prepl server")
old-name)
(ig/init-key key opts)))
(defmethod ig/suspend-key! ::prepl
[_ name]
(l/inf :hint "keep prepl server"))
;; --- NREPL
(defmethod ig/assert-key ::nrepl
[_ params]
(assert (int? (:port params)) "expected valid port")
(assert (string? (:host params)) "expected valid host"))
(defmethod ig/init-key ::nrepl
[_ {:keys [:port :host] :as cfg}]
(when (contains? cf/flags :nrepl-server)
(l/inf :hint "init nrepl server" :host host :port port)
(nrepl/start-server :bind host :port port)))
(defmethod ig/halt-key! ::nrepl
[_ server]
(some-> server nrepl/stop-server))
(defmethod ig/resume-key ::nrepl
[key opts _ old-server]
(if old-server
(do
(l/inf :hint "keep nrepl server")
old-server)
(ig/init-key key opts)))
(defmethod ig/suspend-key! ::nrepl
[_ server]
(l/inf :hint "keep nrepl server"))

View File

@ -8,18 +8,18 @@
(:require (:require
[app.binfile.v2 :as binfile.v2] [app.binfile.v2 :as binfile.v2]
[app.db :as db] [app.db :as db]
[app.main :as main]
[app.srepl.helpers :as h] [app.srepl.helpers :as h]
[app.system :as sys]
[cuerdas.core :as str])) [cuerdas.core :as str]))
(defn export-team! (defn export-team!
[team-id] [team-id]
(let [team-id (h/parse-uuid team-id)] (let [team-id (h/parse-uuid team-id)]
(binfile.v2/export-team! main/system team-id))) (binfile.v2/export-team! sys/system team-id)))
(defn import-team! (defn import-team!
[path & {:keys [owner rollback?] :or {rollback? true}}] [path & {:keys [owner rollback?] :or {rollback? true}}]
(db/tx-run! (assoc main/system ::db/rollback rollback?) (db/tx-run! (assoc sys/system ::db/rollback rollback?)
(fn [cfg] (fn [cfg]
(let [team (binfile.v2/import-team! cfg path) (let [team (binfile.v2/import-team! cfg path)
owner (cond owner (cond

View File

@ -29,7 +29,7 @@
(defn- get-current-system (defn- get-current-system
[] []
(or (deref (requiring-resolve 'app.main/system)) (or (deref (requiring-resolve 'app.system/system))
(deref (requiring-resolve 'user/system)))) (deref (requiring-resolve 'user/system))))
(defmulti ^:private exec-command ::cmd) (defmulti ^:private exec-command ::cmd)

View File

@ -15,7 +15,7 @@
[app.common.time :as ct] [app.common.time :as ct]
[app.db :as db] [app.db :as db]
[app.features.file-snapshots :as fsnap] [app.features.file-snapshots :as fsnap]
[app.main :as main])) [app.system :as sys]))
(def ^:dynamic *system* nil) (def ^:dynamic *system* nil)
@ -37,13 +37,13 @@
(defn get-file (defn get-file
"Get the migrated data of one file." "Get the migrated data of one file."
([id] ([id]
(get-file (or *system* main/system) id)) (get-file (or *system* sys/system) id))
([system id] ([system id]
(db/run! system bfc/get-file id))) (db/run! system bfc/get-file id)))
(defn get-raw-file (defn get-raw-file
"Get the migrated data of one file." "Get the migrated data of one file."
([id] (get-raw-file (or *system* main/system) id)) ([id] (get-raw-file (or *system* sys/system) id))
([system id] ([system id]
(db/run! system (db/run! system
(fn [system] (fn [system]

View File

@ -27,7 +27,6 @@
[app.features.file-snapshots :as fsnap] [app.features.file-snapshots :as fsnap]
[app.http.session :as session] [app.http.session :as session]
[app.loggers.audit :as audit] [app.loggers.audit :as audit]
[app.main :as main]
[app.msgbus :as mbus] [app.msgbus :as mbus]
[app.rpc.commands.auth :as auth] [app.rpc.commands.auth :as auth]
[app.rpc.commands.files :as files] [app.rpc.commands.files :as files]
@ -37,6 +36,7 @@
[app.rpc.commands.teams :as teams] [app.rpc.commands.teams :as teams]
[app.srepl.helpers :as h] [app.srepl.helpers :as h]
[app.srepl.procs.file-repair :as procs.file-repair] [app.srepl.procs.file-repair :as procs.file-repair]
[app.system :as sys]
[app.util.blob :as blob] [app.util.blob :as blob]
[app.util.pointer-map :as pmap] [app.util.pointer-map :as pmap]
[app.worker :as wrk] [app.worker :as wrk]
@ -58,14 +58,14 @@
(defn print-tasks (defn print-tasks
[] []
(let [tasks (:app.worker/registry main/system)] (let [tasks (:app.worker/registry sys/system)]
(pp/pprint (keys tasks) :level 200))) (pp/pprint (keys tasks) :level 200)))
(defn run-task! (defn run-task!
([tname] ([tname]
(run-task! tname {})) (run-task! tname {}))
([tname params] ([tname params]
(wrk/invoke! (-> main/system (wrk/invoke! (-> sys/system
(assoc ::wrk/task tname) (assoc ::wrk/task tname)
(assoc ::wrk/params params))))) (assoc ::wrk/params params)))))
@ -73,14 +73,14 @@
([name] ([name]
(schedule-task! name {})) (schedule-task! name {}))
([name params] ([name params]
(wrk/submit! (-> main/system (wrk/submit! (-> sys/system
(assoc ::wrk/task name) (assoc ::wrk/task name)
(assoc ::wrk/params params))))) (assoc ::wrk/params params)))))
(defn send-test-email! (defn send-test-email!
[destination] [destination]
(assert (string? destination) "destination should be provided") (assert (string? destination) "destination should be provided")
(-> main/system (-> sys/system
(assoc ::wrk/task :sendmail) (assoc ::wrk/task :sendmail)
(assoc ::wrk/params {:body "test email" (assoc ::wrk/params {:body "test email"
:subject "test email" :subject "test email"
@ -89,7 +89,7 @@
(defn resend-email-verification-email! (defn resend-email-verification-email!
[email] [email]
(db/tx-run! main/system (db/tx-run! sys/system
(fn [{:keys [::db/conn] :as cfg}] (fn [{:keys [::db/conn] :as cfg}]
(let [email (profile/clean-email email) (let [email (profile/clean-email email)
profile (profile/get-profile-by-email conn email)] profile (profile/get-profile-by-email conn email)]
@ -103,7 +103,7 @@
"Mark the profile blocked and removes all the http sessiones "Mark the profile blocked and removes all the http sessiones
associated with the profile-id." associated with the profile-id."
[email] [email]
(some-> main/system (some-> sys/system
(db/tx-run! (db/tx-run!
(fn [{:keys [::db/conn] :as system}] (fn [{:keys [::db/conn] :as system}]
(when-let [profile (db/get* conn :profile (when-let [profile (db/get* conn :profile
@ -117,7 +117,7 @@
"Mark the profile blocked and removes all the http sessiones "Mark the profile blocked and removes all the http sessiones
associated with the profile-id." associated with the profile-id."
[email] [email]
(some-> main/system (some-> sys/system
(db/tx-run! (db/tx-run!
(fn [{:keys [::db/conn] :as system}] (fn [{:keys [::db/conn] :as system}]
(when-let [profile (db/get* conn :profile (when-let [profile (db/get* conn :profile
@ -135,7 +135,7 @@
(assert (string? email) "expected email") (assert (string? email) "expected email")
(assert (string? password) "expected password") (assert (string? password) "expected password")
(some-> main/system (some-> sys/system
(db/tx-run! (db/tx-run!
(fn [{:keys [::db/conn] :as system}] (fn [{:keys [::db/conn] :as system}]
(let [password (derive-password password) (let [password (derive-password password)
@ -156,7 +156,7 @@
:hint (str "feature '" feature "' not supported"))) :hint (str "feature '" feature "' not supported")))
(let [team-id (h/parse-uuid team-id)] (let [team-id (h/parse-uuid team-id)]
(db/tx-run! main/system (db/tx-run! sys/system
(fn [{:keys [::db/conn]}] (fn [{:keys [::db/conn]}]
(let [team (-> (db/get conn :team {:id team-id}) (let [team (-> (db/get conn :team {:id team-id})
(update :features db/decode-pgarray #{})) (update :features db/decode-pgarray #{}))
@ -175,7 +175,7 @@
:hint (str "feature '" feature "' not supported"))) :hint (str "feature '" feature "' not supported")))
(let [team-id (h/parse-uuid team-id)] (let [team-id (h/parse-uuid team-id)]
(db/tx-run! main/system (db/tx-run! sys/system
(fn [{:keys [::db/conn]}] (fn [{:keys [::db/conn]}]
(let [team (-> (db/get conn :team {:id team-id}) (let [team (-> (db/get conn :team {:id team-id})
(update :features db/decode-pgarray #{})) (update :features db/decode-pgarray #{}))
@ -216,7 +216,7 @@
:code :incorrect-level :code :incorrect-level
:hint (str "level '" level "' not supported"))) :hint (str "level '" level "' not supported")))
(let [{:keys [::mbus/msgbus ::db/pool]} main/system (let [{:keys [::mbus/msgbus ::db/pool]} sys/system
send send
(fn [dest] (fn [dest]
@ -321,7 +321,7 @@
collectable file-changes entry." collectable file-changes entry."
[& {:keys [file-id label]}] [& {:keys [file-id label]}]
(let [file-id (h/parse-uuid file-id)] (let [file-id (h/parse-uuid file-id)]
(db/tx-run! main/system (db/tx-run! sys/system
(fn [cfg] (fn [cfg]
(let [file (bfc/get-file cfg file-id :realize? true)] (let [file (bfc/get-file cfg file-id :realize? true)]
(fsnap/create! cfg file {:label label :created-by "admin"})))))) (fsnap/create! cfg file {:label label :created-by "admin"}))))))
@ -330,7 +330,7 @@
[file-id & {:keys [label id]}] [file-id & {:keys [label id]}]
(let [file-id (h/parse-uuid file-id) (let [file-id (h/parse-uuid file-id)
snapshot-id (some-> id h/parse-uuid)] snapshot-id (some-> id h/parse-uuid)]
(db/tx-run! main/system (db/tx-run! sys/system
(fn [{:keys [::db/conn] :as system}] (fn [{:keys [::db/conn] :as system}]
(cond (cond
(uuid? snapshot-id) (uuid? snapshot-id)
@ -348,7 +348,7 @@
(defn list-file-snapshots! (defn list-file-snapshots!
[file-id & {:as _}] [file-id & {:as _}]
(let [file-id (h/parse-uuid file-id)] (let [file-id (h/parse-uuid file-id)]
(db/tx-run! main/system (db/tx-run! sys/system
(fn [cfg] (fn [cfg]
(->> (fsnap/get-visible-snapshots cfg file-id) (->> (fsnap/get-visible-snapshots cfg file-id)
(print-table [:label :id :revn :created-at :created-by])))))) (print-table [:label :id :revn :created-at :created-by]))))))
@ -356,7 +356,7 @@
(defn take-team-snapshot! (defn take-team-snapshot!
[team-id & {:keys [label rollback?] :or {rollback? true}}] [team-id & {:keys [label rollback?] :or {rollback? true}}]
(let [team-id (h/parse-uuid team-id)] (let [team-id (h/parse-uuid team-id)]
(-> (assoc main/system ::db/rollback rollback?) (-> (assoc sys/system ::db/rollback rollback?)
(db/tx-run! h/take-team-snapshot! team-id label)))) (db/tx-run! h/take-team-snapshot! team-id label))))
(defn restore-team-snapshot! (defn restore-team-snapshot!
@ -364,7 +364,7 @@
exists for all files; if is not the case, an exception is raised." exists for all files; if is not the case, an exception is raised."
[team-id label & {:keys [rollback?] :or {rollback? true}}] [team-id label & {:keys [rollback?] :or {rollback? true}}]
(let [team-id (h/parse-uuid team-id)] (let [team-id (h/parse-uuid team-id)]
(-> (assoc main/system ::db/rollback rollback?) (-> (assoc sys/system ::db/rollback rollback?)
(db/tx-run! h/restore-team-snapshot! team-id label)))) (db/tx-run! h/restore-team-snapshot! team-id label))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -376,7 +376,7 @@
all contents of a file. Returns a list of errors." all contents of a file. Returns a list of errors."
[file-id] [file-id]
(let [file-id (h/parse-uuid file-id)] (let [file-id (h/parse-uuid file-id)]
(db/tx-run! (assoc main/system ::db/rollback true) (db/tx-run! (assoc sys/system ::db/rollback true)
(fn [system] (fn [system]
(let [file (bfc/get-file system file-id) (let [file (bfc/get-file system file-id)
libs (bfc/get-resolved-file-libraries system file)] libs (bfc/get-resolved-file-libraries system file)]
@ -387,7 +387,7 @@
all contents of a file. Returns a list of errors." all contents of a file. Returns a list of errors."
[file-id] [file-id]
(let [file-id (h/parse-uuid file-id)] (let [file-id (h/parse-uuid file-id)]
(db/tx-run! (assoc main/system ::db/rollback true) (db/tx-run! (assoc sys/system ::db/rollback true)
(fn [system] (fn [system]
(try (try
(let [file (bfc/get-file system file-id)] (let [file (bfc/get-file system file-id)]
@ -405,7 +405,7 @@
(defn repair-file! (defn repair-file!
"Repair the list of errors detected by validation." "Repair the list of errors detected by validation."
[file-id & {:keys [rollback?] :or {rollback? true} :as options}] [file-id & {:keys [rollback?] :or {rollback? true} :as options}]
(let [system (assoc main/system ::db/rollback rollback?) (let [system (assoc sys/system ::db/rollback rollback?)
file-id (h/parse-uuid file-id) file-id (h/parse-uuid file-id)
options (assoc options ::h/with-libraries? true)] options (assoc options ::h/with-libraries? true)]
(db/tx-run! system h/process-file! file-id procs.file-repair/repair-file options))) (db/tx-run! system h/process-file! file-id procs.file-repair/repair-file options)))
@ -415,7 +415,7 @@
The function receives the decoded and migrated file data." The function receives the decoded and migrated file data."
[file-id update-fn & {:keys [rollback?] :or {rollback? true} :as opts}] [file-id update-fn & {:keys [rollback?] :or {rollback? true} :as opts}]
(let [file-id (h/parse-uuid file-id)] (let [file-id (h/parse-uuid file-id)]
(db/tx-run! (assoc main/system ::db/rollback rollback?) (db/tx-run! (assoc sys/system ::db/rollback rollback?)
(fn [system] (fn [system]
(binding [h/*system* system (binding [h/*system* system
db/*conn* (db/get-connection system)] db/*conn* (db/get-connection system)]
@ -461,7 +461,7 @@
(when-let [[index item] (sp/<! in-ch)] (when-let [[index item] (sp/<! in-ch)]
(l/dbg :hint "process item" :worker-id worker-id :index index :item item) (l/dbg :hint "process item" :worker-id worker-id :index index :item item)
(try (try
(-> main/system (-> sys/system
(assoc ::db/rollback rollback?) (assoc ::db/rollback rollback?)
(db/tx-run! (fn [system] (db/tx-run! (fn [system]
(binding [h/*system* system (binding [h/*system* system
@ -506,7 +506,7 @@
(doall))] (doall))]
(try (try
(db/tx-run! main/system process-items) (db/tx-run! sys/system process-items)
;; Await threads termination ;; Await threads termination
(doseq [thread threads] (doseq [thread threads]
@ -535,7 +535,7 @@
(defn mark-file-as-trimmed (defn mark-file-as-trimmed
[id] [id]
(let [id (h/parse-uuid id)] (let [id (h/parse-uuid id)]
(db/tx-run! main/system (fn [cfg] (db/tx-run! sys/system (fn [cfg]
(-> (db/update! cfg :file (-> (db/update! cfg :file
{:has-media-trimmed true} {:has-media-trimmed true}
{:id id} {:id id}
@ -553,14 +553,14 @@
(let [file-id (h/parse-uuid file-id) (let [file-id (h/parse-uuid file-id)
tnow (ct/now)] tnow (ct/now)]
(audit/insert main/system (audit/insert sys/system
{:name "delete-file" {:name "delete-file"
:type "action" :type "action"
:props {:id file-id} :props {:id file-id}
:context {:triggered-by "srepl" :context {:triggered-by "srepl"
:cause "explicit call to delete-file!"} :cause "explicit call to delete-file!"}
:tracked-at tnow}) :tracked-at tnow})
(wrk/invoke! (-> main/system (wrk/invoke! (-> sys/system
(assoc ::wrk/task :delete-object) (assoc ::wrk/task :delete-object)
(assoc ::wrk/params {:object :file (assoc ::wrk/params {:object :file
:deleted-at tnow :deleted-at tnow
@ -571,7 +571,7 @@
"Mark a file and all related objects as not deleted" "Mark a file and all related objects as not deleted"
[file-id] [file-id]
(let [file-id (h/parse-uuid file-id)] (let [file-id (h/parse-uuid file-id)]
(db/tx-run! main/system (db/tx-run! sys/system
(fn [{:keys [::db/conn] :as system}] (fn [{:keys [::db/conn] :as system}]
(when-let [file (db/get* system :file (when-let [file (db/get* system :file
{:id file-id} {:id file-id}
@ -593,7 +593,7 @@
(let [project-id (h/parse-uuid project-id) (let [project-id (h/parse-uuid project-id)
tnow (ct/now)] tnow (ct/now)]
(audit/insert main/system (audit/insert sys/system
{:name "delete-project" {:name "delete-project"
:type "action" :type "action"
:props {:id project-id} :props {:id project-id}
@ -601,7 +601,7 @@
:cause "explicit call to delete-project!"} :cause "explicit call to delete-project!"}
:tracked-at tnow}) :tracked-at tnow})
(wrk/invoke! (-> main/system (wrk/invoke! (-> sys/system
(assoc ::wrk/task :delete-object) (assoc ::wrk/task :delete-object)
(assoc ::wrk/params {:object :project (assoc ::wrk/params {:object :project
:deleted-at tnow :deleted-at tnow
@ -625,7 +625,7 @@
"Mark a project and all related objects as not deleted" "Mark a project and all related objects as not deleted"
[project-id] [project-id]
(let [project-id (h/parse-uuid project-id)] (let [project-id (h/parse-uuid project-id)]
(db/tx-run! main/system (db/tx-run! sys/system
(fn [system] (fn [system]
(when-let [project (db/get* system :project (when-let [project (db/get* system :project
{:id project-id} {:id project-id}
@ -645,7 +645,7 @@
(let [team-id (h/parse-uuid team-id) (let [team-id (h/parse-uuid team-id)
tnow (ct/now)] tnow (ct/now)]
(audit/insert main/system (audit/insert sys/system
{:name "delete-team" {:name "delete-team"
:type "action" :type "action"
:props {:id team-id} :props {:id team-id}
@ -653,7 +653,7 @@
:cause "explicit call to delete-profile!"} :cause "explicit call to delete-profile!"}
:tracked-at tnow}) :tracked-at tnow})
(wrk/invoke! (-> main/system (wrk/invoke! (-> sys/system
(assoc ::wrk/task :delete-object) (assoc ::wrk/task :delete-object)
(assoc ::wrk/params {:object :team (assoc ::wrk/params {:object :team
:deleted-at tnow :deleted-at tnow
@ -681,7 +681,7 @@
"Mark a team and all related objects as not deleted" "Mark a team and all related objects as not deleted"
[team-id] [team-id]
(let [team-id (h/parse-uuid team-id)] (let [team-id (h/parse-uuid team-id)]
(db/tx-run! main/system (db/tx-run! sys/system
(fn [system] (fn [system]
(when-let [team (some-> (db/get* system :team (when-let [team (some-> (db/get* system :team
{:id team-id} {:id team-id}
@ -702,14 +702,14 @@
(let [profile-id (h/parse-uuid profile-id) (let [profile-id (h/parse-uuid profile-id)
tnow (ct/now)] tnow (ct/now)]
(audit/insert main/system (audit/insert sys/system
{:name "delete-profile" {:name "delete-profile"
:type "action" :type "action"
:context {:triggered-by "srepl" :context {:triggered-by "srepl"
:cause "explicit call to delete-profile!"} :cause "explicit call to delete-profile!"}
:tracked-at tnow}) :tracked-at tnow})
(wrk/invoke! (-> main/system (wrk/invoke! (-> sys/system
(assoc ::wrk/task :delete-object) (assoc ::wrk/task :delete-object)
(assoc ::wrk/params {:object :profile (assoc ::wrk/params {:object :profile
:deleted-at tnow :deleted-at tnow
@ -720,7 +720,7 @@
"Mark a team and all related objects as not deleted" "Mark a team and all related objects as not deleted"
[profile-id] [profile-id]
(let [profile-id (h/parse-uuid profile-id)] (let [profile-id (h/parse-uuid profile-id)]
(db/tx-run! main/system (db/tx-run! sys/system
(fn [system] (fn [system]
(when-let [profile (some-> (db/get* system :profile (when-let [profile (some-> (db/get* system :profile
{:id profile-id} {:id profile-id}
@ -793,9 +793,9 @@
(defn process-deleted-profiles-cascade (defn process-deleted-profiles-cascade
[] []
(->> (db/exec! main/system ["select id, deleted_at from profile where deleted_at is not null"]) (->> (db/exec! sys/system ["select id, deleted_at from profile where deleted_at is not null"])
(run! (fn [{:keys [id deleted-at]}] (run! (fn [{:keys [id deleted-at]}]
(wrk/invoke! (-> main/system (wrk/invoke! (-> sys/system
(assoc ::wrk/task :delete-object) (assoc ::wrk/task :delete-object)
(assoc ::wrk/params {:object :profile (assoc ::wrk/params {:object :profile
:deleted-at deleted-at :deleted-at deleted-at
@ -803,9 +803,9 @@
(defn process-deleted-teams-cascade (defn process-deleted-teams-cascade
[] []
(->> (db/exec! main/system ["select id, deleted_at from team where deleted_at is not null"]) (->> (db/exec! sys/system ["select id, deleted_at from team where deleted_at is not null"])
(run! (fn [{:keys [id deleted-at]}] (run! (fn [{:keys [id deleted-at]}]
(wrk/invoke! (-> main/system (wrk/invoke! (-> sys/system
(assoc ::wrk/task :delete-object) (assoc ::wrk/task :delete-object)
(assoc ::wrk/params {:object :team (assoc ::wrk/params {:object :team
:deleted-at deleted-at :deleted-at deleted-at
@ -813,9 +813,9 @@
(defn process-deleted-projects-cascade (defn process-deleted-projects-cascade
[] []
(->> (db/exec! main/system ["select id, deleted_at from project where deleted_at is not null"]) (->> (db/exec! sys/system ["select id, deleted_at from project where deleted_at is not null"])
(run! (fn [{:keys [id deleted-at]}] (run! (fn [{:keys [id deleted-at]}]
(wrk/invoke! (-> main/system (wrk/invoke! (-> sys/system
(assoc ::wrk/task :delete-object) (assoc ::wrk/task :delete-object)
(assoc ::wrk/params {:object :project (assoc ::wrk/params {:object :project
:deleted-at deleted-at :deleted-at deleted-at
@ -823,9 +823,9 @@
(defn process-deleted-files-cascade (defn process-deleted-files-cascade
[] []
(->> (db/exec! main/system ["select id, deleted_at from file where deleted_at is not null"]) (->> (db/exec! sys/system ["select id, deleted_at from file where deleted_at is not null"])
(run! (fn [{:keys [id deleted-at]}] (run! (fn [{:keys [id deleted-at]}]
(wrk/invoke! (-> main/system (wrk/invoke! (-> sys/system
(assoc ::wrk/task :delete-object) (assoc ::wrk/task :delete-object)
(assoc ::wrk/params {:object :file (assoc ::wrk/params {:object :file
:deleted-at deleted-at :deleted-at deleted-at
@ -842,7 +842,7 @@
(assert (string? client-id) "expected a valid client-id") (assert (string? client-id) "expected a valid client-id")
(assert (string? client-secret) "expected a valid client-secret") (assert (string? client-secret) "expected a valid client-secret")
(assert (string? domain) "expected a valid domain") (assert (string? domain) "expected a valid domain")
(db/insert! main/system :sso-provider (db/insert! sys/system :sso-provider
{:id (uuid/next) {:id (uuid/next)
:type "oidc" :type "oidc"
:client-id client-id :client-id client-id
@ -856,7 +856,7 @@
(defn decode-session-token (defn decode-session-token
[token] [token]
(session/decode-token main/system token)) (session/decode-token sys/system token))
(defn instrument-var (defn instrument-var
[var] [var]
@ -881,7 +881,7 @@
(defn duplicate-team (defn duplicate-team
[team-id & {:keys [name]}] [team-id & {:keys [name]}]
(let [team-id (h/parse-uuid team-id)] (let [team-id (h/parse-uuid team-id)]
(db/tx-run! main/system (db/tx-run! sys/system
(fn [{:keys [::db/conn] :as cfg}] (fn [{:keys [::db/conn] :as cfg}]
(db/exec-one! conn ["SET CONSTRAINTS ALL DEFERRED"]) (db/exec-one! conn ["SET CONSTRAINTS ALL DEFERRED"])
(let [team (-> (assoc cfg ::bfc/timestamp (ct/now)) (let [team (-> (assoc cfg ::bfc/timestamp (ct/now))

View File

@ -0,0 +1,9 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC Sucursal en España SL
(ns app.system)
(defonce system nil)