Skip to content

Commit

Permalink
service: move mulog publisher inside donut system config
Browse files Browse the repository at this point in the history
  • Loading branch information
practicalli-johnny committed Apr 8, 2024
1 parent 73b6953 commit 98ceecf
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 29 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 2 additions & 8 deletions resources/practicalli/service/dev/mulog_events.clj
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
;; ---------------------------------------------------------
;; 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
(:require
[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

Expand Down
6 changes: 3 additions & 3 deletions resources/practicalli/service/dev/user_donut.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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*))
;; ---------------------------------------------------------

;; ---------------------------------------------------------
Expand Down
18 changes: 5 additions & 13 deletions resources/practicalli/service/src/service_donut.clj.template
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)))))
Expand All @@ -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
Expand Down
18 changes: 14 additions & 4 deletions resources/practicalli/service/src/system_donut.clj.template
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 98ceecf

Please sign in to comment.