Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(riot): add run_with_status function where main can return a status code/error #61

Merged
merged 10 commits into from
Mar 21, 2024
16 changes: 16 additions & 0 deletions riot/riot.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ let run ?(rnd = Random.State.make_self_init ()) ?workers main =
Log.debug (fun f -> f "Riot runtime shutdown");
Stdlib.exit pool.status

let on_error (error : [ `Msg of string ]) =
let backtrace = Printexc.get_backtrace () in
let error_string =
match error with `Msg reason -> Printf.sprintf "%s\n%s" reason backtrace
in
Log.error (fun f -> f "Riot raised an error: %s\n" error_string);
1

let run_with_status ?(rnd = Random.State.make_self_init ()) ?workers ~on_error
main =
run ~rnd ?workers (fun _ ->
let status =
match main () with Ok code -> code | Error reason -> on_error reason
in
shutdown ~status ())

let start ?rnd ?workers ~apps () =
run ?rnd ?workers @@ fun () ->
let child_specs =
Expand Down
15 changes: 15 additions & 0 deletions riot/riot.mli
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,21 @@ val shutdown : ?status:int -> unit -> unit
val run : ?rnd:Random.State.t -> ?workers:int -> (unit -> unit) -> unit
(** Start the Riot runtime using function [main] to boot the system *)

val on_error : [ `Msg of string ] -> int

val run_with_status :
?rnd:Random.State.t ->
?workers:int ->
on_error:('error -> int) ->
(unit -> (int, 'error) result) ->
unit
(** [run_with_status ~on_error main] starts the Riot runtime using function
[main] to boot the system and handling errors with [on_error].

[main] should return a result of either an exit code or an error.
[on_error] should handle an error code appropriately, then return a status code.
*)

val start :
?rnd:Random.State.t ->
?workers:int ->
Expand Down