Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
made logplex_db a gen_server process that owns ets tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan Sloughter committed Jan 21, 2013
1 parent b190dd7 commit c55e69d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/logplex_app.erl
Expand Up @@ -55,6 +55,7 @@ start() ->
start(_StartType, _StartArgs) ->
?INFO("at=start", []),
set_cookie(),
redo_opts(),
read_git_branch(),
read_availability_zone(),
read_environment(),
Expand Down Expand Up @@ -227,3 +228,11 @@ start_ok(App, Type, {error, {not_started, Dep}}) ->
a_start(App, Type);
start_ok(App, _Type, {error, Reason}) ->
erlang:error({app_start_failed, App, Reason}).

redo_opts() ->
case os:getenv("LOGPLEX_CONFIG_REDIS_URL") of
false -> [];
Url ->
ParsedUrl = redo_uri:parse(Url),
application:set_env(?APP, config_redis_url, ParsedUrl)
end.
40 changes: 32 additions & 8 deletions src/logplex_db.erl
Expand Up @@ -21,14 +21,23 @@
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
%% OTHER DEALINGS IN THE SOFTWARE.
-module(logplex_db).
-export([start_link/0]).

-behaviour(gen_server).

-export([start_link/0, init/1, handle_call/3, handle_cast/2,
handle_info/2, terminate/2, code_change/3]).

-export([poll/2]).

-include("logplex_logging.hrl").
-include_lib("logplex.hrl").

start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

init([]) ->
create_ets_tables(),
redo:start_link(config, redo_opts()).
{ok, []}.

create_ets_tables() ->
ets:new(channels, [named_table, public, set, {keypos, 2}]),
Expand All @@ -40,12 +49,6 @@ create_ets_tables() ->
ets:new(drain_socket_quarentine, [named_table, public, set]),
ok.

redo_opts() ->
case os:getenv("LOGPLEX_CONFIG_REDIS_URL") of
false -> [];
Url -> redo_uri:parse(Url)
end.

-spec poll(fun ( () -> 'not_found' | {'found', T} | {'error', E} ),
pos_integer()) ->
{error, timeout} | {error, E} | T.
Expand Down Expand Up @@ -83,3 +86,24 @@ poll_cancel(Ref) ->
end;
_ -> ok
end.

handle_call(Call, _From, State) ->
?WARN("Unexpected call ~p.", [Call]),
{noreply, State}.

handle_cast(Msg, State) ->
?WARN("Unexpected cast ~p", [Msg]),
{noreply, State}.

%% @private
handle_info(Info, State) ->
?WARN("Unexpected info ~p", [Info]),
{noreply, State}.

%% @private
terminate(_Reason, _State) ->
ok.

%% @private
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
2 changes: 2 additions & 0 deletions src/logplex_sup.erl
Expand Up @@ -38,6 +38,8 @@ init([]) ->
{{one_for_one, 5, 10},
[{logplex_db, {logplex_db, start_link, []},
permanent, 2000, worker, [logplex_db]}
,{config_redis, {redo, start_link, [config, application:get_env(config_redis_url)]},
permanent, 2000, worker, [redo]}
,{logplex_drain_sup,
{logplex_drain_sup, start_link, []},
permanent, 2000, supervisor, [logplex_drain_sup]}
Expand Down

0 comments on commit c55e69d

Please sign in to comment.