diff --git a/src/clj/auto_ap/jobs/core.clj b/src/clj/auto_ap/jobs/core.clj index ff74fc02..74599114 100644 --- a/src/clj/auto_ap/jobs/core.clj +++ b/src/clj/auto_ap/jobs/core.clj @@ -12,7 +12,8 @@ (lc/with-context {:background-job name} (mu/with-context {:background-job 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)) (log/info "Stopping " name) (Thread/sleep 15000) diff --git a/src/clj/auto_ap/square/core3.clj b/src/clj/auto_ap/square/core3.clj index 198c4e4c..58a8ff3f 100644 --- a/src/clj/auto_ap/square/core3.clj +++ b/src/clj/auto_ap/square/core3.clj @@ -25,12 +25,6 @@ "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 (let [stream (s/stream 100)] (->> stream @@ -47,17 +41,17 @@ :background-job "Square 3") (try (client/request (assoc request - :socket-timeout 10000 - :connection-timeout 10000 - :as :json - :retry-handler retry-4)) - (catch Exception e - (log/error ::raw-request-failed + :socket-timeout 5000 + :connection-timeout 5000 + :connection-request-timeout 5000 + :as :json)) + (catch Throwable e + (log/warn ::raw-request-failed :exception e) (throw e))))) (de/catch (fn [e] - (if (= attempt 5) + (if (>= attempt 5) (throw e) (de/chain (mt/in 1000 (fn [] 1)) @@ -144,7 +138,7 @@ (mu/with-context lc (item->category-name-impl client item)))) (fn [e] - (log/error ::couldnt-fetch-variation + (log/warn ::couldnt-fetch-variation :exception e) "Uncategorized")) @@ -153,7 +147,7 @@ :category_data :name) (fn [e] - (log/error ::couldnt-fetch-category + (log/warn ::couldnt-fetch-category :exception e) "Uncategorized")) @@ -324,52 +318,56 @@ (de/success-deferred (->> (:tenders order) (map #(tender->charge order client location %)))) - (de/let-flow [line-items - (->> - (or (:line_items order) []) - (s/->source) - (s/transform - (map-indexed (fn [i li] - (mu/with-context lc - (de/let-flow [category (item-id->category-name client (:catalog_object_id li))] - (remove-nils - #:order-line-item - {:external-id (str "square/order/" (:client/code client) "-" (:square-location/client-location location) "-" (:id order) "-" i) - :item-name (:name li) - :category (if (= "GIFT_CARD" (:item_type li)) - "Gift Card" - category) - :total (amount->money (:total_money li)) - :tax (amount->money (:total_tax_money li)) - :discount (amount->money (:total_discount_money li))}))))) - ) - (s/buffer 5) - (s/realize-each) - (s/reduce conj []))] - [(remove-nils - #:sales-order - {:date (coerce/to-date (time/to-time-zone (coerce/to-date-time (:created_at order)) (time/time-zone-for-id "America/Los_Angeles"))) - :client (:db/id client) - :location (:square-location/client-location location) - :external-id (str "square/order/" (:client/code client) "-" (:square-location/client-location location) "-" (:id order)) - :source (or (:name (:source order)) - "Square") - :vendor :vendor/ccp-square + (de/catch + (de/let-flow [line-items + (->> + (or (:line_items order) []) + (s/->source) + (s/transform + (map-indexed (fn [i li] + (mu/with-context lc + (de/let-flow [category (item-id->category-name client (:catalog_object_id li))] + (remove-nils + #:order-line-item + {:external-id (str "square/order/" (:client/code client) "-" (:square-location/client-location location) "-" (:id order) "-" i) + :item-name (:name li) + :category (if (= "GIFT_CARD" (:item_type li)) + "Gift Card" + category) + :total (amount->money (:total_money li)) + :tax (amount->money (:total_tax_money li)) + :discount (amount->money (:total_discount_money li))}))))) + ) + (s/buffer 5) + (s/realize-each) + (s/reduce conj []))] + [(remove-nils + #:sales-order + {:date (coerce/to-date (time/to-time-zone (coerce/to-date-time (:created_at order)) (time/time-zone-for-id "America/Los_Angeles"))) + :client (:db/id client) + :location (:square-location/client-location location) + :external-id (str "square/order/" (:client/code client) "-" (:square-location/client-location location) "-" (:id order)) + :source (or (:name (:source order)) + "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))) - :total (-> order :net_amounts :total_money amount->money) - :tax (-> order :net_amounts :tax_money amount->money) - :tip (-> order :net_amounts :tip_money amount->money) - :discount (-> order :net_amounts :discount_money amount->money) - :service-charge (-> order :net_amounts :service_charge_money amount->money) - :returns (+ (- (-> order :return_amounts :total_money amount->money) - (-> order :return_amounts :tax_money amount->money) - (-> order :return_amounts :tip_money amount->money) - (-> order :return_amounts :service_charge_money amount->money)) - (-> order :return_amounts :discount_money amount->money)) - :charges (->> (:tenders order) - (map #(tender->charge order client location %))) - :line-items line-items})]))))) + :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) + :tax (-> order :net_amounts :tax_money amount->money) + :tip (-> order :net_amounts :tip_money amount->money) + :discount (-> order :net_amounts :discount_money amount->money) + :service-charge (-> order :net_amounts :service_charge_money amount->money) + :returns (+ (- (-> order :return_amounts :total_money amount->money) + (-> order :return_amounts :tax_money amount->money) + (-> order :return_amounts :tip_money amount->money) + (-> order :return_amounts :service_charge_money amount->money)) + (-> order :return_amounts :discount_money amount->money)) + :charges (->> (:tenders order) + (map #(tender->charge order client location %))) + :line-items line-items})]) + (fn [e] + (log/error ::failed-to-transform-order + :exception e))))))) (defn daily-results ([client location] @@ -498,7 +496,7 @@ (map (fn [p] {:charge/external-id (str "square/charge/" (:payment_id p))})))}) (filter :expected-deposit/date) (into [])) - (catch Exception e + (catch Throwable e (log/error ::transform-settlement-failed :exception e))))))) @@ -694,7 +692,7 @@ (mu/with-context lc (let [data (ex-data e)] (log/info ::upsert-all-failed - :severity :warn + :severity :error :exception e) (cond (= (:status data) 401) (mark-integration-status client {:integration-status/state :integration-state/unauthorized