From 14fb2757117b6d3459177af1d06004bb01284c38 Mon Sep 17 00:00:00 2001 From: Leandro Ostera Date: Tue, 16 Jul 2024 13:31:05 +0200 Subject: [PATCH] fix: logger app blocks until ready This change makes the Logger application startup process _block_ until the logger is ready to take in requests. This means applications relying on the logger will take a tiny bit longer to boot, but we can guarantee that the log requests will not be dropped because of process spawn order. Close #82 --- riot/lib/logger/logger.ml | 1 + riot/lib/logger_app.ml | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/riot/lib/logger/logger.ml b/riot/lib/logger/logger.ml index eb26c057..245b7ebf 100644 --- a/riot/lib/logger/logger.ml +++ b/riot/lib/logger/logger.ml @@ -3,6 +3,7 @@ module Log = Riot_runtime.Log open Global type opts = { print_source : bool; print_time : bool; color_output : bool } +type config = { opts : opts; started_by : Riot_runtime.Core.Pid.t } type ('a, 'b) logger_format = (('a, Format.formatter, unit, 'b) format4 -> 'a) -> 'b diff --git a/riot/lib/logger_app.ml b/riot/lib/logger_app.ml index 10b324f7..85486dbb 100644 --- a/riot/lib/logger_app.ml +++ b/riot/lib/logger_app.ml @@ -5,6 +5,8 @@ open Logger.Make (struct let namespace = [ "riot"; "logger" ] end) +type Message.t += Logger_ready + module Formatter = struct type Message.t += Log of log @@ -47,7 +49,8 @@ module Formatter = struct let pid = spawn_link (fun () -> Process.flag (Priority High); - formatter_loop config) + send config.started_by Logger_ready; + formatter_loop config.opts) in set_on_log (fun log -> send pid (Log log)); Ok pid @@ -59,5 +62,12 @@ let default_opts = { print_time = true; print_source = false; color_output = true } let start () = - let child_specs = [ Formatter.child_spec default_opts ] in - Supervisor.start_link ~child_specs () + let this = self () in + let config = { opts = default_opts; started_by = this } in + let child_specs = [ Formatter.child_spec config ] in + let result = Supervisor.start_link ~child_specs () in + let `ready = + let selector msg = if msg = Logger_ready then `select `ready else `skip in + receive ~selector () + in + result