Skip to content

Commit 14fb275

Browse files
committed
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
1 parent c71ef46 commit 14fb275

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

riot/lib/logger/logger.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Log = Riot_runtime.Log
33
open Global
44

55
type opts = { print_source : bool; print_time : bool; color_output : bool }
6+
type config = { opts : opts; started_by : Riot_runtime.Core.Pid.t }
67

78
type ('a, 'b) logger_format =
89
(('a, Format.formatter, unit, 'b) format4 -> 'a) -> 'b

riot/lib/logger_app.ml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ open Logger.Make (struct
55
let namespace = [ "riot"; "logger" ]
66
end)
77

8+
type Message.t += Logger_ready
9+
810
module Formatter = struct
911
type Message.t += Log of log
1012

@@ -47,7 +49,8 @@ module Formatter = struct
4749
let pid =
4850
spawn_link (fun () ->
4951
Process.flag (Priority High);
50-
formatter_loop config)
52+
send config.started_by Logger_ready;
53+
formatter_loop config.opts)
5154
in
5255
set_on_log (fun log -> send pid (Log log));
5356
Ok pid
@@ -59,5 +62,12 @@ let default_opts =
5962
{ print_time = true; print_source = false; color_output = true }
6063

6164
let start () =
62-
let child_specs = [ Formatter.child_spec default_opts ] in
63-
Supervisor.start_link ~child_specs ()
65+
let this = self () in
66+
let config = { opts = default_opts; started_by = this } in
67+
let child_specs = [ Formatter.child_spec config ] in
68+
let result = Supervisor.start_link ~child_specs () in
69+
let `ready =
70+
let selector msg = if msg = Logger_ready then `select `ready else `skip in
71+
receive ~selector ()
72+
in
73+
result

0 commit comments

Comments
 (0)