Skip to content

Commit

Permalink
irmin: Integrate irmin-watcher changes to eio
Browse files Browse the repository at this point in the history
  • Loading branch information
clecat committed Sep 10, 2024
1 parent 9046cf5 commit 64df2de
Show file tree
Hide file tree
Showing 25 changed files with 119 additions and 69 deletions.
4 changes: 2 additions & 2 deletions examples/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

let root = "/tmp/irmin/test"

let init () =
let init ~sw ~path () =
let _ = Sys.command (Printf.sprintf "rm -rf %s" root) in
let _ = Sys.command (Printf.sprintf "mkdir -p %s" root) in
Irmin.Backend.Watch.set_listen_dir_hook Irmin_watcher.hook
Irmin.Backend.Watch.set_listen_dir_eio_hook ~sw path Irmin_watcher.hook
7 changes: 4 additions & 3 deletions examples/custom_graphql.ml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ end
module Server =
Irmin_graphql_unix.Server.Make_ext (Store) (Remote) (Custom_types)

let main () =
Config.init ();
let main env =
Eio.Switch.run @@ fun sw ->
Config.init ~sw ~path:(Eio.Stdenv.cwd env) ();
let config = Irmin_git.config Config.root in
let repo = Store.Repo.v config in
let server = Server.v repo in
Expand All @@ -122,4 +123,4 @@ let main () =

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main env
7 changes: 4 additions & 3 deletions examples/custom_merge.ml
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ let print_logs name t =
let logs = all_logs t in
Fmt.pr "-----------\n%s:\n-----------\n%a%!" name (Irmin.Type.pp Log.t) logs

let main () =
Config.init ();
let main env =
Eio.Switch.run @@ fun sw ->
Config.init ~sw ~path:(Eio.Stdenv.cwd env) ();
let repo = Store.Repo.v config in
let t = Store.main repo in

Expand All @@ -160,4 +161,4 @@ let main () =

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main env
11 changes: 6 additions & 5 deletions examples/deploy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ let info ~user message () =
Store.Info.v ~author ~message date

(* 1. Cloning the gold image. *)
let provision repo =
Config.init ();
let provision env repo =
Eio.Switch.run @@ fun sw ->
Config.init ~sw ~path:(Eio.Stdenv.cwd env) ();
let provision = info ~user:"Automatic VM provisioning" in
let t = Store.of_branch repo "upstream" in
let v =
Expand Down Expand Up @@ -80,7 +81,7 @@ let revert repo =
Eio_unix.sleep 2.;
Store.Head.set prod h2)

let main () =
let main env =
let cmd = Sys.argv.(0) in
let help () =
Printf.eprintf
Expand All @@ -106,7 +107,7 @@ let main () =
match Sys.argv.(1) with
| "provision" ->
(let repo = Store.Repo.v config in
provision repo);
provision env repo);
Printf.printf
"The VM is now provisioned. Run `%s configure` to simulate a sysadmin \n\
configuration.\n"
Expand All @@ -133,4 +134,4 @@ let main () =

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main env
11 changes: 6 additions & 5 deletions examples/irmin_git_store.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ let read_exn t k =
print_endline msg;
Store.get t k

let main () =
Config.init ();
let main env =
Eio.Switch.run @@ fun sw ->
Config.init ~sw ~path:(Eio.Stdenv.cwd env) ();
let config = Irmin_git.config ~bare:true Config.root in
let repo = Store.Repo.v config in
let t = Store.main repo in
Expand All @@ -52,16 +53,16 @@ let main () =
let _ = read_exn t [ "root"; "misc"; "3.txt" ] in
()

let main () =
let main env =
Printf.printf
"This example creates a Git repository in %s and use it to read \n\
and write data:\n"
Config.root;
let _ = Sys.command (Printf.sprintf "rm -rf %s" Config.root) in
main ();
main env;
Printf.printf "You can now run `cd %s && tig` to inspect the store.\n"
Config.root

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main env
11 changes: 6 additions & 5 deletions examples/process.ml
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ let info image message () =

