Переглянути джерело

added multiprocessing, fixed db queries (#2763)

Dmitri Sotnikov 8 роки тому
батько
коміт
6d287848c2

+ 13 - 1
frameworks/Clojure/macchiato/hello/src/hello/core.cljs

@@ -18,4 +18,16 @@
        :port       port
        :on-success #(info "hello started on" host ":" port)})))
 
-(defn main [& args] (app))
+(defn start-workers [cluster]
+  (let [os (js/require "os")]
+    (dotimes [_ (get-in @env [:cluster :procs] (-> os .cpus .-length))]
+      (.fork cluster))
+    (.on cluster "exit"
+         (fn [worker code signal]
+           (info "worker terminated" (-> worker .-process .-pid))))))
+
+(defn main [& args]
+  (let [cluster (js/require "cluster")]
+    (if (.-isMaster cluster)
+      (start-workers cluster)
+      (app))))

+ 5 - 3
frameworks/Clojure/macchiato/hello/src/hello/db.cljs

@@ -43,9 +43,11 @@
       (.then #(handler (js->clj % :keywordize-keys true)))
       (.catch error-handler)))
 
+(defn world-promise [id]
+  (.findOne @worlds (clj->js {:where {:id id} :raw true})))
+
 (defn world [id handler error-handler]
-  (-> @worlds
-      (.findOne (clj->js {:where {:id id} :raw true}))
+  (-> (world-promise id)
       (.then handler)
       (.catch error-handler)))
 
@@ -73,7 +75,7 @@
   "Run the specified number of queries, return the results"
   [queries handler error-handler]
   (-> (get-query-count queries)
-      (repeatedly #(world (unchecked-inc (rand-int 10000)) identity error-handler))
+      (repeatedly #(world-promise (unchecked-inc (rand-int 10000))))
       (js/Promise.all)
       (.then handler)
       (.catch error-handler)))