Set up some debugging features for square jobs.

This commit is contained in:
2023-01-21 21:18:18 -08:00
parent 7a8267f09f
commit 221adb24f8
2 changed files with 62 additions and 63 deletions

View File

@@ -12,7 +12,8 @@
(lc/with-context {:background-job name} (lc/with-context {:background-job name}
(mu/with-context {:background-job name (mu/with-context {:background-job name
:service name} :service name}
(mount/start (mount/only #{#'conn #'metrics-setup #'container-tags #'logging-context #'container-data})) (mount/start (mount/only #{#'conn #'metrics-setup #'container-tags #'logging-context #'container-data }))
(start-server :port 9000 :bind "0.0.0.0" #_#_:handler (cider-nrepl-handler))
((heartbeat f name)) ((heartbeat f name))
(log/info "Stopping " name) (log/info "Stopping " name)
(Thread/sleep 15000) (Thread/sleep 15000)

View File

@@ -25,12 +25,6 @@
"Content-Type" "application/json"}) "Content-Type" "application/json"})
(defn retry-4 [ex try-count _]
(log/error ::aborting-request
:attempt try-count
:exception ex)
(if (> try-count 4) false true))
(def manifold-api-stream (def manifold-api-stream
(let [stream (s/stream 100)] (let [stream (s/stream 100)]
(->> stream (->> stream
@@ -47,17 +41,17 @@
:background-job "Square 3") :background-job "Square 3")
(try (try
(client/request (assoc request (client/request (assoc request
:socket-timeout 10000 :socket-timeout 5000
:connection-timeout 10000 :connection-timeout 5000
:as :json :connection-request-timeout 5000
:retry-handler retry-4)) :as :json))
(catch Exception e (catch Throwable e
(log/error ::raw-request-failed (log/warn ::raw-request-failed
:exception e) :exception e)
(throw e))))) (throw e)))))
(de/catch (de/catch
(fn [e] (fn [e]
(if (= attempt 5) (if (>= attempt 5)
(throw e) (throw e)
(de/chain (de/chain
(mt/in 1000 (fn [] 1)) (mt/in 1000 (fn [] 1))
@@ -144,7 +138,7 @@
(mu/with-context lc (mu/with-context lc
(item->category-name-impl client item)))) (item->category-name-impl client item))))
(fn [e] (fn [e]
(log/error ::couldnt-fetch-variation (log/warn ::couldnt-fetch-variation
:exception e) :exception e)
"Uncategorized")) "Uncategorized"))
@@ -153,7 +147,7 @@
:category_data :category_data
:name) :name)
(fn [e] (fn [e]
(log/error ::couldnt-fetch-category (log/warn ::couldnt-fetch-category
:exception e) :exception e)
"Uncategorized")) "Uncategorized"))
@@ -324,52 +318,56 @@
(de/success-deferred (de/success-deferred
(->> (:tenders order) (->> (:tenders order)
(map #(tender->charge order client location %)))) (map #(tender->charge order client location %))))
(de/let-flow [line-items (de/catch
(->> (de/let-flow [line-items
(or (:line_items order) []) (->>
(s/->source) (or (:line_items order) [])
(s/transform (s/->source)
(map-indexed (fn [i li] (s/transform
(mu/with-context lc (map-indexed (fn [i li]
(de/let-flow [category (item-id->category-name client (:catalog_object_id li))] (mu/with-context lc
(remove-nils (de/let-flow [category (item-id->category-name client (:catalog_object_id li))]
#:order-line-item (remove-nils
{:external-id (str "square/order/" (:client/code client) "-" (:square-location/client-location location) "-" (:id order) "-" i) #:order-line-item
:item-name (:name li) {:external-id (str "square/order/" (:client/code client) "-" (:square-location/client-location location) "-" (:id order) "-" i)
:category (if (= "GIFT_CARD" (:item_type li)) :item-name (:name li)
"Gift Card" :category (if (= "GIFT_CARD" (:item_type li))
category) "Gift Card"
:total (amount->money (:total_money li)) category)
:tax (amount->money (:total_tax_money li)) :total (amount->money (:total_money li))
:discount (amount->money (:total_discount_money li))}))))) :tax (amount->money (:total_tax_money li))
) :discount (amount->money (:total_discount_money li))})))))
(s/buffer 5) )
(s/realize-each) (s/buffer 5)
(s/reduce conj []))] (s/realize-each)
[(remove-nils (s/reduce conj []))]
#:sales-order [(remove-nils
{:date (coerce/to-date (time/to-time-zone (coerce/to-date-time (:created_at order)) (time/time-zone-for-id "America/Los_Angeles"))) #:sales-order
:client (:db/id client) {:date (coerce/to-date (time/to-time-zone (coerce/to-date-time (:created_at order)) (time/time-zone-for-id "America/Los_Angeles")))
:location (:square-location/client-location location) :client (:db/id client)
:external-id (str "square/order/" (:client/code client) "-" (:square-location/client-location location) "-" (:id order)) :location (:square-location/client-location location)
:source (or (:name (:source order)) :external-id (str "square/order/" (:client/code client) "-" (:square-location/client-location location) "-" (:id order))
"Square") :source (or (:name (:source order))
:vendor :vendor/ccp-square "Square")
:vendor :vendor/ccp-square
:reference-link (str (url/url "https://squareup.com/dashboard/sales/transactions" (:id order) "by-unit" (:square-location/square-id location))) :reference-link (str (url/url "https://squareup.com/dashboard/sales/transactions" (:id order) "by-unit" (:square-location/square-id location)))
:total (-> order :net_amounts :total_money amount->money) :total (-> order :net_amounts :total_money amount->money)
:tax (-> order :net_amounts :tax_money amount->money) :tax (-> order :net_amounts :tax_money amount->money)
:tip (-> order :net_amounts :tip_money amount->money) :tip (-> order :net_amounts :tip_money amount->money)
:discount (-> order :net_amounts :discount_money amount->money) :discount (-> order :net_amounts :discount_money amount->money)
:service-charge (-> order :net_amounts :service_charge_money amount->money) :service-charge (-> order :net_amounts :service_charge_money amount->money)
:returns (+ (- (-> order :return_amounts :total_money amount->money) :returns (+ (- (-> order :return_amounts :total_money amount->money)
(-> order :return_amounts :tax_money amount->money) (-> order :return_amounts :tax_money amount->money)
(-> order :return_amounts :tip_money amount->money) (-> order :return_amounts :tip_money amount->money)
(-> order :return_amounts :service_charge_money amount->money)) (-> order :return_amounts :service_charge_money amount->money))
(-> order :return_amounts :discount_money amount->money)) (-> order :return_amounts :discount_money amount->money))
:charges (->> (:tenders order) :charges (->> (:tenders order)
(map #(tender->charge order client location %))) (map #(tender->charge order client location %)))
:line-items line-items})]))))) :line-items line-items})])
(fn [e]
(log/error ::failed-to-transform-order
:exception e)))))))
(defn daily-results (defn daily-results
([client location] ([client location]
@@ -498,7 +496,7 @@
(map (fn [p] {:charge/external-id (str "square/charge/" (:payment_id p))})))}) (map (fn [p] {:charge/external-id (str "square/charge/" (:payment_id p))})))})
(filter :expected-deposit/date) (filter :expected-deposit/date)
(into [])) (into []))
(catch Exception e (catch Throwable e
(log/error ::transform-settlement-failed (log/error ::transform-settlement-failed
:exception e))))))) :exception e)))))))
@@ -694,7 +692,7 @@
(mu/with-context lc (mu/with-context lc
(let [data (ex-data e)] (let [data (ex-data e)]
(log/info ::upsert-all-failed (log/info ::upsert-all-failed
:severity :warn :severity :error
:exception e) :exception e)
(cond (= (:status data) 401) (cond (= (:status data) 401)
(mark-integration-status client {:integration-status/state :integration-state/unauthorized (mark-integration-status client {:integration-status/state :integration-state/unauthorized