Merge pull request #4145 from penpot/niwinz-staging-tmp

 Minor improvements on TMP storage API
This commit is contained in:
Alejandro 2024-02-19 07:16:36 +01:00 committed by GitHub
commit 7ac4b89a0e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 59 additions and 47 deletions

View file

@ -44,7 +44,7 @@
[clojure.walk :refer [macroexpand-all]] [clojure.walk :refer [macroexpand-all]]
[criterium.core :as crit] [criterium.core :as crit]
[cuerdas.core :as str] [cuerdas.core :as str]
[datoteka.core] [datoteka.fs :as fs]
[integrant.core :as ig] [integrant.core :as ig]
[malli.core :as m] [malli.core :as m]
[malli.dev.pretty :as mdp] [malli.dev.pretty :as mdp]

View file

@ -1524,21 +1524,9 @@
(cfv/validate-file! file libs) (cfv/validate-file! file libs)
(cfv/validate-file-schema! file)) (cfv/validate-file-schema! file))
(defn- process-file (defn- persist-file!
[{:keys [::db/conn] :as system} {:keys [id] :as file} & {:keys [validate?]}] [{:keys [::db/conn] :as system} {:keys [id] :as file}]
(let [libs (->> (files/get-file-libraries conn id) (let [file (if (contains? (:features file) "fdata/objects-map")
(into [file] (comp (map :id)
(map (partial get-file system))))
(d/index-by :id))
file (-> file
(update :data migrate-fdata libs)
(update :features conj "components/v2"))
_ (when validate?
(validate-file! file libs))
file (if (contains? (:features file) "fdata/objects-map")
(fdata/enable-objects-map file) (fdata/enable-objects-map file)
file) file)
@ -1547,15 +1535,32 @@
(let [file (fdata/enable-pointer-map file)] (let [file (fdata/enable-pointer-map file)]
(fdata/persist-pointers! system id) (fdata/persist-pointers! system id)
file)) file))
file)] file)
;; Ensure all files has :data with id
file (update file :data assoc :id id)]
(db/update! conn :file (db/update! conn :file
{:data (blob/encode (:data file)) {:data (blob/encode (:data file))
:features (db/create-array conn "text" (:features file)) :features (db/create-array conn "text" (:features file))
:revn (:revn file)} :revn (:revn file)}
{:id (:id file)}) {:id (:id file)})))
(dissoc file :data))) (defn- process-file!
[{:keys [::db/conn] :as system} {:keys [id] :as file} & {:keys [validate?]}]
(let [libs (->> (files/get-file-libraries conn id)
(into [file] (comp (map :id)
(map (partial get-file system))))
(d/index-by :id))
file (-> file
(update :data migrate-fdata libs)
(update :features conj "components/v2"))]
(when validate?
(validate-file! file libs))
file))
(def ^:private sql:get-and-lock-team-files (def ^:private sql:get-and-lock-team-files
"SELECT f.id "SELECT f.id
@ -1605,13 +1610,15 @@
(when (string? label) (when (string? label)
(fsnap/take-file-snapshot! system {:file-id file-id (fsnap/take-file-snapshot! system {:file-id file-id
:label (str "migration/" label)})) :label (str "migration/" label)}))
(let [file (get-file system file-id)] (let [file (get-file system file-id)
file (process-file! system file :validate? validate?)]
(events/tap :progress (events/tap :progress
{:op :migrate-file {:op :migrate-file
:name (:name file) :name (:name file)
:id (:id file)}) :id (:id file)})
(process-file system file :validate? validate?))))) (persist-file! system file)))))
(catch Throwable cause (catch Throwable cause
(vreset! err true) (vreset! err true)

View file

@ -19,6 +19,8 @@
[promesa.exec :as px] [promesa.exec :as px]
[promesa.exec.csp :as sp])) [promesa.exec.csp :as sp]))
(def default-tmp-dir "/tmp/penpot")
(declare ^:private remove-temp-file) (declare ^:private remove-temp-file)
(declare ^:private io-loop) (declare ^:private io-loop)
@ -33,6 +35,7 @@
(defmethod ig/init-key ::cleaner (defmethod ig/init-key ::cleaner
[_ cfg] [_ cfg]
(fs/create-dir default-tmp-dir)
(px/fn->thread (partial io-loop cfg) (px/fn->thread (partial io-loop cfg)
{:name "penpot/storage/tmp-cleaner" :virtual true})) {:name "penpot/storage/tmp-cleaner" :virtual true}))
@ -70,18 +73,14 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn tempfile (defn tempfile
"Returns a tmpfile candidate (without creating it)"
[& {:keys [suffix prefix min-age] [& {:keys [suffix prefix min-age]
:or {prefix "penpot." :or {prefix "penpot."
suffix ".tmp"}}] suffix ".tmp"}}]
(let [path (fs/tempfile :suffix suffix :prefix prefix)] (let [path (fs/create-tempfile
(sp/offer! queue [path (some-> min-age dt/duration)]) :perms "rw-r--r--"
path)) :dir default-tmp-dir
:suffix suffix
(defn create-tempfile :prefix prefix)]
[& {:keys [suffix prefix min-age] (fs/delete-on-exit! path)
:or {prefix "penpot."
suffix ".tmp"}}]
(let [path (fs/create-tempfile :suffix suffix :prefix prefix)]
(sp/offer! queue [path (some-> min-age dt/duration)]) (sp/offer! queue [path (some-> min-age dt/duration)])
path)) path))

