mirror of
https://github.com/penpot/penpot.git
synced 2025-05-24 16:06:18 +02:00
🎉 Add update-file (batched) to webhooks
This commit is contained in:
parent
782f2ed57d
commit
d56082307b
4 changed files with 54 additions and 15 deletions
|
@ -104,11 +104,18 @@
|
||||||
(s/def ::type ::us/string)
|
(s/def ::type ::us/string)
|
||||||
(s/def ::props (s/map-of ::us/keyword any?))
|
(s/def ::props (s/map-of ::us/keyword any?))
|
||||||
(s/def ::ip-addr ::us/string)
|
(s/def ::ip-addr ::us/string)
|
||||||
|
|
||||||
(s/def ::webhooks/event? ::us/boolean)
|
(s/def ::webhooks/event? ::us/boolean)
|
||||||
|
(s/def ::webhooks/batch-timeout ::dt/duration)
|
||||||
|
(s/def ::webhooks/batch-key
|
||||||
|
(s/or :fn fn? :str string? :kw keyword?))
|
||||||
|
|
||||||
(s/def ::event
|
(s/def ::event
|
||||||
(s/keys :req-un [::type ::name ::profile-id]
|
(s/keys :req-un [::type ::name ::profile-id]
|
||||||
:opt-un [::ip-addr ::props ::webhooks/event?]))
|
:opt-un [::ip-addr ::props]
|
||||||
|
:opt [::webhooks/event?
|
||||||
|
::webhooks/batch-timeout
|
||||||
|
::webhooks/batch-key]))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; COLLECTOR
|
;; COLLECTOR
|
||||||
|
@ -153,13 +160,22 @@
|
||||||
|
|
||||||
(when (and (contains? cf/flags :webhooks)
|
(when (and (contains? cf/flags :webhooks)
|
||||||
(::webhooks/event? event))
|
(::webhooks/event? event))
|
||||||
|
(let [batch-key (::webhooks/batch-key event)
|
||||||
|
batch-timeout (::webhooks/batch-timeout event)]
|
||||||
(wrk/submit! ::wrk/conn pool
|
(wrk/submit! ::wrk/conn pool
|
||||||
::wrk/task :process-webhook-event
|
::wrk/task :process-webhook-event
|
||||||
::wrk/queue :webhooks
|
::wrk/queue :webhooks
|
||||||
::wrk/max-retries 0
|
::wrk/max-retries 0
|
||||||
|
::wrk/delay (or batch-timeout 0)
|
||||||
|
::wrk/label (cond
|
||||||
|
(fn? batch-key) (batch-key (:props event))
|
||||||
|
(keyword? batch-key) (name batch-key)
|
||||||
|
(string? batch-key) batch-key
|
||||||
|
:else "default")
|
||||||
|
::wrk/dedupe true
|
||||||
::webhooks/event (-> params
|
::webhooks/event (-> params
|
||||||
(dissoc :ip-addr)
|
(dissoc :ip-addr)
|
||||||
(dissoc :type))))))
|
(dissoc :type)))))))
|
||||||
|
|
||||||
(defn submit!
|
(defn submit!
|
||||||
"Submit audit event to the collector."
|
"Submit audit event to the collector."
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
[app.common.logging :as l]
|
[app.common.logging :as l]
|
||||||
[app.common.transit :as t]
|
[app.common.transit :as t]
|
||||||
[app.common.uri :as uri]
|
[app.common.uri :as uri]
|
||||||
|
[app.config :as cf]
|
||||||
[app.db :as db]
|
[app.db :as db]
|
||||||
[app.http.client :as http]
|
[app.http.client :as http]
|
||||||
[app.util.json :as json]
|
[app.util.json :as json]
|
||||||
|
@ -56,6 +57,7 @@
|
||||||
[_ {:keys [::db/pool] :as cfg}]
|
[_ {:keys [::db/pool] :as cfg}]
|
||||||
(fn [{:keys [props] :as task}]
|
(fn [{:keys [props] :as task}]
|
||||||
(let [event (::event props)]
|
(let [event (::event props)]
|
||||||
|
|
||||||
(l/debug :hint "process webhook event"
|
(l/debug :hint "process webhook event"
|
||||||
:name (:name event))
|
:name (:name event))
|
||||||
|
|
||||||
|
@ -134,7 +136,8 @@
|
||||||
:webhook-mtype (:mtype whook))
|
:webhook-mtype (:mtype whook))
|
||||||
|
|
||||||
(let [req {:uri (:uri whook)
|
(let [req {:uri (:uri whook)
|
||||||
:headers {"content-type" (:mtype whook)}
|
:headers {"content-type" (:mtype whook)
|
||||||
|
"user-agent" (str/ffmt "penpot/%" (:main cf/version))}
|
||||||
:timeout (dt/duration "4s")
|
:timeout (dt/duration "4s")
|
||||||
:method :post
|
:method :post
|
||||||
:body body}]
|
:body body}]
|
||||||
|
|
|
@ -171,7 +171,16 @@
|
||||||
:profile-id profile-id
|
:profile-id profile-id
|
||||||
:ip-addr (some-> request audit/parse-client-ip)
|
:ip-addr (some-> request audit/parse-client-ip)
|
||||||
:props props
|
:props props
|
||||||
::webhooks/event? (or (::webhooks/event? mdata)
|
::webhooks/batch-key
|
||||||
|
(or (::webhooks/batch-key mdata)
|
||||||
|
(::webhooks/batch-key resultm))
|
||||||
|
|
||||||
|
::webhooks/batch-timeout
|
||||||
|
(or (::webhooks/batch-timeout mdata)
|
||||||
|
(::webhooks/batch-timeout resultm))
|
||||||
|
|
||||||
|
::webhooks/event?
|
||||||
|
(or (::webhooks/event? mdata)
|
||||||
(::webhooks/event? resultm)
|
(::webhooks/event? resultm)
|
||||||
false)}]
|
false)}]
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
[app.config :as cf]
|
[app.config :as cf]
|
||||||
[app.db :as db]
|
[app.db :as db]
|
||||||
[app.loggers.audit :as audit]
|
[app.loggers.audit :as audit]
|
||||||
|
[app.loggers.webhooks :as-alias webhooks]
|
||||||
[app.metrics :as mtx]
|
[app.metrics :as mtx]
|
||||||
[app.msgbus :as mbus]
|
[app.msgbus :as mbus]
|
||||||
[app.rpc.climit :as-alias climit]
|
[app.rpc.climit :as-alias climit]
|
||||||
|
@ -122,12 +123,18 @@
|
||||||
;; set is different than the persisted one, update it on the
|
;; set is different than the persisted one, update it on the
|
||||||
;; database.
|
;; database.
|
||||||
|
|
||||||
|
(defn webhook-batch-keyfn
|
||||||
|
[props]
|
||||||
|
(str "rpc:update-file:" (:id props)))
|
||||||
|
|
||||||
(sv/defmethod ::update-file
|
(sv/defmethod ::update-file
|
||||||
{::climit/queue :update-file
|
{::climit/queue :update-file
|
||||||
::climit/key-fn :id
|
::climit/key-fn :id
|
||||||
|
::webhooks/event? true
|
||||||
|
::webhooks/batch-timeout (dt/duration "2s")
|
||||||
|
::webhooks/batch-key webhook-batch-keyfn
|
||||||
::doc/added "1.17"}
|
::doc/added "1.17"}
|
||||||
[{:keys [pool] :as cfg} {:keys [id profile-id] :as params}]
|
[{:keys [pool] :as cfg} {:keys [id profile-id] :as params}]
|
||||||
|
|
||||||
(db/with-atomic [conn pool]
|
(db/with-atomic [conn pool]
|
||||||
(files/check-edition-permissions! conn profile-id id)
|
(files/check-edition-permissions! conn profile-id id)
|
||||||
(db/xact-lock! conn id)
|
(db/xact-lock! conn id)
|
||||||
|
@ -173,7 +180,11 @@
|
||||||
{:id id})))
|
{:id id})))
|
||||||
|
|
||||||
(-> (update-fn cfg params)
|
(-> (update-fn cfg params)
|
||||||
(vary-meta assoc ::audit/props {:project-id (:project-id file)
|
(vary-meta assoc ::audit/replace-props
|
||||||
|
{:id (:id file)
|
||||||
|
:name (:name file)
|
||||||
|
:features (:features file)
|
||||||
|
:project-id (:project-id file)
|
||||||
:team-id (:team-id file)}))))))
|
:team-id (:team-id file)}))))))
|
||||||
|
|
||||||
(defn- update-file*
|
(defn- update-file*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue