Source file async_command.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
open Core
open Import
include Core.Command
type 'a with_options = ?behave_nicely_in_pipeline:bool -> ?extract_exn:bool -> 'a
let shutdown_with_error e =
Stdlib.at_exit (fun () ->
Core.prerr_endline (Error.to_string_hum e));
shutdown 1
;;
let maybe_print_error_and_shutdown = function
| Ok () -> shutdown 0
| Error e -> shutdown_with_error e
;;
let in_async ?(behave_nicely_in_pipeline = true) ? param on_result =
Param.map param ~f:(fun staged_main () ->
if behave_nicely_in_pipeline then Writer.behave_nicely_in_pipeline ();
let main = Or_error.try_with (fun () -> unstage (staged_main ())) in
match main with
| Error e ->
shutdown_with_error e;
(never_returns (Scheduler.go ()) : unit)
| Ok main ->
let before_shutdown () =
Deferred.List.iter
~how:`Parallel
Writer.[ force stdout; force stderr ]
~f:(fun writer ->
Deferred.any_unit
[ Writer.close_finished writer
; Writer.consumer_left writer
; Writer.flushed writer
])
in
Shutdown.at_shutdown before_shutdown;
Shutdown.set_default_force
(let prev = Shutdown.default_force () in
fun () ->
Deferred.all_unit
[ prev (); (before_shutdown () >>= fun () -> after (sec 1.)) ]);
upon
(Deferred.Or_error.try_with
~run:`Schedule
~rest:`Log
?extract_exn
(fun () -> main `Scheduler_started))
on_result;
(never_returns (Scheduler.go ()) : unit))
;;
type 'r staged = ([ `Scheduler_started ] -> 'r) Staged.t
module Staged = struct
let async ?behave_nicely_in_pipeline ? ~summary ?readme param =
let on_result = maybe_print_error_and_shutdown in
basic
~summary
?readme
(in_async ?behave_nicely_in_pipeline ?extract_exn param on_result)
;;
let async_spec ?behave_nicely_in_pipeline ? ~summary ?readme spec main =
async
?behave_nicely_in_pipeline
?extract_exn
~summary
?readme
(Spec.to_param spec main)
;;
let async_or_error ?behave_nicely_in_pipeline ? ~summary ?readme param =
let on_result res = maybe_print_error_and_shutdown (Or_error.join res) in
basic
~summary
?readme
(in_async ?behave_nicely_in_pipeline ?extract_exn param on_result)
;;
let async_spec_or_error
?behave_nicely_in_pipeline
?
~summary
?readme
spec
main
=
async_or_error
?behave_nicely_in_pipeline
?extract_exn
~summary
?readme
(Spec.to_param spec main)
;;
end
let stage_param = Param.map ~f:(fun main () -> stage (fun `Scheduler_started -> main ()))
let async ?behave_nicely_in_pipeline ? ~summary ?readme param =
Staged.async
?behave_nicely_in_pipeline
?extract_exn
~summary
?readme
(stage_param param)
;;
let async_or_error ?behave_nicely_in_pipeline ? ~summary ?readme param =
Staged.async_or_error
?behave_nicely_in_pipeline
?extract_exn
~summary
?readme
(stage_param param)
;;
let async_spec ?behave_nicely_in_pipeline ? ~summary ?readme spec main =
async ?behave_nicely_in_pipeline ?extract_exn ~summary ?readme (Spec.to_param spec main)
;;
let async_spec_or_error ?behave_nicely_in_pipeline ? ~summary ?readme spec main
=
async_or_error
?behave_nicely_in_pipeline
?extract_exn
~summary
?readme
(Spec.to_param spec main)
;;