View file

@ -38,7 +38,7 @@
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[clojure.test :as t] [clojure.test :as t]
[cuerdas.core :as str] [cuerdas.core :as str]
[datoteka.core :as fs] [datoteka.fs :as fs]
[environ.core :refer [env]] [environ.core :refer [env]]
[expound.alpha :as expound] [expound.alpha :as expound]
[integrant.core :as ig] [integrant.core :as ig]
@ -127,6 +127,8 @@
app.auth/verify-password (fn [a b] {:valid (= a b)}) app.auth/verify-password (fn [a b] {:valid (= a b)})
app.common.features/get-enabled-features (fn [& _] app.common.features/supported-features)] app.common.features/get-enabled-features (fn [& _] app.common.features/supported-features)]
(fs/create-dir "/tmp/penpot")
(let [templates [{:id "test" (let [templates [{:id "test"
:name "test" :name "test"
:file-uri "test" :file-uri "test"
@ -191,6 +193,7 @@
(let [path (fs/path "/tmp/penpot")] (let [path (fs/path "/tmp/penpot")]
(when (fs/exists? path) (when (fs/exists? path)
(fs/delete (fs/path "/tmp/penpot"))) (fs/delete (fs/path "/tmp/penpot")))
(fs/create-dir "/tmp/penpot")
(next))) (next)))
(defn serial (defn serial
@ -496,7 +499,7 @@
(defn tempfile (defn tempfile
[source] [source]
(let [rsc (io/resource source) (let [rsc (io/resource source)
tmp (fs/create-tempfile)] tmp (fs/create-tempfile :dir "/tmp/penpot" :prefix "test-")]
(io/copy (io/file rsc) (io/copy (io/file rsc)
(io/file tmp)) (io/file tmp))
tmp)) tmp))

View file

@ -17,7 +17,7 @@
[app.util.time :as dt] [app.util.time :as dt]
[backend-tests.helpers :as th] [backend-tests.helpers :as th]
[clojure.test :as t] [clojure.test :as t]
[datoteka.core :as fs] [datoteka.fs :as fs]
[mockery.core :refer [with-mocks]])) [mockery.core :refer [with-mocks]]))
(t/use-fixtures :once th/state-init) (t/use-fixtures :once th/state-init)

View file

@ -15,7 +15,7 @@
[backend-tests.helpers :as th] [backend-tests.helpers :as th]
[backend-tests.storage-test :refer [configure-storage-backend]] [backend-tests.storage-test :refer [configure-storage-backend]]
[clojure.test :as t] [clojure.test :as t]
[datoteka.core :as fs])) [datoteka.fs :as fs]))
(t/use-fixtures :once th/state-init) (t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset) (t/use-fixtures :each th/database-reset)

View file

@ -21,7 +21,7 @@
[clojure.java.io :as io] [clojure.java.io :as io]
[clojure.test :as t] [clojure.test :as t]
[cuerdas.core :as str] [cuerdas.core :as str]
[datoteka.core :as fs] [datoteka.fs :as fs]
[mockery.core :refer [with-mocks]])) [mockery.core :refer [with-mocks]]))
(t/use-fixtures :once th/state-init) (t/use-fixtures :once th/state-init)

View file

@ -16,7 +16,7 @@
[backend-tests.storage-test :refer [configure-storage-backend]] [backend-tests.storage-test :refer [configure-storage-backend]]
[buddy.core.bytes :as b] [buddy.core.bytes :as b]
[clojure.test :as t] [clojure.test :as t]
[datoteka.core :as fs])) [datoteka.fs :as fs]))
(t/use-fixtures :once th/state-init) (t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset) (t/use-fixtures :each th/database-reset)

View file

@ -12,7 +12,7 @@
[app.storage :as sto] [app.storage :as sto]
[backend-tests.helpers :as th] [backend-tests.helpers :as th]
[clojure.test :as t] [clojure.test :as t]
[datoteka.core :as fs])) [datoteka.fs :as fs]))
(t/use-fixtures :once th/state-init) (t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset) (t/use-fixtures :each th/database-reset)

View file

@ -18,7 +18,7 @@
[clojure.java.io :as io] [clojure.java.io :as io]
[clojure.test :as t] [clojure.test :as t]
[cuerdas.core :as str] [cuerdas.core :as str]
[datoteka.core :as fs] [datoteka.fs :as fs]
[mockery.core :refer [with-mocks]])) [mockery.core :refer [with-mocks]]))
;; TODO: profile deletion with teams ;; TODO: profile deletion with teams

View file

@ -14,7 +14,7 @@
[app.rpc.quotes :as-alias quotes] [app.rpc.quotes :as-alias quotes]
[backend-tests.helpers :as th] [backend-tests.helpers :as th]
[clojure.test :as t] [clojure.test :as t]
[datoteka.core :as fs] [datoteka.fs :as fs]
[mockery.core :refer [with-mocks]])) [mockery.core :refer [with-mocks]]))
(t/use-fixtures :once th/state-init) (t/use-fixtures :once th/state-init)

View file

@ -16,7 +16,7 @@
[app.util.time :as dt] [app.util.time :as dt]
[backend-tests.helpers :as th] [backend-tests.helpers :as th]
[clojure.test :as t] [clojure.test :as t]
[datoteka.core :as fs] [datoteka.fs :as fs]
[mockery.core :refer [with-mocks]])) [mockery.core :refer [with-mocks]]))
(t/use-fixtures :once th/state-init) (t/use-fixtures :once th/state-init)

View file

@ -11,7 +11,7 @@
[app.rpc :as-alias rpc] [app.rpc :as-alias rpc]
[backend-tests.helpers :as th] [backend-tests.helpers :as th]
[clojure.test :as t] [clojure.test :as t]
[datoteka.core :as fs])) [datoteka.fs :as fs]))
(t/use-fixtures :once th/state-init) (t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset) (t/use-fixtures :each th/database-reset)

View file

@ -15,7 +15,7 @@
[backend-tests.helpers :as th] [backend-tests.helpers :as th]
[clojure.test :as t] [clojure.test :as t]
[cuerdas.core :as str] [cuerdas.core :as str]
[datoteka.core :as fs] [datoteka.fs :as fs]
[datoteka.io :as io] [datoteka.io :as io]
[mockery.core :refer [with-mocks]])) [mockery.core :refer [with-mocks]]))

View file

@ -32,11 +32,14 @@
funcool/tubax {:mvn/version "2021.05.20-0"} funcool/tubax {:mvn/version "2021.05.20-0"}
funcool/cuerdas {:mvn/version "2023.11.09-407"} funcool/cuerdas {:mvn/version "2023.11.09-407"}
funcool/promesa {:git/sha "0c5ed6ad033515a2df4b55addea044f60e9653d0" funcool/promesa
{:git/sha "0c5ed6ad033515a2df4b55addea044f60e9653d0"
:git/url "https://github.com/funcool/promesa"} :git/url "https://github.com/funcool/promesa"}
funcool/datoteka {:mvn/version "3.0.66" funcool/datoteka
:exclusions [funcool/promesa]} {:git/sha "5ac3781"
:git/tag "3.0.0"
:git/url "https://github.com/funcool/datoteka"}
lambdaisland/uri {:mvn/version "1.16.134" lambdaisland/uri {:mvn/version "1.16.134"
:exclusions [org.clojure/data.json]} :exclusions [org.clojure/data.json]}