Yaron Minsky avatar Yaron Minsky committed 895887c

a number of small tweaks, added logging, cleaned up daemonization

Comments (0)

Files changed (2)

 (* First, we build the implementations *)
 
 let publish_impl (dir,_) msg =
-  Log.Global.info "Message published";
   return (Directory.publish dir msg)
 
 let subscribe_impl (dir,_) topic ~aborted =
-  Log.Global.info "Subscription started";
   return (
     match Directory.subscribe dir topic with
     | None -> Error "Unknown topic"
   )
 
 let dump_impl (dir,_) () =
-  Log.Global.info "Dump requested";
   return (Directory.dump dir)
 
 let shutdown_impl (_,stop) () =
-  Log.Global.info "Shutdown requested";
+  Log.Global.info "Shutdown request";
   Ivar.fill_if_empty stop ();
   return ()
 
   Command.Spec.(
     empty
     +> Common.port_arg ()
+    +> flag "-fg" no_arg ~doc:" Run in the foreground (daemonize is the default)"
   )
-  (fun port () ->
+  (fun port fg () ->
     (* We use a blocking call to get the working directory, because the Async
        scheduler isn't running yet.
     *)
     let basedir = Core.Std.Unix.getcwd () in
-(*
-    let finish_daemonize =
-      unstage
-        (Daemon.daemonize_wait ()
-           ~redirect_stdout:`Do_not_redirect
-           ~redirect_stderr:`Do_not_redirect
-           ~cd:basedir)
-    in
-*)
+    let logfile = basedir ^/ "broker.log" in
+    if not fg then
+      Daemon.daemonize ()
+        ~redirect_stdout:(`File_append logfile)
+        ~redirect_stderr:(`File_append logfile)
+        ~cd:basedir;
     Log.Global.set_output
-      [ Log.Output.file `Text ~filename:(basedir ^/ "broker.log") ];
-    return () >>= fun () ->
-(*    finish_daemonize ();*)
-    Log.Global.info "Starting broker";
+      [ Log.Output.file `Text ~filename:logfile ];
+    Log.Global.info "Starting up";
     let stop = Ivar.create () in
     let directory = Directory.create () in
-    Log.Global.info "Starting server";
     Common.start_server ()
       ~stop:(Ivar.read stop)
       ~port
       ~implementations
       ~env:(directory,stop)
+    >>| fun () ->
+    Log.Global.info "Shutting down"
   )
 
 let () = Command.run command
 
 let port_arg () =
   Command.Spec.(
-    flag "-port" (optional_with_default 8080 int)
+    flag "-port" (optional_with_default 8124 int)
       ~doc:" Broker's port"
   )
 
 let start_server ~env ?(stop=Deferred.never ()) ~implementations ~port () =
   Log.Global.info "starting server on %d" port;
   let implementations =
-    Rpc.Implementations.create ~on_unknown_rpc:`Ignore ~implementations
+    Rpc.Implementations.create_exn ~implementations
+      ~on_unknown_rpc:(`Call (fun ~rpc_tag ~version ->
+        Log.Global.info "Unexpected RPC, tag %s, version %d" rpc_tag version))
   in
-  match implementations with
-  | Error (`Duplicate_implementations _) -> assert false
-  | Ok implementations ->
-    Log.Global.info "About to start TCP server";
-    Tcp.Server.create
-      ~on_handler_error:(`Call (fun _ exn -> Log.Global.sexp exn Exn.sexp_of_t))
-      (Tcp.on_port port)
-      (fun _addr r w ->
-        Rpc.Connection.server_with_close r w
-          ~connection_state:env
-          ~on_handshake_error:(
-            `Call (fun exn -> Log.Global.sexp exn Exn.sexp_of_t; return ()))
-          ~implementations
-      )
-    >>= fun server ->
-    Log.Global.info "TCP server started, waiting for close";
-    Deferred.any
-      [ (stop >>= fun () -> Tcp.Server.close server)
-      ; Tcp.Server.close_finished server ]
+  Log.Global.info "About to start TCP server";
+  Tcp.Server.create
+    ~on_handler_error:(`Call (fun _ exn -> Log.Global.sexp exn Exn.sexp_of_t))
+    (Tcp.on_port port)
+    (fun _addr r w ->
+      Rpc.Connection.server_with_close r w
+        ~connection_state:env
+        ~on_handshake_error:(
+          `Call (fun exn -> Log.Global.sexp exn Exn.sexp_of_t; return ()))
+        ~implementations
+    )
+  >>= fun server ->
+  Log.Global.info "TCP server started, waiting for close";
+  Deferred.any
+    [ (stop >>= fun () -> Tcp.Server.close server)
+    ; Tcp.Server.close_finished server ]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.