mirror of
https://github.com/penpot/penpot.git
synced 2025-03-27 23:21:47 -05:00
♻️ Refactor error handling on exporter browser module.
This commit is contained in:
parent
bf51e3db60
commit
0be2b2791f
9 changed files with 106 additions and 64 deletions
|
@ -6,14 +6,17 @@
|
|||
|
||||
(ns app.browser
|
||||
(:require
|
||||
["puppeteer-core" :as pp]
|
||||
["generic-pool" :as gp]
|
||||
["puppeteer-core" :as pp]
|
||||
[app.common.data :as d]
|
||||
[app.common.logging :as l]
|
||||
[app.common.uuid :as uuid]
|
||||
[app.config :as cf]
|
||||
[lambdaisland.glogi :as log]
|
||||
[promesa.core :as p]))
|
||||
|
||||
|
||||
(l/set-level! :trace)
|
||||
|
||||
;; --- BROWSER API
|
||||
|
||||
(def default-timeout 30000)
|
||||
|
@ -107,18 +110,18 @@
|
|||
(-> (pp/launch #js {:executablePath path :args #js ["--no-sandbox" "--font-render-hinting=none"]})
|
||||
(p/then (fn [browser]
|
||||
(let [id (deref pool-browser-id)]
|
||||
(log/info :origin "factory" :action "create" :browser-id id)
|
||||
(l/info :origin "factory" :action "create" :browser-id id)
|
||||
(unchecked-set browser "__id" id)
|
||||
(swap! pool-browser-id inc)
|
||||
browser))))))
|
||||
(destroy [obj]
|
||||
(let [id (unchecked-get obj "__id")]
|
||||
(log/info :origin "factory" :action "destroy" :browser-id id)
|
||||
(l/info :origin "factory" :action "destroy" :browser-id id)
|
||||
(.close ^js obj)))
|
||||
|
||||
(validate [obj]
|
||||
(let [id (unchecked-get obj "__id")]
|
||||
(log/info :origin "factory" :action "validate" :browser-id id :obj obj)
|
||||
(l/info :origin "factory" :action "validate" :browser-id id :obj obj)
|
||||
(p/resolved (.isConnected ^js obj))))]
|
||||
|
||||
#js {:create create
|
||||
|
@ -127,7 +130,7 @@
|
|||
|
||||
(defn init
|
||||
[]
|
||||
(log/info :msg "initializing browser pool")
|
||||
(l/info :msg "initializing browser pool")
|
||||
(let [opts #js {:max (cf/get :browser-pool-max 3)
|
||||
:min (cf/get :browser-pool-min 0)
|
||||
:testOnBorrow true
|
||||
|
@ -142,34 +145,74 @@
|
|||
(defn stop
|
||||
[]
|
||||
(when-let [pool (deref pool)]
|
||||
(log/info :msg "finalizing browser pool")
|
||||
(l/info :msg "finalizing browser pool")
|
||||
(-> (.drain ^js pool)
|
||||
(p/then (fn [] (.clear ^js pool))))))
|
||||
|
||||
(defn exec!
|
||||
[callback]
|
||||
(letfn [(on-release [pool browser ctx result error]
|
||||
(-> (p/do! (.close ^js ctx))
|
||||
(p/handle
|
||||
(fn [_ _]
|
||||
(.release ^js pool browser)))
|
||||
(p/handle
|
||||
(fn [_ _]
|
||||
(let [id (unchecked-get browser "__id")]
|
||||
(log/info :origin "exec" :action "release" :browser-id id))
|
||||
(if result
|
||||
(p/resolved result)
|
||||
(p/rejected error))))))
|
||||
(letfn [(release-browser [pool browser]
|
||||
(let [id (unchecked-get browser "__id")]
|
||||
(-> (p/do! (.release ^js pool browser))
|
||||
(p/handle (fn [res err]
|
||||
(l/trace :action "exec:release-browser" :browser-id id)
|
||||
(when err (js/console.log err))
|
||||
(if err
|
||||
(p/rejected err)
|
||||
(p/resolved res)))))))
|
||||
|
||||
(destroy-browser [pool browser]
|
||||
(let [id (unchecked-get browser "__id")]
|
||||
(-> (p/do! (.destroy ^js pool browser))
|
||||
(p/handle (fn [res err]
|
||||
(l/trace :action "exec:destroy-browser" :browser-id id)
|
||||
(when err (js/console.log err))
|
||||
(if err
|
||||
(p/rejected err)
|
||||
(p/resolved res)))))))
|
||||
|
||||
(handle-error [pool browser obj err]
|
||||
(let [id (unchecked-get browser "__id")]
|
||||
(if err
|
||||
(do
|
||||
(l/trace :action "exec:handle-error" :browser-id id)
|
||||
(-> (p/do! (destroy-browser pool browser))
|
||||
(p/handle #(p/rejected err))))
|
||||
(p/resolved obj))))
|
||||
|
||||
(on-result [pool browser context result]
|
||||
(let [id (unchecked-get browser "__id")]
|
||||
(l/trace :action "exec:on-result" :browser-id id)
|
||||
(-> (p/do! (.close ^js context))
|
||||
(p/handle (fn [_ err]
|
||||
(if err
|
||||
(destroy-browser pool browser)
|
||||
(release-browser pool browser))))
|
||||
(p/handle #(p/resolved result)))))
|
||||
|
||||
(on-page [pool browser context page]
|
||||
(let [id (unchecked-get browser "__id")]
|
||||
(l/trace :action "exec:on-page" :browser-id id)
|
||||
(-> (p/do! (callback page))
|
||||
(p/handle (partial handle-error pool browser))
|
||||
(p/then (partial on-result pool browser context)))))
|
||||
|
||||
(on-context [pool browser ctx]
|
||||
(-> (p/do! (.newPage ^js ctx))
|
||||
(p/then callback)
|
||||
(p/handle #(on-release pool browser ctx %1 %2))))
|
||||
(let [id (unchecked-get browser "__id")]
|
||||
(l/trace :action "exec:on-context" :browser-id id)
|
||||
(-> (p/do! (.newPage ^js ctx))
|
||||
(p/handle (partial handle-error pool browser))
|
||||
(p/then (partial on-page pool browser ctx)))))
|
||||
|
||||
(on-acquire [pool browser]
|
||||
(-> (.createIncognitoBrowserContext ^js browser)
|
||||
(p/then #(on-context pool browser %))))]
|
||||
(on-acquire [pool browser err]
|
||||
(let [id (unchecked-get browser "__id")]
|
||||
(l/trace :action "exec:on-acquire" :browser-id id)
|
||||
(if err
|
||||
(js/console.log err)
|
||||
(-> (p/do! (.createIncognitoBrowserContext ^js browser))
|
||||
(p/handle (partial handle-error pool browser))
|
||||
(p/then (partial on-context pool browser))))))]
|
||||
|
||||
(when-let [pool (deref pool)]
|
||||
(-> (p/do! (.acquire ^js pool))
|
||||
(p/then (partial on-acquire pool))))))
|
||||
(p/handle (partial on-acquire pool))))))
|
||||
|
|
|
@ -6,23 +6,22 @@
|
|||
|
||||
(ns app.core
|
||||
(:require
|
||||
[lambdaisland.glogi :as log]
|
||||
[lambdaisland.glogi.console :as glogi-console]
|
||||
[promesa.core :as p]
|
||||
[app.browser :as bwr]
|
||||
[app.common.logging :as l]
|
||||
[app.config]
|
||||
[app.http :as http]
|
||||
[app.sentry :as sentry]
|
||||
[app.config]
|
||||
[app.browser :as bwr]))
|
||||
[promesa.core :as p]))
|
||||
|
||||
(glogi-console/install!)
|
||||
(enable-console-print!)
|
||||
(l/initialize!)
|
||||
(sentry/init!)
|
||||
|
||||
(defonce state (atom nil))
|
||||
|
||||
(defn start
|
||||
[& args]
|
||||
(log/info :msg "initializing")
|
||||
(l/info :msg "initializing")
|
||||
(p/do!
|
||||
(bwr/init)
|
||||
(http/init)))
|
||||
|
@ -34,7 +33,7 @@
|
|||
;; an empty line for visual feedback of restart
|
||||
(js/console.log "")
|
||||
|
||||
(log/info :msg "stoping")
|
||||
(l/info :msg "stoping")
|
||||
(p/do!
|
||||
(bwr/stop)
|
||||
(http/stop)
|
||||
|
|
|
@ -6,17 +6,19 @@
|
|||
|
||||
(ns app.http
|
||||
(:require
|
||||
[app.common.logging :as l]
|
||||
[app.config :as cf]
|
||||
[app.http.export :refer [export-handler]]
|
||||
[app.http.export-frames :refer [export-frames-handler]]
|
||||
[app.http.impl :as impl]
|
||||
[app.util.transit :as t]
|
||||
[app.sentry :as sentry]
|
||||
[app.util.transit :as t]
|
||||
[cuerdas.core :as str]
|
||||
[lambdaisland.glogi :as log]
|
||||
[promesa.core :as p]
|
||||
[reitit.core :as r]))
|
||||
|
||||
(l/set-level! :info)
|
||||
|
||||
(def routes
|
||||
[["/export-frames" {:handler export-frames-handler}]
|
||||
["/export" {:handler export-handler}]])
|
||||
|
@ -49,7 +51,7 @@
|
|||
|
||||
:else
|
||||
(do
|
||||
(log/error :msg "Unexpected error" :error error)
|
||||
(l/error :msg "Unexpected error" :error error)
|
||||
(js/console.error error)
|
||||
{:status 500
|
||||
:headers {"x-error" (t/encode data)}
|
||||
|
@ -62,10 +64,10 @@
|
|||
server (impl/server handler on-error)
|
||||
port (cf/get :http-server-port 6061)]
|
||||
(.listen server port)
|
||||
(log/info :msg "welcome to penpot"
|
||||
(l/info :msg "welcome to penpot"
|
||||
:module "exporter"
|
||||
:version (:full @cf/version))
|
||||
(log/info :msg "starting http server" :port port)
|
||||
(l/info :msg "starting http server" :port port)
|
||||
(reset! instance server)))
|
||||
|
||||
(defn stop
|
||||
|
@ -73,6 +75,6 @@
|
|||
(if-let [server @instance]
|
||||
(p/create (fn [resolve]
|
||||
(.close server (fn []
|
||||
(log/info :msg "shutdown http server")
|
||||
(l/info :msg "shutdown http server")
|
||||
(resolve)))))
|
||||
(p/resolved nil)))
|
||||
|
|
|
@ -9,12 +9,11 @@
|
|||
[app.common.exceptions :as exc :include-macros true]
|
||||
[app.common.spec :as us]
|
||||
[app.renderer.bitmap :as rb]
|
||||
[app.renderer.svg :as rs]
|
||||
[app.renderer.pdf :as rp]
|
||||
[app.renderer.svg :as rs]
|
||||
[app.zipfile :as zip]
|
||||
[cljs.spec.alpha :as s]
|
||||
[cuerdas.core :as str]
|
||||
[lambdaisland.glogi :as log]
|
||||
[promesa.core :as p]))
|
||||
|
||||
(s/def ::name ::us/string)
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
["raw-body" :as raw-body]
|
||||
[app.util.transit :as t]
|
||||
[cuerdas.core :as str]
|
||||
[lambdaisland.glogi :as log]
|
||||
[lambdaisland.uri :as u]
|
||||
[promesa.core :as p]
|
||||
[reitit.core :as r]))
|
||||
|
|
|
@ -10,13 +10,13 @@
|
|||
[app.browser :as bw]
|
||||
[app.common.data :as d]
|
||||
[app.common.exceptions :as ex :include-macros true]
|
||||
[app.common.logging :as l]
|
||||
[app.common.pages :as cp]
|
||||
[app.common.spec :as us]
|
||||
[app.config :as cf]
|
||||
[cljs.spec.alpha :as s]
|
||||
[cuerdas.core :as str]
|
||||
[lambdaisland.uri :as u]
|
||||
[lambdaisland.glogi :as log]
|
||||
[promesa.core :as p]))
|
||||
|
||||
(defn create-cookie
|
||||
|
@ -39,7 +39,7 @@
|
|||
(screenshot page (str uri) cookie)))
|
||||
|
||||
(screenshot [page uri cookie]
|
||||
(log/info :uri uri)
|
||||
(l/info :uri uri)
|
||||
(let [viewport {:width 1920
|
||||
:height 1080
|
||||
:scale scale}
|
||||
|
|
|
@ -9,11 +9,11 @@
|
|||
(:require
|
||||
[app.browser :as bw]
|
||||
[app.common.exceptions :as ex :include-macros true]
|
||||
[app.common.logging :as l]
|
||||
[app.common.spec :as us]
|
||||
[app.config :as cf]
|
||||
[cljs.spec.alpha :as s]
|
||||
[lambdaisland.uri :as u]
|
||||
[lambdaisland.glogi :as log]
|
||||
[promesa.core :as p]))
|
||||
|
||||
(defn create-cookie
|
||||
|
@ -36,7 +36,7 @@
|
|||
(pdf-from page (str uri) cookie)))
|
||||
|
||||
(pdf-from [page uri cookie]
|
||||
(log/info :uri uri)
|
||||
(l/info :uri uri)
|
||||
(let [options {:cookie cookie}]
|
||||
(p/do!
|
||||
(bw/configure-page! page options)
|
||||
|
|
|
@ -11,19 +11,19 @@
|
|||
[app.browser :as bw]
|
||||
[app.common.data :as d]
|
||||
[app.common.exceptions :as ex :include-macros true]
|
||||
[app.common.logging :as l]
|
||||
[app.common.pages :as cp]
|
||||
[app.common.spec :as us]
|
||||
[app.config :as cf]
|
||||
[app.renderer.bitmap :refer [create-cookie]]
|
||||
[app.util.shell :as sh]
|
||||
[cljs.spec.alpha :as s]
|
||||
[clojure.walk :as walk]
|
||||
[cuerdas.core :as str]
|
||||
[lambdaisland.glogi :as log]
|
||||
[lambdaisland.uri :as u]
|
||||
[app.renderer.bitmap :refer [create-cookie]]
|
||||
[promesa.core :as p]))
|
||||
|
||||
(log/set-level "app.renderer.svg" :trace)
|
||||
(l/set-level! :trace)
|
||||
|
||||
(defn- xml->clj
|
||||
[data]
|
||||
|
@ -116,22 +116,22 @@
|
|||
(defn- render-object
|
||||
[{:keys [page-id file-id object-id token scale suffix type]}]
|
||||
(letfn [(convert-to-ppm [pngpath]
|
||||
(log/trace :fn :convert-to-ppm)
|
||||
(l/trace :fn :convert-to-ppm)
|
||||
(let [basepath (path/dirname pngpath)
|
||||
ppmpath (path/join basepath "origin.ppm")]
|
||||
(-> (sh/run-cmd! (str "convert " pngpath " " ppmpath))
|
||||
(p/then (constantly ppmpath)))))
|
||||
|
||||
(trace-color-mask [pbmpath]
|
||||
(log/trace :fn :trace-color-mask :pbmpath pbmpath)
|
||||
(l/trace :fn :trace-color-mask :pbmpath pbmpath)
|
||||
(let [basepath (path/dirname pbmpath)
|
||||
basename (path/basename pbmpath ".pbm")
|
||||
svgpath (path/join basepath (str basename ".svg"))]
|
||||
(-> (sh/run-cmd! (str "potrace --flat -b svg " pbmpath " -o " svgpath))
|
||||
(p/then (constantly svgpath)))))
|
||||
|
||||
|
||||
(generate-color-layer [ppmpath color]
|
||||
(log/trace :fn :generate-color-layer :ppmpath ppmpath :color color)
|
||||
(l/trace :fn :generate-color-layer :ppmpath ppmpath :color color)
|
||||
(let [basepath (path/dirname ppmpath)
|
||||
pbmpath (path/join basepath (str "mask-" (subs color 1) ".pbm"))]
|
||||
(-> (sh/run-cmd! (str/format "ppmcolormask \"%s\" %s" color ppmpath))
|
||||
|
@ -193,7 +193,7 @@
|
|||
(mapv (partial data->gradient-def id))))
|
||||
|
||||
(join-color-layers [{:keys [id x y width height mapping] :as node} layers]
|
||||
(log/trace :fn :join-color-layers :mapping mapping)
|
||||
(l/trace :fn :join-color-layers :mapping mapping)
|
||||
(loop [result (-> (:svgdata (first layers))
|
||||
(assoc "elements" []))
|
||||
layers (seq layers)]
|
||||
|
@ -241,12 +241,12 @@
|
|||
(assoc "elements" elements))))))
|
||||
|
||||
(convert-to-svg [ppmpath {:keys [colors] :as node}]
|
||||
(log/trace :fn :convert-to-svg :ppmpath ppmpath :colors colors)
|
||||
(l/trace :fn :convert-to-svg :ppmpath ppmpath :colors colors)
|
||||
(-> (p/all (map (partial generate-color-layer ppmpath) colors))
|
||||
(p/then (partial join-color-layers node))))
|
||||
|
||||
(trace-node [{:keys [data] :as node}]
|
||||
(log/trace :fn :trace-node)
|
||||
(l/trace :fn :trace-node)
|
||||
(p/let [tdpath (sh/create-tmpdir! "svgexport-")
|
||||
pngpath (path/join tdpath "origin.png")
|
||||
_ (sh/write-file! pngpath data)
|
||||
|
@ -270,7 +270,7 @@
|
|||
:mapping (js/JSON.parse mapping)}))
|
||||
|
||||
(extract-single-node [[shot node]]
|
||||
(log/trace :fn :extract-single-node)
|
||||
(l/trace :fn :extract-single-node)
|
||||
|
||||
(p/let [attrs (bw/eval! node extract-element-attrs)]
|
||||
{:id (unchecked-get attrs "id")
|
||||
|
@ -302,7 +302,7 @@
|
|||
(p/then clean-temp-data)))
|
||||
|
||||
(process-text-nodes [page]
|
||||
(log/trace :fn :process-text-nodes)
|
||||
(l/trace :fn :process-text-nodes)
|
||||
(-> (bw/select-all page "#screenshot foreignObject")
|
||||
(p/then (fn [nodes] (p/all (map (partial process-text-node page) nodes))))))
|
||||
|
||||
|
@ -345,7 +345,7 @@
|
|||
cookie (create-cookie uri token)
|
||||
rctx {:cookie cookie
|
||||
:uri (str uri)}]
|
||||
(log/info :uri (:uri rctx))
|
||||
(l/info :uri (:uri rctx))
|
||||
(bw/exec! (partial handle rctx)))))
|
||||
|
||||
(s/def ::name ::us/string)
|
||||
|
|
|
@ -11,10 +11,10 @@
|
|||
["fs" :as fs]
|
||||
["os" :as os]
|
||||
["path" :as path]
|
||||
[lambdaisland.glogi :as log]
|
||||
[app.common.logging :as l]
|
||||
[promesa.core :as p]))
|
||||
|
||||
(log/set-level "app.util.shell" :trace)
|
||||
(l/set-level! :trace)
|
||||
|
||||
(defn create-tmpdir!
|
||||
[prefix]
|
||||
|
@ -47,10 +47,10 @@
|
|||
[cmd]
|
||||
(p/create
|
||||
(fn [resolve reject]
|
||||
(log/trace :fn :run-cmd :cmd cmd)
|
||||
(l/trace :fn :run-cmd :cmd cmd)
|
||||
(chp/exec cmd #js {:encoding "buffer"}
|
||||
(fn [error stdout stderr]
|
||||
;; (log/trace :fn :run-cmd :stdout stdout)
|
||||
;; (l/trace :fn :run-cmd :stdout stdout)
|
||||
(if error
|
||||
(reject error)
|
||||
(resolve stdout)))))))
|
||||
|
|
Loading…
Add table
Reference in a new issue