solved some TODOs about Tgeneric type arguments (2)
[hiphop-php.git] / hphp / hack / src / monitor / serverProgress.ml
blob5bfb16c1c0dd7ef97f3513a5879346544414393a
1 (*
2 * Copyright (c) 2018, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE fn in the "hack" directory of this source tree.
8 *)
10 type pipe_from_server = Unix.file_descr
12 let make_pipe_from_server fd = fd
14 let read_from_server fd =
15 try
16 let (readable, _, _) = Unix.select [fd] [] [] 0.0 in
17 if readable = [] then
18 None
19 else
20 Some (Marshal_tools.from_fd_with_preamble fd)
21 with e ->
22 (* If something went wrong here, the system is likely in broken state
23 * (the server died). We'll keep going so that monitor
24 * can resolve this (by restarting the server / exiting itself *)
25 let stack = Printexc.get_backtrace () in
26 Hh_logger.exc stack e;
27 None
29 let pipe_to_monitor_ref = ref None
31 let previous_message = ref None
33 let make_pipe_to_monitor fd = pipe_to_monitor_ref := Some fd
35 let send_to_monitor (msg : MonitorRpc.server_to_monitor_message) =
36 match !pipe_to_monitor_ref with
37 | None -> ()
38 (* This function can be invoked in non-server code paths,
39 * when there is no monitor. *)
40 | Some fd ->
41 begin
42 match (msg, !previous_message) with
43 | (msg, Some previous_message) when msg = previous_message ->
44 (* Avoid sending the same message repeatedly. *)
46 | _ ->
47 previous_message := Some msg;
48 let (_ : int) = Marshal_tools.to_fd_with_preamble fd msg in
50 end
52 let send_progress_to_monitor ?(include_in_logs = true) fmt =
53 let f s =
54 if include_in_logs then Hh_logger.log "%s" s;
55 send_to_monitor (MonitorRpc.PROGRESS s)
57 Printf.ksprintf f fmt
59 (* The message will look roughly like this:
60 <operation> <done_count>/<total_count> <unit> <percent done> <extra>*)
61 let make_percentage_progress_message
62 ~(operation : string)
63 ~(done_count : int)
64 ~(total_count : int)
65 ~(unit : string)
66 ~(extra : string option) : string =
67 let unit =
68 if unit = "" then
69 unit
70 else
71 unit ^ " "
73 let percent = 100.0 *. float_of_int done_count /. float_of_int total_count in
74 let main_message =
75 Printf.sprintf
76 "%s %d/%d %s(%.1f%%)"
77 operation
78 done_count
79 total_count
80 unit
81 percent
83 match extra with
84 | Some extra -> main_message ^ " " ^ extra
85 | None -> main_message
87 let send_percentage_progress_to_monitor
88 ~(operation : string)
89 ~(done_count : int)
90 ~(total_count : int)
91 ~(unit : string)
92 ~(extra : string option) : unit =
93 send_progress_to_monitor
94 ~include_in_logs:false
95 "%s"
96 (make_percentage_progress_message
97 ~operation
98 ~done_count
99 ~total_count
100 ~unit
101 ~extra)