-
Notifications
You must be signed in to change notification settings - Fork 3
Add initial systemd lifecycle notification support #20
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,4 @@ | ||
| (ns puppetlabs.trapperkeeper.internal | ||
| (:import (clojure.lang ExceptionInfo IFn IDeref) | ||
| (java.lang ArithmeticException NumberFormatException)) | ||
| (:require [clojure.tools.logging :as log] | ||
| [beckon] | ||
| [plumbing.graph :as graph] | ||
|
|
@@ -14,7 +12,14 @@ | |
| [schema.core :as schema] | ||
| [clojure.core.async :as async] | ||
| [clojure.core.async.impl.protocols :as async-prot] | ||
| [me.raynes.fs :as fs])) | ||
| [me.raynes.fs :as fs]) | ||
| (:import | ||
| (clojure.lang ExceptionInfo IFn IDeref) | ||
| (java.lang ArithmeticException NumberFormatException) | ||
| (java.io File) | ||
| (java.net DatagramPacket SocketException) | ||
| ;; Looks like JDK 16+ support doesn't do SOCK_DGRAM yet | ||
| (org.newsclub.net.unix AFUNIXSocketAddress AFUNIXDatagramSocket))) | ||
|
|
||
| ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| ;; Schemas | ||
|
|
@@ -28,6 +33,31 @@ | |
| ;; Private | ||
| ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
|
||
| ;; Optional systemd support | ||
|
|
||
| (def systemd-notify-socket (System/getenv "NOTIFY_SOCKET")) | ||
|
|
||
| (defn maybe-notify-systemd [message] | ||
| (when systemd-notify-socket | ||
| (with-open [s (AFUNIXDatagramSocket/newInstance)] | ||
| (let [buf (.getBytes message "UTF-8") | ||
|
Comment on lines
+42
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The linter shows this: Any idea how to solve those? is
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If trapperkeeper is enforcing "no reflection warnings" globally, then I'd assume those cases just need type hints, e.g. perhaps |
||
| addr (-> systemd-notify-socket File. AFUNIXSocketAddress/of) | ||
| connected? (try | ||
| (.connect s addr) | ||
| true | ||
| ;; Doesn't appear to throw ConnectException | ||
| ;; for an unmonitored socket. | ||
| (catch SocketException _ | ||
| (log/warn (i18n/trs "Unable to connect to NOTIFY_SOCKET {0}" | ||
| (pr-str systemd-notify-socket))) | ||
| nil))] | ||
| (when connected? | ||
| (.send s (DatagramPacket. buf (count buf)))))))) | ||
|
|
||
| (defn notice-service-ready [] (maybe-notify-systemd "READY=1\n")) | ||
| (defn notice-service-reloading [] (maybe-notify-systemd "RELOADING=1\n")) | ||
| (defn notice-service-stopping [] (maybe-notify-systemd "STOPPING=1\n")) | ||
|
|
||
| ;; This is (eww) a global variable that holds a reference to all of the running | ||
| ;; Trapperkeeper apps in the process. It can be used when connecting via nrepl | ||
| ;; to allow you to do useful things, and also may be used for other things | ||
|
|
@@ -615,11 +645,13 @@ | |
| this) | ||
| (a/start [this] | ||
| (run-lifecycle-fns app-context s/start "start" ordered-services) | ||
| (notice-service-ready) | ||
| (inc-restart-counter! this) | ||
| this) | ||
| (a/stop [this] | ||
| (a/stop this false)) | ||
| (a/stop [this throw?] | ||
| (notice-service-stopping) | ||
| (let [errors (shutdown! app-context)] | ||
| (if (and throw? (seq errors)) | ||
| (let [msg (i18n/trs "Error during app shutdown!")] | ||
|
|
@@ -628,10 +660,12 @@ | |
| this))) | ||
| (a/restart [this] | ||
| (try | ||
| (notice-service-reloading) | ||
| (run-lifecycle-fns app-context s/stop "stop" (reverse ordered-services)) | ||
| (doseq [svc-id (keys services-by-id)] (swap! app-context assoc-in [:service-contexts svc-id] {})) | ||
| (run-lifecycle-fns app-context s/init "init" ordered-services) | ||
| (run-lifecycle-fns app-context s/start "start" ordered-services) | ||
| (notice-service-ready) | ||
| (inc-restart-counter! this) | ||
| this | ||
| (catch Throwable t | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.