|
7 | 7 | [puppetlabs.i18n.core :as i18n])
|
8 | 8 | (:import (clojure.lang IFn IDeref)
|
9 | 9 | (puppetlabs.services.jruby_pool_manager.jruby_schemas PoisonPill JRubyInstance)
|
10 |
| - (java.util.concurrent TimeUnit TimeoutException))) |
| 10 | + (java.util.concurrent TimeUnit TimeoutException ExecutionException Future ExecutorService))) |
11 | 11 |
|
12 | 12 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
13 | 13 | ;;; Private
|
14 | 14 |
|
| 15 | +(schema/defn execute-tasks! |
| 16 | + [tasks :- [IFn] |
| 17 | + task-executor :- ExecutorService] |
| 18 | + (let [results (.invokeAll task-executor tasks)] |
| 19 | + (try |
| 20 | + (doseq [result results] |
| 21 | + (.get ^Future result)) |
| 22 | + (catch ExecutionException ex |
| 23 | + (throw (.getCause ex)))))) |
| 24 | + |
15 | 25 | (schema/defn ^:always-validate
|
16 | 26 | next-instance-id :- schema/Int
|
17 | 27 | [id :- schema/Int
|
|
68 | 78 | (i18n/trs "Initializing JRubyInstances with the following settings:")
|
69 | 79 | (ks/pprint-to-string config)))
|
70 | 80 | (let [pool (jruby-internal/get-pool pool-context)
|
71 |
| - count (.remainingCapacity pool)] |
72 |
| - (dotimes [i count] |
73 |
| - (let [id (inc i)] |
74 |
| - (log/debug (i18n/trs "Priming JRubyInstance {0} of {1}" |
75 |
| - id count)) |
76 |
| - (add-instance pool-context id) |
77 |
| - (log/info (i18n/trs "Finished creating JRubyInstance {0} of {1}" |
78 |
| - id count)))))) |
79 |
| - |
| 81 | + creation-service (jruby-internal/get-creation-service pool-context) |
| 82 | + total (.remainingCapacity pool) |
| 83 | + ids (->> total range (map inc)) |
| 84 | + add-instance* (fn [id] |
| 85 | + (log/debug (i18n/trs "Priming JRubyInstance {0} of {1}" |
| 86 | + id count)) |
| 87 | + (add-instance pool-context id) |
| 88 | + (log/info (i18n/trs "Finished creating JRubyInstance {0} of {1}" |
| 89 | + id count))) |
| 90 | + tasks (for [id ids] (fn [] (add-instance* id)))] |
| 91 | + (execute-tasks! tasks creation-service))) |
80 | 92 |
|
81 | 93 | (schema/defn ^:always-validate
|
82 | 94 | flush-instance!
|
|
148 | 160 | refill? :- schema/Bool]
|
149 | 161 | (let [pool (jruby-internal/get-pool pool-context)
|
150 | 162 | pool-size (jruby-internal/get-pool-size pool-context)
|
| 163 | + creation-service (jruby-internal/get-creation-service pool-context) |
151 | 164 | new-instance-ids (map inc (range pool-size))
|
152 | 165 | config (:config pool-context)
|
153 |
| - cleanup-fn (get-in config [:lifecycle :cleanup])] |
154 |
| - (doseq [[old-instance new-id] (zipmap old-instances new-instance-ids)] |
155 |
| - (try |
156 |
| - (jruby-internal/cleanup-pool-instance! old-instance cleanup-fn) |
157 |
| - (when refill? |
158 |
| - (jruby-internal/create-pool-instance! pool new-id config |
159 |
| - (:splay-instance-flush config)) |
160 |
| - (log/info (i18n/trs "Finished creating JRubyInstance {0} of {1}" |
161 |
| - new-id pool-size))) |
162 |
| - (catch Exception e |
163 |
| - (.clear pool) |
164 |
| - (jruby-internal/insert-poison-pill pool e) |
165 |
| - (throw (IllegalStateException. |
166 |
| - (i18n/trs "There was a problem creating a JRubyInstance for the pool.") |
167 |
| - e)))))) |
| 166 | + cleanup-fn (get-in config [:lifecycle :cleanup]) |
| 167 | + cleanup-and-refill-instance |
| 168 | + (fn [old-instance new-id] |
| 169 | + (try |
| 170 | + (jruby-internal/cleanup-pool-instance! old-instance cleanup-fn) |
| 171 | + (when refill? |
| 172 | + (jruby-internal/create-pool-instance! pool new-id config |
| 173 | + (:splay-instance-flush config)) |
| 174 | + (log/info (i18n/trs "Finished creating JRubyInstance {0} of {1}" |
| 175 | + new-id pool-size))) |
| 176 | + (catch Exception e |
| 177 | + (.clear pool) |
| 178 | + (jruby-internal/insert-poison-pill pool e) |
| 179 | + (throw (IllegalStateException. |
| 180 | + (i18n/trs "There was a problem creating a JRubyInstance for the pool.") |
| 181 | + e))))) |
| 182 | + cleanup-and-refill-tasks (for [[old-instance new-id] (zipmap old-instances new-instance-ids)] |
| 183 | + (fn [] (cleanup-and-refill-instance old-instance new-id)))] |
| 184 | + (execute-tasks! cleanup-and-refill-tasks creation-service)) |
168 | 185 | (if refill?
|
169 | 186 | (log/info (i18n/trs "Finished draining and refilling pool."))
|
170 | 187 | (log/info (i18n/trs "Finished draining pool."))))
|
|
0 commit comments