This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-11-23
Channels
- # announcements (26)
- # babashka (8)
- # babashka-sci-dev (3)
- # beginners (93)
- # biff (44)
- # calva (1)
- # cider (7)
- # clj-kondo (13)
- # cljdoc (1)
- # clojure (121)
- # clojure-australia (2)
- # clojure-europe (18)
- # clojure-nl (1)
- # clojure-norway (5)
- # clojure-uk (1)
- # clojurescript (35)
- # conjure (1)
- # core-async (2)
- # datalevin (6)
- # datomic (28)
- # emacs (25)
- # events (1)
- # fulcro (5)
- # introduce-yourself (2)
- # jobs (8)
- # leiningen (2)
- # off-topic (13)
- # other-languages (1)
- # podcasts-discuss (1)
- # polylith (7)
- # rdf (6)
- # re-frame (1)
- # reagent (53)
- # releases (3)
- # rewrite-clj (7)
- # scittle (5)
- # shadow-cljs (63)
- # specter (1)
- # squint (5)
- # tools-build (5)
- # xtdb (7)
Hello my use case is I want to start some long running process on the server and signal the client with something like "pending" to say "retry the same request later and you get a result". Then I want to hold on to the result for a few minutes or something
(def result-cache
"request -> result-promise channel."
(atom {}))
(defn send*
"Call `f` with `r` on a thread.
Assoc a result promise-chan on `a` (an atom).
Dissoc the result 5min after completion."
[a r f]
(let [prom (a/promise-chan)]
(a/thread
(let [result (try (f r) (catch Throwable t {:error (ex-data t)}))]
(a/>!! prom result))
(a/go
(a/<! (a/timeout (* 1000 60 5)))
(swap! a dissoc r)))
(swap! a assoc r prom)))
(defn poll-result!
"Returns the current result of the result promise.
"
[request f]
(let [prom
(@result-cache request)
result (when prom (a/poll! prom))]
(cond
result result
prom :running
:else
(do
(send* result-cache request f)
:started))))