|
176 | 176 | required []] |
177 | 177 | (first (ks/cli! cli-args specs required)))) |
178 | 178 |
|
| 179 | +(def exit-request-schema |
| 180 | + "A process exit request like |
| 181 | + {:status 7 |
| 182 | + :messages [[\"something for stderr\n\" *err*]] |
| 183 | + [\"something for stdout\n\" *out*]] |
| 184 | + [\"something else for stderr\n\" *err*]]" |
| 185 | + {:status schema/Int |
| 186 | + :messages [[(schema/one schema/Str "message") |
| 187 | + (schema/one java.io.Writer "stream")]]}) |
| 188 | + |
| 189 | +(defn exit-exception? [ex] |
| 190 | + (and (instance? ExceptionInfo ex) |
| 191 | + (not (schema/check {(schema/optional-key :puppetlabs.trapperkeeper.core/exit) |
| 192 | + exit-request-schema} |
| 193 | + (ex-data ex))))) |
| 194 | + |
| 195 | +(defn shutdown-reason-for-ex |
| 196 | + [exception] |
| 197 | + (if (exit-exception? exception) |
| 198 | + (merge {:cause :requested} |
| 199 | + (select-keys (ex-data exception) [:puppetlabs.trapperkeeper.core/exit])) |
| 200 | + {:cause :service-error :error exception})) |
| 201 | + |
179 | 202 | (schema/defn ^:always-validate run-lifecycle-fn! |
180 | 203 | "Run a lifecycle function for a service. Required arguments: |
181 | 204 |
|
|
234 | 257 | (log/debug (i18n/trs "Finished running lifecycle function ''{0}'' for service ''{1}''" |
235 | 258 | lifecycle-fn-name |
236 | 259 | service-id))) |
237 | | - (catch Throwable t |
238 | | - (log/error t (i18n/trs "Error during service {0}!!!" lifecycle-fn-name)) |
239 | | - (throw t)))) |
| 260 | + (catch ExceptionInfo ex |
| 261 | + (if (exit-exception? ex) |
| 262 | + (log/info (i18n/trs "Immediate shutdown requested during service {0}" |
| 263 | + lifecycle-fn-name)) |
| 264 | + (log/error ex (i18n/trs "Error during service {0}!!!" lifecycle-fn-name))) |
| 265 | + (throw ex)) |
| 266 | + (catch Throwable ex |
| 267 | + (log/error ex (i18n/trs "Error during service {0}!!!" lifecycle-fn-name)) |
| 268 | + (throw ex)))) |
240 | 269 |
|
241 | 270 | (schema/defn ^:always-validate initialize-lifecycle-worker :- (schema/protocol async-prot/Channel) |
242 | 271 | "Initializes a 'worker' which will listen for lifecycle-related tasks and perform |
|
286 | 315 | (log/debug (i18n/trs "Lifecycle worker completed {0} lifecycle task; awaiting next task." type)) |
287 | 316 | (catch Exception e |
288 | 317 | (log/debug e (i18n/trs "Exception caught in lifecycle worker loop")) |
289 | | - (deliver shutdown-reason-promise |
290 | | - {:cause :service-error |
291 | | - :error e}))) |
| 318 | + (deliver shutdown-reason-promise (shutdown-reason-for-ex e)))) |
292 | 319 | (recur)) |
293 | 320 |
|
294 | 321 | (do |
|
345 | 372 | ;;;; regarding the cause of the shutdown, and is intended to be passed back |
346 | 373 | ;;;; in to the top-level functions that perform various shutdown steps. |
347 | 374 |
|
348 | | -(def exit-request-schema |
349 | | - "A process exit request like |
350 | | - {:status 7 |
351 | | - :messages [[\"something for stderr\n\" *err*]] |
352 | | - [\"something for stdout\n\" *out*]] |
353 | | - [\"something else for stderr\n\" *err*]]" |
354 | | - {:status schema/Int |
355 | | - :messages [[(schema/one schema/Str "message") |
356 | | - (schema/one java.io.Writer "stream")]]}) |
357 | | - |
358 | 375 | (def ^{:private true |
359 | 376 | :doc "The possible causes for shutdown to be initiated."} |
360 | 377 | shutdown-causes #{:requested :service-error :jvm-shutdown-hook}) |
|
635 | 652 | (inc-restart-counter! this) |
636 | 653 | this |
637 | 654 | (catch Throwable t |
638 | | - (deliver shutdown-reason-promise {:cause :service-error |
639 | | - :error t}))))))) |
| 655 | + (deliver shutdown-reason-promise (shutdown-reason-for-ex t)))))))) |
640 | 656 |
|
641 | 657 | (schema/defn ^:always-validate boot-services-for-app** |
642 | 658 | "Boots services for a TK app. WARNING: This should only ever be called |
|
648 | 664 | (a/init app) |
649 | 665 | (a/start app) |
650 | 666 | (catch Throwable t |
651 | | - (deliver shutdown-reason-promise {:cause :service-error |
652 | | - :error t}))) |
| 667 | + (deliver shutdown-reason-promise (shutdown-reason-for-ex t)))) |
653 | 668 | (deliver result-promise app))) |
654 | 669 |
|
655 | 670 | (schema/defn ^:always-validate boot-services-for-app* :- (schema/protocol a/TrapperkeeperApp) |
|
0 commit comments