Skip to content

Commit

Permalink
Added RABBIT_HOST on environment to start with a new rabbit host
Browse files Browse the repository at this point in the history
  • Loading branch information
auser authored and tolbrino committed Feb 7, 2010
1 parent c24beb7 commit 3c2567b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/alice/alice_app.erl
Expand Up @@ -10,4 +10,5 @@ start(Type, Args) ->
% ), % ),
alice_sup:start(Type, Args). alice_sup:start(Type, Args).


stop(_State) -> ok. stop(State) ->
alice_sup:stop(State).
6 changes: 3 additions & 3 deletions src/alice/alice_log.erl
Expand Up @@ -11,7 +11,7 @@
-include ("alice.hrl"). -include ("alice.hrl").


%% API %% API
-export([start_link/0, stop/0, append/1, print/0, -export([start_link/0, stop/0, stop/1, append/1, print/0,
error/1,error/2, error/1,error/2,
info/1,info/2 info/1,info/2
]). ]).
Expand All @@ -35,8 +35,8 @@
start_link() -> start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).


stop() -> stop(Args) -> stop().
gen_server:call(?MODULE, stop). stop() -> gen_server:call(?MODULE, stop).


error(Msg) -> error(Msg, []). error(Msg) -> error(Msg, []).
error(Msg, Args) -> error_logger:error_msg(lists:flatten(io_lib:format(Msg, Args))). error(Msg, Args) -> error_logger:error_msg(lists:flatten(io_lib:format(Msg, Args))).
Expand Down
11 changes: 9 additions & 2 deletions src/alice/alice_sup.erl
Expand Up @@ -2,7 +2,7 @@
-include ("alice.hrl"). -include ("alice.hrl").
-behaviour(supervisor). -behaviour(supervisor).


-export([start/2]). -export([start/2, stop/1]).
-export([init/1]). -export([init/1]).


start(Type, Args) -> start(Type, Args) ->
Expand All @@ -17,4 +17,11 @@ init([_Type, Args]) ->
{ok,{_SupFlags = {one_for_one, ?MAXIMUM_RESTARTS, ?MAX_DELAY_TIME},[ {ok,{_SupFlags = {one_for_one, ?MAXIMUM_RESTARTS, ?MAX_DELAY_TIME},[
LogServerSup, LogServerSup,
RestServerSup RestServerSup
]}}. ]}}.

stop(Args) ->
lists:map(fun(Term) -> Term:stop(Args) end, [
alice_log,
rest_server_sup
]),
ok.
1 change: 1 addition & 0 deletions src/rest/rest_server.erl
Expand Up @@ -122,6 +122,7 @@ handle_info(_Info, State) ->
%% The return value is ignored. %% The return value is ignored.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
terminate(_Reason, _State) -> terminate(_Reason, _State) ->
mochiweb_http:stop(),
ok. ok.


%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
Expand Down
10 changes: 7 additions & 3 deletions src/rest/rest_server_sup.erl
@@ -1,10 +1,14 @@
-module (rest_server_sup). -module (rest_server_sup).
-behaviour(supervisor). -behaviour(supervisor).


-export([start_link/1, init/1]). -export([start_link/1, init/1, stop/1]).


start_link(Args) -> start_link(Args) ->
supervisor:start_link({local, ?MODULE}, ?MODULE, Args). supervisor:start_link({local, ?MODULE}, ?MODULE, Args).


init(Args) -> init(Args) ->
{ok, {{one_for_one, 2, 10}, [{the_rest_server, {rest_server, start_link, [Args]}, permanent, 2000, worker, [rest_server]}]}}. {ok, {{one_for_one, 2, 10}, [{the_rest_server, {rest_server, start_link, [Args]}, permanent, 2000, worker, [rest_server]}]}}.

stop(_Args) ->
Pid = whereis(?MODULE),
exit(Pid,kill).
13 changes: 10 additions & 3 deletions src/utils/rabint.erl
Expand Up @@ -13,11 +13,17 @@ rpc_call(Mod, Fun, Args) -> rpc:call(rabbit_node(), Mod, Fun, Args, ?RPC_TIMEOU
% Get the rabbit node from the env % Get the rabbit node from the env
rabbit_node() -> rabbit_node() ->
case application:get_env(alice, rabbithost) of case application:get_env(alice, rabbithost) of
undefined -> localnode(rabbit); undefined -> get_rabbit_node_from_environment();
{ok, undefined} -> localnode(rabbit); {ok, undefined} -> get_rabbit_node_from_environment();
{ok, Hostname} -> rabbit_node(Hostname) {ok, Hostname} -> rabbit_node(Hostname)
end. end.


get_rabbit_node_from_environment() ->
case os:getenv("RABBIT_HOST") of
false -> localnode(rabbit);
Server -> Server
end.

rabbit_node(Hostname) -> case Hostname of rabbit_node(Hostname) -> case Hostname of
H when is_atom(H) -> H; H when is_atom(H) -> H;
Else -> list_to_atom(Else) Else -> list_to_atom(Else)
Expand All @@ -37,6 +43,7 @@ localnode(Name) ->
stay_connected_to_rabbit_node(Attempts) -> stay_connected_to_rabbit_node(Attempts) ->
case Attempts > ?MAX_ATTEMPTS of case Attempts > ?MAX_ATTEMPTS of
true -> true ->
alice_app:stop([]),
?ERROR("Lost connect with rabbitmq_server. Check that it's up and try again~n", []); ?ERROR("Lost connect with rabbitmq_server. Check that it's up and try again~n", []);
false -> false ->
timer:sleep(?RABBIT_HEARTBEAT_DELAY), timer:sleep(?RABBIT_HEARTBEAT_DELAY),
Expand Down

0 comments on commit 3c2567b

Please sign in to comment.