diff --git a/CHANGELOG.md b/CHANGELOG.md index 51bee8c..cd0547b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file. This change # [Unreleased] ## Changed - dev: repository trivy ignore CVE-2024-22871 - +- service: move mulog publisher inside donut system config # 2024-01-23 ## Changed diff --git a/resources/practicalli/service/dev/mulog_events.clj b/resources/practicalli/service/dev/mulog_events.clj index 13d6741..ba0d386 100644 --- a/resources/practicalli/service/dev/mulog_events.clj +++ b/resources/practicalli/service/dev/mulog_events.clj @@ -1,9 +1,10 @@ ;; --------------------------------------------------------- ;; Mulog Global Context and Custom Publisher ;; -;; - set event log global context ;; - tap publisher for use with Portal and other tap sources ;; - publish all mulog events to Portal tap source +;; +;; NOTE: Mulog global context set in system configuration ;; --------------------------------------------------------- (ns mulog-events @@ -11,13 +12,6 @@ [com.brunobonacci.mulog :as mulog] [com.brunobonacci.mulog.buffer :as mulog-buffer])) -;; --------------------------------------------------------- -;; Set event global context -;; - information added to every event for REPL workflow -(mulog/set-global-context! {:app-name "{{main/ns}} Service", - :version "0.1.0", :env "dev"}) -;; --------------------------------------------------------- - ;; --------------------------------------------------------- ;; Mulog event publishing diff --git a/resources/practicalli/service/dev/user_donut.clj b/resources/practicalli/service/dev/user_donut.clj index 7a4b070..11caa37 100644 --- a/resources/practicalli/service/dev/user_donut.clj +++ b/resources/practicalli/service/dev/user_donut.clj @@ -76,9 +76,9 @@ ;; and set global context for all events ;; Example mulog event message -(mulog/log ::dev-user-ns - :message "Example event from user namespace" - :ns (ns-publics *ns*)) +#_(mulog/log ::dev-user-ns + :message "Example event from user namespace" + :ns (ns-publics *ns*)) ;; --------------------------------------------------------- ;; --------------------------------------------------------- diff --git a/resources/practicalli/service/src/service_donut.clj.template b/resources/practicalli/service/src/service_donut.clj.template index 06fef4a..dc55c6a 100644 --- a/resources/practicalli/service/src/service_donut.clj.template +++ b/resources/practicalli/service/src/service_donut.clj.template @@ -20,18 +20,13 @@ "Gameboard service component lifecycle management" (:gen-class) (:require - ;; Component system [donut.system :as donut] - [{{top/ns}}.{{main/ns}}.system :as system] - - ;; System dependencies - [org.httpkit.server :as http-server] - [com.brunobonacci.mulog :as mulog])) + [{{top/ns}}.{{main/ns}}.system :as system])) ;; -------------------------------------------------- -;; Application entry point +;; Service entry point (defn -main "{{top/ns}} {{main/ns}} service managed by donut system, @@ -40,15 +35,13 @@ The shutdown hook gracefully stops the service on receipt of a SIGTERM from the infrastructure, giving the application 30 seconds before forced termination." [] - - (mulog/set-global-context! - {:app-name "{{top/ns}} {{main/ns}} service" :version "0.1.0"}) - (let [profile (or (keyword (System/getenv "SERVICE_PROFILE")) :dev) + ;; Reference to running system for shutdown hook running-system (donut/start (or (profile :profile) :prod))] - + + ;; Shutdown system components on SIGTERM (.addShutdownHook (Runtime/getRuntime) (Thread. ^Runnable #(donut/signal running-system ::donut/stop))))) @@ -58,7 +51,6 @@ ;; -------------------------------------------------- ;; Example clojure.exec function -#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} (defn greet "Greeting message via Clojure CLI clojure.exec" ;; TODO: call greet with hash-map argument diff --git a/resources/practicalli/service/src/system_donut.clj.template b/resources/practicalli/service/src/system_donut.clj.template index 335db7b..b8139d7 100644 --- a/resources/practicalli/service/src/system_donut.clj.template +++ b/resources/practicalli/service/src/system_donut.clj.template @@ -29,7 +29,9 @@ {::donut/defs ;; Option: move :env data to resources/config.edn and parse with aero reader {:env - {:http-port 8080 + {:app-version "0.1.0" + :app-env "prod" + :http-port (or (System/getenv "SERVICE_HTTP_PORT") 8080) :persistence {:database-host (or (System/getenv "POSTGRES_HOST") "http://localhost") :database-port (or (System/getenv "POSTGRES_PORT") "5432") @@ -52,7 +54,8 @@ :event-log {:publisher #::donut{:start (fn mulog-publisher-start - [{{:keys [publisher]} ::donut/config}] + [{{:keys [global-context publisher]} ::donut/config}] + (mulog/set-global-context! global-context) (mulog/log ::log-publish-component :publisher-config publisher :local-time (java.time.LocalDateTime/now)) @@ -65,7 +68,14 @@ (Thread/sleep 250) (instance)) - :config {:publisher {:type :console :pretty? true}}}} + :config {:global-context {:app-name "{{top/ns}} {{main/ns}} service" + :version (donut/ref [:env :app-version]) + :environment (donut/ref [:env :app-env])} + ;; Publish events to console in json format + ;; optionally add `:transform` function to filter events before publishing + :publisher {:type :console-json + :pretty? false + #_#_:transform identity}}}} ;; HTTP server start - returns function to stop the server :http @@ -87,7 +97,7 @@ :config {:handler (donut/local-ref [:handler]) :options {:port (donut/ref [:env :http-port]) - :join? false}}} + :join? true}}} ;; Function handling all requests, passing system environment ;; Configure environment for router application, e.g. database connection details, etc.