Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle thrown exception and deliver lacinia result appropriately #1

Merged
merged 6 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{:paths ["src"]
:deps {funcool/urania {:mvn/version "0.2.0"}
funcool/promesa {:mvn/version "5.1.0"}
org.clojure/tools.logging {:mvn/version "0.6.0"}}
{:paths ["src"]
:deps {funcool/urania {:git/url "https://github.com/green-labs/urania.git"
:sha "097afe6b7889baf8eff2ddc34937b61f36adc48c"}
; urania 0.2.0의 다음 버전이 릴리즈되면 그 버전으로 업데이트 필요
funcool/promesa {:mvn/version "5.1.0"}
org.clojure/tools.logging {:mvn/version "0.6.0"}}

:mvn/repos
{"central" {:url "https://repo1.maven.org/maven2/"}
Expand Down
9 changes: 6 additions & 3 deletions src/superlifter/api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

(defn unwrap
([p] (unwrap identity p))
([f p]
([f p] (unwrap f identity p))
([then-f catch-f p]
(if (prom/promise? p)
(prom/then p f)
(prom/resolved (f p)))))
(-> p
(prom/then then-f)
(prom/catch catch-f))
(prom/resolved (then-f p)))))

#?(:clj (defmacro def-fetcher [sym bindings do-fetch-fn]
`(defrecord ~sym ~bindings
Expand Down
45 changes: 26 additions & 19 deletions src/superlifter/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@
(fn [buckets]
(update buckets bucket-id (comp f clear-ready))))
(get bucket-id))]
(if-let [muses (not-empty (get-in new [:queue :ready]))]
(let [cache (get-in new [:urania-opts :cache])]
(log :info "Fetching" (count muses) "muses from bucket" bucket-id)
(if-let [muses-and-promises (not-empty (get-in new [:queue :ready]))]
(let [cache (get-in new [:urania-opts :cache])

muses (map :muse muses-and-promises)
promises (map :promise muses-and-promises)]
(log :info "Fetching" (count muses-and-promises) "muses from bucket" bucket-id)
(-> (u/execute! (u/collect muses)
(merge (:urania-opts new)
(when cache
Expand All @@ -49,7 +52,14 @@
(fn [[result new-cache-value]]
(when cache
(urania-> cache new-cache-value))
result))))
(doall (map prom/resolve! promises result))))
;(run! (fn [[p result]] (prom/resolve! p result))
; (zipmap promises result))))
Copy link
Author

@ljb7977 ljb7977 Apr 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사실 이건 원 저자가 만든 것처럼 run!을 써도 되는데, 그냥 doall~map이 맘에 더 들어서 바꿨습니다 ㅋㅋㅋ
(zipmap을 굳이 쓰는 게 별로기도 했구요)

run!이든 doall이든 둘 다 잘 동작하긴 합니다.

(prom/catch
(fn [ex]
(doall (map prom/reject! promises (repeat ex)))))))
;(run! (fn [p] (prom/reject! p ex))
; promises)))))
(do (log :debug "Nothing ready to fetch for" bucket-id)
(prom/resolved nil)))))

Expand All @@ -72,37 +82,34 @@
The muses in the queue will all be fetched together when a trigger condition is met."
([context muse] (enqueue! context default-bucket-id muse))
([context bucket-id muse]
(let [p (prom/deferred)
delivering-muse (u/map (fn [result]
(prom/resolve! p result)
result)
muse)]
(let [promise (prom/deferred)]
(log :debug "Enqueuing muse into" bucket-id (:id muse))
(update-bucket! context
bucket-id
(fn [bucket]
(reduce (fn [b trigger-fn]
(trigger-fn b))
(update-in bucket [:queue :waiting] conj delivering-muse)
(update-in bucket [:queue :waiting] conj {:muse muse
:promise promise})
(keep :enqueue-fn (vals (:triggers bucket))))))
p)))
promise)))

(defn- fetch-all-handling-errors! [context bucket-id]
(try (prom/catch (fetch-bucket! context bucket-id)
(fn [error]
(log :warn "Fetch failed" error)))
(fn [error]
(log :warn "Fetch failed" error)))
(catch Throwable t
(log :warn "Fetch failed" t))))

(defmulti start-trigger! (fn [kind _context _bucket-id _opts] kind))

(defmethod start-trigger! :queue-size [_ _context _bucket-id {:keys [threshold] :as opts}]
(assoc opts :enqueue-fn (fn [{:keys [queue] :as bucket}]
(if (= threshold (count (:waiting queue)))
(-> bucket
(assoc-in [:queue :ready] (take threshold (:waiting queue)))
(update-in [:queue :waiting] #(drop threshold %)))
bucket))))
(assoc opts :enqueue-fn (fn [{:keys [queue] :as bucket}]
(if (= threshold (count (:waiting queue)))
(-> bucket
(assoc-in [:queue :ready] (take threshold (:waiting queue)))
(update-in [:queue :waiting] #(drop threshold %)))
bucket))))

(defmethod start-trigger! :elastic [kind _context _bucket-id opts]
(assoc opts
Expand Down
4 changes: 3 additions & 1 deletion src/superlifter/lacinia.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

(defn ->lacinia-promise [sl-result]
(let [l-prom (resolve/resolve-promise)]
(api/unwrap #(resolve/deliver! l-prom %) (prom/catch sl-result prom/resolved))
(api/unwrap (fn [result] (resolve/deliver! l-prom result))
(fn [error] (resolve/deliver! l-prom nil {:message (.getMessage error)}))
sl-result)
l-prom))

(defmacro with-superlifter [ctx body]
Expand Down