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

use vthread #3

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
: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"}
funcool/promesa {:mvn/version "10.0.594"}
org.clojure/tools.logging {:mvn/version "0.6.0"}}

:mvn/repos
Expand Down
33 changes: 25 additions & 8 deletions src/superlifter/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,31 @@
(defn update-trigger! [context bucket-id trigger-kind opts-fn]
(update-bucket! context bucket-id (fn [bucket]
(update-in bucket [:triggers trigger-kind] opts-fn))))
(def futures (atom []))

(comment
(def continue? (atom true))
(def p1 (prom/create (fn [resolve reject]
(while @continue?
(println "Hello!")
(Thread/sleep 1000))
(resolve :done))
:vthread))
(reset! continue? false)
@p1)

(defmethod start-trigger! :interval [_ context bucket-id opts]
(let [watcher #?(:clj (future (loop []
(Thread/sleep (:interval opts))
(fetch-all-handling-errors! context bucket-id)
(recur)))
(let [continue? (atom true)
watcher #?(:clj (prom/create (fn [resolve reject]
(while @continue?
(Thread/sleep (:interval opts))
(fetch-all-handling-errors! context bucket-id))
(resolve true))
:vthread)
:cljs (js/setInterval #(fetch-all-handling-errors! context bucket-id)
(:interval opts)))]
(assoc opts :stop-fn #?(:clj #(future-cancel watcher)
;(swap! futures conj watcher)
(assoc opts :stop-fn #?(:clj #(reset! continue? false)
:cljs #(js/clearInterval watcher)))))

#?(:cljs
Expand Down Expand Up @@ -186,15 +202,15 @@

(defn- start-triggers! [context bucket-id {:keys [triggers] :as opts}]
(update opts :triggers
#(do (log :debug "Starting" (count triggers) "triggers for bucket" bucket-id)
#(do (log :info "Starting" (count triggers) "triggers for bucket" bucket-id)
(reduce-kv (fn [ts trigger-kind trigger-opts]
(log :debug "Starting trigger" trigger-kind "for bucket" bucket-id trigger-opts)
(log :info "Starting trigger" trigger-kind "for bucket" bucket-id trigger-opts)
(assoc ts trigger-kind (start-trigger! trigger-kind context bucket-id trigger-opts)))
{}
%))))

(defn- start-bucket! [context bucket-id opts]
(log :debug "Starting bucket" bucket-id)
(log :info "Starting bucket" bucket-id)
(start-triggers! context bucket-id (-> (assoc opts :queue {:ready [] :waiting []} :id bucket-id)
(update :urania-opts #(merge (:urania-opts context) %)))))

Expand All @@ -207,6 +223,7 @@
context)

(defn- stop-bucket! [context bucket-id]
(log :info "Stopping bucket" bucket-id)
Copy link
Author

Choose a reason for hiding this comment

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

(여기 log 레벨은 테스트 후에 다시 :debug로 원복해야 함)

(doseq [{:keys [stop-fn]} (vals (:triggers (get @(:buckets context) bucket-id)))
:when stop-fn]
(stop-fn)))
Expand Down