let main = branch images.(0)

let init () =
Config.init ();
let init env =
Eio.Switch.run @@ fun sw ->
Config.init ~sw ~path:(Eio.Stdenv.cwd env) ();
let repo = Store.Repo.v config in
let t = Store.of_branch repo main in
Store.set_exn t ~info:(info images.(0) "init") [ "0" ] "0";
Expand Down Expand Up @@ -162,10 +163,10 @@ let rec watchdog () =
Eio_unix.sleep 1.;
watchdog ()

let main () =
init ();
let main env =
init env;
Eio.Fiber.any (watchdog :: List.map (protect process) (Array.to_list images))

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main env
7 changes: 4 additions & 3 deletions examples/push.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ let headers =
let e = Cohttp.Header.of_list [] in
Cohttp.Header.add_authorization e (`Basic (user, token))

let test () =
Config.init ();
let test env =
Eio.Switch.run @@ fun sw ->
Config.init ~sw ~path:(Eio.Stdenv.cwd env) ();
let config = Irmin_git.config Config.root in
let repo = Store.Repo.v config in
let t = Store.main repo in
Expand All @@ -51,4 +52,4 @@ let test () =

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> test ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> test env
7 changes: 4 additions & 3 deletions examples/sync.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ let path =
module Store = Irmin_git_unix.FS.KV (Irmin.Contents.String)
module Sync = Irmin.Sync.Make (Store)

let test () =
Config.init ();
let test env =
Eio.Switch.run @@ fun sw ->
Config.init ~sw ~path:(Eio.Stdenv.cwd env) ();
let config = Irmin_git.config Config.root in
let repo = Store.Repo.v config in
let t = Store.of_branch repo "master" in
Expand All @@ -39,4 +40,4 @@ let test () =

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> test ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> test env
7 changes: 4 additions & 3 deletions examples/trees.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ let t_of_tree v =
let t2s = List.rev (List.sort compare t2s) in
List.fold_left aux [] t2s

let main () =
Config.init ();
let main env =
Eio.Switch.run @@ fun sw ->
Config.init ~sw ~path:(Eio.Stdenv.cwd env) ();
let config = Irmin_git.config ~bare:false Config.root in
let t =
[ { x = "foo"; y = 3 }; { x = "bar"; y = 5 }; { x = "too"; y = 10 } ]
Expand All @@ -69,4 +70,4 @@ let main () =

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main env
3 changes: 2 additions & 1 deletion irmin-cli.opam
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ depends: [
"irmin-test" {with-test & = version}
"alcotest" {with-test}
"mdx" {>= "2.0.0" & with-test}
"eio" {>= "1.1"}
]

pin-depends: [
[ "irmin-watcher.dev" "git+https://github.com/patricoferris/irmin-watcher#d0e92b4ba5631b5f4dc0f3c00d97e79542dba45d" ]
[ "irmin-watcher.dev" "git+https://github.com/clecat/irmin-watcher#834fc4903209b1516b010cbc7e134ebe115299c9" ]
]

synopsis: "CLI for Irmin"
Expand Down
2 changes: 1 addition & 1 deletion irmin-client.opam
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ depends: [
"irmin-watcher" {= "dev" & with-test}
]
pin-depends: [
[ "irmin-watcher.dev" "git+https://github.com/patricoferris/irmin-watcher#d0e92b4ba5631b5f4dc0f3c00d97e79542dba45d" ]
[ "irmin-watcher.dev" "git+https://github.com/clecat/irmin-watcher#834fc4903209b1516b010cbc7e134ebe115299c9" ]
]
build: [
["dune" "subst"] {pinned}
Expand Down
2 changes: 1 addition & 1 deletion irmin-fs.opam
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ depends: [
]

pin-depends: [
[ "irmin-watcher.dev" "git+https://github.com/patricoferris/irmin-watcher#d0e92b4ba5631b5f4dc0f3c00d97e79542dba45d" ]
[ "irmin-watcher.dev" "git+https://github.com/clecat/irmin-watcher#834fc4903209b1516b010cbc7e134ebe115299c9" ]
]

synopsis: "Generic file-system backend for Irmin"
2 changes: 1 addition & 1 deletion irmin-git.opam
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ depends: [
]

pin-depends: [
[ "irmin-watcher.dev" "git+https://github.com/patricoferris/irmin-watcher#d0e92b4ba5631b5f4dc0f3c00d97e79542dba45d" ]
[ "irmin-watcher.dev" "git+https://github.com/clecat/irmin-watcher#834fc4903209b1516b010cbc7e134ebe115299c9" ]
]

synopsis: "Git backend for Irmin"
Expand Down
1 change: 1 addition & 0 deletions irmin-pack-tools.opam
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ depends: [
"hex"
"irmin-test" {with-test & = version}
"alcotest" {with-test}
"eio_main" {>= "1.1"}
]

synopsis: "Utils for Irmin-pack"
Expand Down
2 changes: 1 addition & 1 deletion irmin-server.opam
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ depends: [
]

pin-depends: [
[ "irmin-watcher.dev" "git+https://github.com/patricoferris/irmin-watcher#d0e92b4ba5631b5f4dc0f3c00d97e79542dba45d" ]
[ "irmin-watcher.dev" "git+https://github.com/clecat/irmin-watcher#834fc4903209b1516b010cbc7e134ebe115299c9" ]
]

build: [
Expand Down
4 changes: 3 additions & 1 deletion src/irmin-cli/cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -976,8 +976,10 @@ let commands = List.map create_command commands

let run ~default:x y =
Eio_main.run @@ fun env ->
Eio.Switch.run @@ fun sw ->
let cwd = Eio.Stdenv.cwd env in
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ ->
Irmin.Backend.Watch.set_listen_dir_hook Irmin_watcher.hook;
Irmin.Backend.Watch.set_listen_dir_eio_hook ~sw cwd Irmin_watcher.hook;
Eio.Switch.run @@ fun sw ->
let env =
object
Expand Down
2 changes: 0 additions & 2 deletions src/irmin-cli/server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
open Lwt.Syntax
open Irmin_server

let () = Irmin.Backend.Watch.set_listen_dir_hook Irmin_watcher.hook

let setup_log style_renderer level =
Fmt_tty.setup_std_outputs ?style_renderer ();
Logs.set_level level;
Expand Down
9 changes: 9 additions & 0 deletions src/irmin/watch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ let watch_switch = ref None
type hook = int -> string -> (string -> unit) -> unit -> unit

let set_listen_dir_hook (h : hook) = listen_dir_hook := h

let set_listen_dir_eio_hook ~sw fs h =
let h id path callback =
h ~sw id
Eio.Path.(fs / path)
(fun path -> callback (Eio.Path.native_exn path))
in
listen_dir_hook := h

let set_watch_switch sw = watch_switch := Some sw

let id () =
Expand Down
13 changes: 13 additions & 0 deletions src/irmin/watch_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ module type Sigs = sig
a function to stop watching. It is probably best to use
{!Irmin_watcher.hook} there. By default, it uses {!none}. *)

val set_listen_dir_eio_hook :
sw:Eio.Switch.t ->
Eio.Fs.dir_ty Eio.Path.t ->
(sw:Eio.Switch.t ->
int ->
Eio.Fs.dir_ty Eio.Path.t ->
(Eio.Fs.dir_ty Eio.Path.t -> unit) ->
unit ->
unit) ->
unit
(** Same as {!set_listen_dir_hook}, however uses {!Eio.Path} instead of
regular {!string} for compatibility with the eio version of Irmin-watcher. *)

(** [Make] builds an implementation of watch helpers. *)
module Make (K : Type.S) (V : Type.S) :
S with type key = K.t and type value = V.t
Expand Down
9 changes: 6 additions & 3 deletions test/irmin-client/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ let misc client = [ ("misc", misc client) ]

let unix_suite ~env () =
Eio.Switch.run @@ fun sw ->
let path = Eio.Stdenv.cwd env in
let clock = Eio.Stdenv.clock env in
let kind, uri = run_server ~sw ~clock `Unix_domain in
let kind, uri = run_server ~sw ~path ~clock `Unix_domain in
let module Unix_socket = Make (struct
let uri = uri
let kind = kind
Expand All @@ -76,9 +77,10 @@ let unix_suite ~env () =

let tcp_suite ~env () =
Eio.Switch.run @@ fun sw ->
let path = Eio.Stdenv.cwd env in
let clock = Eio.Stdenv.clock env in
let module Tcp_socket = Make (struct
let kind, uri = run_server ~sw ~clock `Tcp
let kind, uri = run_server ~sw ~path ~clock `Tcp
end) in
let tests = Tcp_socket.suite () in
Irmin_test.Store.run "irmin-server.tcp" ~and_exit:false ~sleep:Eio_unix.sleep
Expand All @@ -87,9 +89,10 @@ let tcp_suite ~env () =

let websocket_suite ~env () =
Eio.Switch.run @@ fun sw ->
let path = Eio.Stdenv.cwd env in
let clock = Eio.Stdenv.clock env in
let module Websocket = Make (struct
let kind, uri = run_server ~sw ~clock `Websocket
let kind, uri = run_server ~sw ~path ~clock `Websocket
end) in
let tests = Websocket.suite () in
Irmin_test.Store.run "irmin-server.ws" ~and_exit:false ~sleep:Eio_unix.sleep
Expand Down
6 changes: 4 additions & 2 deletions test/irmin-client/util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let test name f client _switch () =
Logs.debug (fun l -> l "Running: %s" name);
f client

let run_server ~sw ~clock s =
let run_server ~sw ~path ~clock s =
let kind, uri =
match s with
| `Websocket -> ("Websocket", Uri.of_string "ws://localhost:90991")
Expand All @@ -36,7 +36,9 @@ let run_server ~sw ~clock s =
let stop, set_stop = Lwt.wait () in
Eio.Switch.on_release sw (fun () -> Lwt.wakeup_later set_stop ());
Eio.Fiber.fork_daemon ~sw (fun () ->
let () = Irmin.Backend.Watch.set_listen_dir_hook Irmin_watcher.hook in
let () =
Irmin.Backend.Watch.set_listen_dir_eio_hook ~sw path Irmin_watcher.hook
in
let key = Irmin.Backend.Conf.root Irmin_mem.Conf.spec in
let conf = Irmin.Backend.Conf.singleton Irmin_mem.Conf.spec key kind in
Lwt_eio.run_lwt (fun () -> Server.v ~uri conf >>= Server.serve ~stop);
Expand Down
13 changes: 7 additions & 6 deletions test/irmin-fs/test_fs_unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

let stats () =
let stats = Irmin_watcher.stats () in
let stats ~sw () =
let stats = Irmin_watcher.stats ~sw () in
(stats.Irmin_watcher.watchdogs, Irmin.Backend.Watch.workers ())

let test_db = Test_fs.test_db
Expand All @@ -30,14 +30,15 @@ let clean_dirs config =
let _ = Sys.command cmd in
()

let init ~config =
let init ~sw ~path ~config =
clean_dirs config;
Irmin.Backend.Watch.set_listen_dir_hook Irmin_watcher.hook
Irmin.Backend.Watch.set_listen_dir_eio_hook ~sw path Irmin_watcher.hook

let clean ~config =
clean_dirs config;
Irmin.Backend.Watch.(set_listen_dir_hook none)

let suite ~path ~clock =
let suite ~sw ~path ~clock =
let config = Irmin_fs_unix.conf ~path ~clock in
Irmin_test.Suite.create ~name:"FS.UNIX" ~init ~store ~config ~clean ~stats ()
Irmin_test.Suite.create ~name:"FS.UNIX" ~init:(init ~sw ~path) ~store ~config
~clean ~stats:(stats ~sw) ()
Loading

0 comments on commit 64df2de

Please sign in to comment.