Permalink
Browse files

Merge pull request #205 from cprice-puppet/feature/master/shutdown-ho…

…ok-with-log-msg

Add a shutdown hook with a log message
  • Loading branch information...
2 parents f6ce313 + 2d40c52 commit c7b4bb10d9a108b821de228e5527c37e594f0e7d @grimradical grimradical committed Jul 6, 2012
Showing with 20 additions and 0 deletions.
  1. +9 −0 src/com/puppetlabs/puppetdb/cli/services.clj
  2. +11 −0 src/com/puppetlabs/utils.clj
View
9 src/com/puppetlabs/puppetdb/cli/services.clj
@@ -189,6 +189,12 @@
(configure-database)
(set-global-configuration!))))
+(defn on-shutdown
+ "General cleanup when a shutdown request is received."
+ []
+ ;; nothing much to do here for now, but let's at least log that we're shutting down.
+ (log/info "Shutdown request received; puppetdb exiting."))
+
(defn -main
[& args]
(let [[options _] (cli! args)
@@ -206,6 +212,9 @@
(when version
(log/info (format "PuppetDB version %s" version)))
+ ;; Add a shutdown hook where we can handle any required cleanup
+ (pl-utils/add-shutdown-hook! on-shutdown)
+
;; Ensure the database is migrated to the latest version
(sql/with-connection db
(migrate!))
View
11 src/com/puppetlabs/utils.clj
@@ -264,6 +264,17 @@
(f thread exception)))]
(Thread/setDefaultUncaughtExceptionHandler handler))))
+(defn add-shutdown-hook!
+ "Adds a shutdown hook to the JVM runtime.
+
+ `f` is a function that takes 0 arguments; the return value is ignored. This
+ function will be called if the JVM receiveds an interrupt signal (e.g. from
+ `kill` or CTRL-C); you can use it to log shutdown messages, handle state
+ cleanup, etc."
+ [f]
+ {:pre [(fn? f)]}
+ (.addShutdownHook (Runtime/getRuntime) (Thread. f)))
+
(defn configure-logger-via-file!
"Reconfigures the current logger based on the supplied configuration
file. You can optionally supply a delay (in millis) that governs how

0 comments on commit c7b4bb1

Please sign in to comment.