Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
[puppetlabs/kitchensink nil :exclusions [cheshire]]
[puppetlabs/i18n]
[nrepl/nrepl]
[io.github.clj-kondo/config-slingshot-slingshot "1.0.0"]]
[io.github.clj-kondo/config-slingshot-slingshot "1.0.0"]

[com.kohlschutter.junixsocket/junixsocket-core "2.6.1" :extension "pom"]]

:deploy-repositories [["clojars" {:url "https://clojars.org/repo"
:username :env/CLOJARS_USERNAME
Expand Down
40 changes: 37 additions & 3 deletions src/puppetlabs/trapperkeeper/internal.clj
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]
Expand All @@ -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
Expand All @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linter shows this:

== Linting puppetlabs.trapperkeeper.internal ==
src/puppetlabs/trapperkeeper/internal.clj:43:17: reflection: call to method getBytes can't be resolved.
src/puppetlabs/trapperkeeper/internal.clj:44:18: reflection: call to java.io.File ctor can't be resolved.
src/puppetlabs/trapperkeeper/internal.clj:158:39: reflection: reference to field error can't be resolved.

Any idea how to solve those? is org.newsclub.net.unix somehow not pulled in project.cli?

Copy link
Contributor

Choose a reason for hiding this comment

The 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 (.getBytes ^String message "UTF-8"), etc.

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
Expand Down Expand Up @@ -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!")]
Expand All @@ -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
Expand Down
Loading