Permalink
Browse files

Move role extraction to o.ml

  • Loading branch information...
1 parent 37e4a9d commit cfcfece3e241ece78a47f4d25ec9036911554b66 @VictorNicollet committed Sep 3, 2012
Showing with 49 additions and 39 deletions.
  1. +1 −1 install/ocaml/main.ml
  2. +10 −2 install/ocaml/o.ml
  3. +10 −13 src/action.ml
  4. +2 −2 src/main.ml
  5. +25 −20 src/util.ml
  6. +1 −1 src/util.mli
View
@@ -4,6 +4,6 @@ open Ohm
open BatPervasives
module Main = Ohm.Main.Make(O.Reset)
-let _ = Main.run (Some O.run_async)
+let _ = Main.run ~async:O.run_async O.role
View
@@ -4,6 +4,14 @@ open Ohm
open Ohm.Universal
open BatPervasives
+(** {1 Instance role} *)
+
+(** The role of the current instance. This value can be used to only perform
+ some actions in a certain context, such as the web server or the asynchronous
+ bot.
+*)
+let role = Util.role ()
+
(** {1 Environment and basic configuration} *)
(** Available environments. These represent the contexts in which the
@@ -24,7 +32,7 @@ let env = match environment with
(** The full absolute path to the log file.
*)
-let logpath = match Ohm.Util.role with
+let logpath = match role with
| `Put
| `Reset -> None
| `Bot
@@ -92,7 +100,7 @@ module Reset = Reset.Make(ConfigDB)
(** Run an action when the instance is running in [`Put] mode.
*)
let put action =
- if Ohm.Util.role = `Put then
+ if role = `Put then
ignore (Ohm.Run.eval (ctx `EN) action)
(** {1 Asynchronous tasks} *)
View
@@ -18,20 +18,17 @@ let declared = ref []
let defined = Hashtbl.create 100
let dispatch_define (server,prefix,args) action =
- if Util.role = `Web then begin
- let key = path_clean (lowercase prefix) in
- let endpt = endpoint_of_controller (server,prefix,args) in
-
- let value protocol domain port suffix cgi =
- match Args.parse args suffix with None -> None | Some args ->
- match server # matches protocol domain port with None -> None | Some s ->
- Some (server # cookie_domain, action (new fcgi_request endpt s args cgi))
- in
-
- Hashtbl.add defined key value
-
- end
+ let key = path_clean (lowercase prefix) in
+ let endpt = endpoint_of_controller (server,prefix,args) in
+
+ let value protocol domain port suffix cgi =
+ match Args.parse args suffix with None -> None | Some args ->
+ match server # matches protocol domain port with None -> None | Some s ->
+ Some (server # cookie_domain, action (new fcgi_request endpt s args cgi))
+ in
+
+ Hashtbl.add defined key value
let dispatch_declare (server,prefix,args) =
let cell = ref (Some (path_clean (lowercase prefix))) in
View
@@ -4,7 +4,7 @@ module Make =
functor (Reset:Reset.RESET) ->
struct
- let run async =
+ let run ?async role =
let web_loop () =
if not (Reset.resetting ()) then
@@ -39,7 +39,7 @@ struct
loop_check ()
in
- match Util.role with
+ match role with
| `Web -> web_loop ()
| `Bot -> bot_loop ()
| `Put -> CouchDB.compile_views ()
View
@@ -4,20 +4,26 @@ type role = [ `Bot | `Web | `Put | `Reset ]
let pid = Unix.getpid ()
-let role =
- let bot = ref false
- and put = ref false
- and cgi = ref false
- and reset = ref false in
- Arg.parse [
- "--reset", Arg.Set reset, "force other processes to restart" ;
- "--cgi", Arg.Set cgi, "run as FastCGI (default)" ;
- "--put", Arg.Set put, "run as view/i18n uploader" ;
- "--bot", Arg.Set bot, "run as bot" ;
- ] (fun _ -> ()) "Start an instance of the Ohm server" ;
- if !bot then `Bot else
- if !put then `Put else
+let _role = ref None
+
+let role () =
+ let role =
+ let bot = ref false
+ and put = ref false
+ and cgi = ref false
+ and reset = ref false in
+ Arg.parse [
+ "--reset", Arg.Set reset, "force other processes to restart" ;
+ "--cgi", Arg.Set cgi, "run as FastCGI (default)" ;
+ "--put", Arg.Set put, "run as view/i18n uploader" ;
+ "--bot", Arg.Set bot, "run as bot" ;
+ ] (fun _ -> ()) "Start an instance of the Ohm server" ;
+ if !bot then `Bot else
+ if !put then `Put else
if !reset then `Reset else `Web
+ in
+ _role := Some role ;
+ role
module Logging = struct
@@ -36,11 +42,12 @@ module Logging = struct
let prefix =
Printf.sprintf "[%s:%d]"
- (match role with
- | `Reset -> "RESET"
- | `Bot -> "BOT"
- | `Web -> "WEB"
- | `Put -> "PUT")
+ (match !_role with
+ | None -> "---"
+ | Some `Reset -> "RST"
+ | Some `Bot -> "BOT"
+ | Some `Web -> "WEB"
+ | Some `Put -> "PUT")
pid
let output string =
@@ -81,7 +88,6 @@ let memoize f =
try Hashtbl.find h x with Not_found ->
let y = f x in Hashtbl.add h x y ; y
-
let get_binary_contents full =
try
let chan = open_in_bin full in
@@ -99,7 +105,6 @@ let get_binary_contents full =
log "Util.get_contents: could not open %s (%s)" full (Printexc.to_string exn);
None
-
let urlencode str =
let regexp = Str.regexp "[^-a-zA-Z0-9$_.+!*'(),]" in
let result = Str.global_substitute regexp begin fun str ->
View
@@ -2,7 +2,7 @@
type role = [ `Bot | `Web | `Put | `Reset ]
-val role : role
+val role : unit -> role
val get_binary_contents : string -> string option
val get_view_contents : string -> string option

0 comments on commit cfcfece

Please sign in to comment.