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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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
;;
module Kind = struct
type t =
| Unit of ([ `Scheduler_started ] -> unit Deferred.t)
| Or_error of ([ `Scheduler_started ] -> unit Or_error.t Deferred.t)
[@@deriving variants]
end
let recursive_invocation = ref None
let in_async ?(behave_nicely_in_pipeline = true) ? param on_result kind =
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 !recursive_invocation with
| Some r -> Set_once.set_exn r [%here] (Or_error.map ~f:kind main)
| None ->
(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 Kind.unit)
;;
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 Kind.or_error)
;;
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)
;;
module For_testing = struct
let run_from_within_async ~argv cmd =
let r = Set_once.create () in
match !recursive_invocation with
| Some _ ->
raise_s [%message "cannot nest recursive command invocations during parsing"]
| None ->
recursive_invocation := Some r;
(match
Or_error.try_with (fun () ->
Exn.protect
~finally:(fun () -> recursive_invocation := None)
~f:(fun () -> Command_unix.run ~argv cmd))
with
| Error _ as e -> return e
| Ok () ->
(match Set_once.get r with
| None -> return (Ok ())
| Some (Error _ as e) -> return e
| Some (Ok kind) ->
Monitor.try_with_join_or_error (fun () ->
match kind with
| Unit thunk ->
let%map.Deferred () = thunk `Scheduler_started in
Ok ()
| Or_error thunk -> thunk `Scheduler_started)))
;;
end