6 let (/) = Filename.concat
9 match Sys.command("mkdir -p " ^ dir) with
13 (sprintf "Failed to create directory: %S. mkdir status: %d\n" dir n)
16 match Sys.command("gzip " ^ path) with
20 (sprintf "Failed to gzip path: %S. gzip status: %d\n" path n)
22 let main ~node ~cache ~dump_interval:interval ~dump_directory =
23 mkdir_p dump_directory;
24 let dump_filename = dump_directory / "khatus-cache-dump.psv.gz" in
25 let rec loop ~time ~time_dumped =
26 (match read_line () with
27 | exception End_of_file ->
30 (match Msg_parser.parse_msg (Lexing.from_string line) with
32 let time = Msg.next_time msg ~node ~time in
33 Cache.update_if_data cache ~msg ~time;
34 if (Time.Span.is_gt_or_eq (Time.diff time_dumped time) interval)
37 let (tmp_filename, oc) =
38 Filename.open_temp_file "khatus-cache" "dump"
40 Cache.dump cache ~node ~modul ~oc;
43 Sys.rename (tmp_filename ^ ".gz") dump_filename;
44 loop ~time ~time_dumped:time
47 loop ~time ~time_dumped
51 | `Bad_format_of_msg_head -> "Bad_format_of_msg_head"
52 | `Bad_format_of_msg_content -> "Bad_format_of_msg_content"
60 { location = "main:loop"
62 ; msg = sprintf "Parse error %s in %s" e line
65 loop ~time ~time_dumped
69 loop ~time:Time.init ~time_dumped:Time.init
74 ~dump_interval:(Time.Span.of_string Sys.argv.(2))
75 ~dump_directory:(Sys.argv.(3))
76 ~cache:(Cache.create ())