This commit is contained in:
swrup 2025-11-11 02:07:51 +01:00
parent aa2ff7b2f0
commit 2f3113f55d
11742 changed files with 1223940 additions and 0 deletions

View file

@ -0,0 +1,14 @@
# RPC Client Example
This project contains an executable `rpc_client` which connects to the RPC
server started when Dune is run in watch mode. To use this program, start Dune
in watch mode:
```
$ dune build --watch
```
Then run `rpc_client` from the same directory as `dune` was run from. The
`rpc_client` program will connect to the server (`dune build --watch` starts an
RPC server) and perform several RPC calls to it before instructing the server
(over RPC) to shutdown.

View file

@ -0,0 +1,5 @@
(executable
(name rpc_client)
(package dune-rpc-lwt)
(public_name dune-rpc-lwt-example-client)
(libraries dune-rpc dune-rpc-lwt csexp lwt lwt.unix))

View file

@ -0,0 +1,55 @@
let () =
let init =
Dune_rpc.V1.Initialize.create
~id:(Dune_rpc.V1.Id.make (Csexp.Atom "example_rpc_client"))
in
let where = Dune_rpc_lwt.V1.Where.default ~build_dir:"_build" () in
Lwt_main.run
(let open Lwt.Syntax in
let open Lwt.Infix in
let* chan = Dune_rpc_lwt.V1.connect_chan where in
Dune_rpc_lwt.V1.Client.connect chan init ~f:(fun client ->
print_endline "Sending ping to server...";
let* () =
let* request =
Dune_rpc_lwt.V1.Client.Versioned.prepare_request
client
Dune_rpc.V1.Request.ping
>|= Result.get_ok
in
Dune_rpc_lwt.V1.Client.request client request () >|= Result.get_ok
in
print_endline "Got response from server...";
print_endline "Creating progress stream...";
let* progress_stream =
Dune_rpc_lwt.V1.Client.poll client Dune_rpc.V1.Sub.progress >|= Result.get_ok
in
print_endline "Waiting for next progress event...";
let* progress_event = Dune_rpc_lwt.V1.Client.Stream.next progress_stream in
let message =
match progress_event with
| None -> "(none)"
| Some Success -> "Success"
| Some Failed -> "Failed"
| Some Interrupted -> "Interrupted"
| Some (In_progress { complete; remaining; failed }) ->
Printf.sprintf
"In_progress { complete = %d; remaining = %d; failed = %d }"
complete
remaining
failed
| Some Waiting -> "Waiting"
in
print_endline (Printf.sprintf "Got progress_event: %s" message);
print_endline "Shutting down RPC server...";
let* () =
let* shutdown_notification =
Dune_rpc_lwt.V1.Client.Versioned.prepare_notification
client
Dune_rpc.V1.Notification.shutdown
>|= Result.get_ok
in
Dune_rpc_lwt.V1.Client.notification client shutdown_notification ()
in
Lwt.return ()))
;;

View file

@ -0,0 +1,3 @@
(cram
(applies_to :whole_subtree)
(deps %{bin:dune} ../rpc_client.exe))

View file

@ -0,0 +1,2 @@
(executable
(name hello))

View file

@ -0,0 +1,21 @@
Start Dune in watch mode, sending errors to `/dev/null` to suppress the alerts
about using the unstable module Dune_rpc.
$ export XDG_STATE_HOME="$PWD/.dune.state"
$ mkdir $XDG_STATE_HOME
$ dune build -w 2> /dev/null &
Wait for the program produced by the above step to exist.
$ while ! test -f _build/default/hello.exe; do sleep 1; done
Run the program. The program takes care of shutting down the build server.
$ ../../rpc_client.exe
Sending ping to server...
Got response from server...
Creating progress stream...
Waiting for next progress event...
Got progress_event: Success
Shutting down RPC server...
$ wait