fix running process

This commit is contained in:
Mylloon 2023-05-13 11:59:09 +02:00
parent 3748b72e6a
commit 4caaf7ee16
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 20 additions and 22 deletions

View file

@ -3,14 +3,15 @@ open Pusk
open Pusk.Net open Pusk.Net
open Pusk.Drivers open Pusk.Drivers
let main = let main pid =
print_endline (fmt "Driver running as %d" pid);
let body = execute_request "http://localhost:4444/session" Json.connection_payload in let body = execute_request "http://localhost:4444/session" Json.connection_payload in
print_endline body print_endline body
;; ;;
let () = let () =
let driver = prepare (Gecko "0.33.0") in let driver = prepare (Gecko "0.33.0") in
let driver_pid = run driver [] in let driver_process = run_process driver [] in
print_endline (fmt "Driver running as %d" driver_pid); main (snd driver_process);
main stop_process driver_process
;; ;;

View file

@ -40,19 +40,6 @@ let download_gecko_driver version output =
download (Uri.of_string (fmt url version version)) output download (Uri.of_string (fmt url version version)) output
;; ;;
let run_program_in_background program args =
let pid = Unix.fork () in
match pid with
| 0 ->
(* Child process *)
let dev_null = Unix.openfile "/dev/null" [ O_WRONLY ] 0o666 in
Unix.dup2 dev_null Unix.stdout;
Unix.dup2 dev_null Unix.stderr;
Unix.close dev_null;
Unix.execvp program (Array.of_list (program :: args))
| _ -> pid (* Parent process *)
;;
type driver = Gecko of string type driver = Gecko of string
let prepare = function let prepare = function
@ -70,9 +57,19 @@ let prepare = function
driver driver
;; ;;
let run path args = let run_process path args =
let pid = run_program_in_background path args in let command = fmt "./%s" path in
(* Wait so we sure the server is up *) let output_file = fmt "%s-output.txt" path in
Unix.sleepf 0.5; let out_channel = open_out output_file in
pid let output_fd = Unix.descr_of_out_channel out_channel in
let pid =
Unix.create_process command (Array.of_list args) output_fd output_fd Unix.stdin
in
out_channel, pid
;;
let stop_process data =
let out_channel, pid = data in
close_out out_channel;
Unix.kill pid Sys.sigterm
;; ;;