Skip to content
Snippets Groups Projects
Commit 06b8d091 authored by Loïc Correnson's avatar Loïc Correnson Committed by Michele Alberti
Browse files

[server/zmq] background server

parent 3e6e1d94
No related branches found
No related tags found
No related merge requests found
...@@ -297,6 +297,7 @@ let stop server = ...@@ -297,6 +297,7 @@ let stop server =
| Some db -> | Some db ->
begin begin
server.background <- None ; server.background <- None ;
signal false ;
Db.progress := db ; Db.progress := db ;
end end
......
...@@ -32,12 +32,19 @@ module Senv = Server_parameters ...@@ -32,12 +32,19 @@ module Senv = Server_parameters
let zmq_group = Senv.add_group "Protocol ZeroMQ" let zmq_group = Senv.add_group "Protocol ZeroMQ"
let () = Parameter_customize.set_group zmq_group let () = Parameter_customize.set_group zmq_group
module Enabled = Senv.String module Socket = Senv.String
(struct (struct
let option_name = "-server-zmq" let option_name = "-server-zmq"
let arg_name = "url" let arg_name = "url"
let default = "" let default = ""
let help = "Establish a ZeroMQ server and listen for connections" let help = "Setup the ZeroMQ server (in background)."
end)
let () = Parameter_customize.set_group zmq_group
module Run = Senv.Action
(struct
let option_name = "-server-zmq-run"
let help = "Launch the ZeroMQ server (until shutdown)."
end) end)
let _ = Doc.page `Protocol ~title:"ZeroMQ Protocol" ~filename:"server_zmq.md" let _ = Doc.page `Protocol ~title:"ZeroMQ Protocol" ~filename:"server_zmq.md"
...@@ -46,7 +53,7 @@ let _ = Doc.page `Protocol ~title:"ZeroMQ Protocol" ~filename:"server_zmq.md" ...@@ -46,7 +53,7 @@ let _ = Doc.page `Protocol ~title:"ZeroMQ Protocol" ~filename:"server_zmq.md"
(* --- ZMQ Context --- *) (* --- ZMQ Context --- *)
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
let context = let zmq_context =
let zmq = ref None in let zmq = ref None in
fun () -> fun () ->
match !zmq with match !zmq with
...@@ -117,26 +124,43 @@ let fetch socket () = ...@@ -117,26 +124,43 @@ let fetch socket () =
(* --- Establish the Server --- *) (* --- Establish the Server --- *)
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
let establish url = let pretty = Format.pp_print_string
if url <> "" then let background = ref None
begin
let context = context () in
let socket = Zmq.Socket.(create context rep) in
try
Zmq.Socket.bind socket url ;
Senv.feedback "ZeroMQ [%s]" url ;
Main.create ~pretty:Format.pp_print_string ~fetch:(fetch socket) ()
|> Main.run ;
Zmq.Socket.close socket ;
with exn ->
Zmq.Socket.close socket ;
raise exn
end
(* -------------------------------------------------------------------------- *) let cmdline () =
(* --- Establish the Server from Command line --- *) begin
(* -------------------------------------------------------------------------- *) match !background with
| Some server ->
let () = Db.Main.extend (fun () -> establish (Enabled.get ())) if Run.get () then Main.run server
| None ->
let url = Socket.get () in
if url <> "" then
let context = zmq_context () in
let socket = Zmq.Socket.(create context rep) in
try
Zmq.Socket.bind socket url ;
let server = Main.create ~pretty ~fetch:(fetch socket) () in
background := Some server ;
at_exit begin fun () ->
Senv.feedback "ZeroMQ [%s] stopped" url ;
Zmq.Socket.close socket ;
background := None ;
end ;
if Run.get () then
begin
Senv.feedback "ZeroMQ [%s] running" url ;
Main.run server
end
else
begin
Senv.feedback "ZeroMQ [%s] started" url ;
Main.start server
end
with exn ->
Zmq.Socket.close socket ;
raise exn
end
let () = Db.Main.extend cmdline
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment