Skip to content
Browse files

Data sources can be defined

  • Loading branch information...
1 parent 2f52a05 commit eb4908ee2ab56267f3d9bf6299345cc8a39254d3 @mitchellh committed
View
8 apps/lifeguard/src/lifeguard.app.src
@@ -9,7 +9,9 @@
{modules, [
lifeguard_app,
lifeguard_sup,
- lifeguard_ds_garbage
+ lifeguard_ds_garbage,
+ lifeguard_ds_manager,
+ lifeguard_ds_manager_sup
]},
{applications, [
kernel,
@@ -17,5 +19,7 @@
lager
]},
{mod, { lifeguard_app, []}},
- {env, []}
+ {env, [
+ {data_sources, [{"garbage", lifeguard_ds_garbage, []}]}
+ ]}
]}.
View
13 apps/lifeguard/src/lifeguard_ds_garbage.erl
@@ -3,7 +3,7 @@
-module(lifeguard_ds_garbage).
-behavior(gen_server).
--export([start_link/1]).
+-export([start_link/2]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-ifdef(TEST).
@@ -11,16 +11,19 @@
-endif.
%% @doc Start the data source in a supervision tree.
-start_link(_Config) ->
- gen_server:start_link(?MODULE, [], []).
+start_link(Name, _Args) ->
+ ServerRef = list_to_atom("ds_" ++ Name),
+ gen_server:start_link({local, ServerRef}, ?MODULE, [], []).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% gen_server callbacks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-init(_Args) -> {ok, any}.
+init(_Args) ->
+ lager:info("Started the garbage data source..."),
+ {ok, any}.
-handle_call({get, Amount}, _From, State) ->
+handle_call({get, [Amount]}, _From, State) ->
{reply, get_numbers(Amount), State}.
handle_cast(_Request, State) -> {noreply, State}.
View
35 apps/lifeguard/src/lifeguard_ds_manager.erl
@@ -0,0 +1,35 @@
+-module(lifeguard_ds_manager).
+-behavior(gen_server).
+-export([start_link/0, get/2]).
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
+
+%% Represents the state of our server.
+-record(manager_state, { sources }).
+
+%% @doc Start the data source manager.
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+get(SourceName, Args) ->
+ gen_server:call(?MODULE, {get, SourceName, Args}).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% gen_server callbacks
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+init(_Args) ->
+ State = #manager_state{sources=dict:new()},
+ {ok, State}.
+
+handle_call({get, SourceName, Args}, _From, State) ->
+ SourceRef = list_to_atom("ds_" ++ SourceName),
+ Response = gen_server:call(SourceRef, {get, Args}),
+ {reply, Response, State}.
+
+handle_cast(_Request, State) -> {noreply, State}.
+
+handle_info(_Request, State) -> {noreply, State}.
+
+terminate(_Reason, _State) -> ok.
+
+code_change(_OldVsn, State, _Extra) -> {ok, State}.
View
22 apps/lifeguard/src/lifeguard_ds_manager_sup.erl
@@ -0,0 +1,22 @@
+-module(lifeguard_ds_manager_sup).
+-behavior(supervisor).
+-export([start_link/1, init/1]).
+
+start_link(DataSources) ->
+ supervisor:start_link({local, ?MODULE}, ?MODULE, DataSources).
+
+init(DataSources) ->
+ % Define the manager...
+ Manager = {data_source_manager,
+ {lifeguard_ds_manager, start_link, []},
+ permanent, 5000, worker, dynamic},
+
+ % Add all the individual sources
+ SourceSpecs = [data_source_spec(Source) || Source <- DataSources],
+
+ {ok, {{one_for_one, 10, 10}, [Manager | SourceSpecs]} }.
+
+data_source_spec({Name, Module, Args}) ->
+ {{data_source, Name},
+ {Module, start_link, [Name, Args]},
+ permanent, 30000, worker, [Module]}.
View
9 apps/lifeguard/src/lifeguard_sup.erl
@@ -1,4 +1,3 @@
-
-module(lifeguard_sup).
-behaviour(supervisor).
@@ -24,5 +23,9 @@ start_link() ->
%% ===================================================================
init([]) ->
- {ok, { {one_for_one, 5, 10}, []} }.
-
+ {ok, DataSources} = application:get_env(data_sources),
+ DSManager = {data_store_manager_sup,
+ {lifeguard_ds_manager_sup, start_link, [DataSources]},
+ permanent, 30000, supervisor, [lifeguard_ds_manager_sup]},
+ Children = [DSManager],
+ {ok, { {one_for_one, 5, 10}, Children} }.

0 comments on commit eb4908e

Please sign in to comment.
Something went wrong with that request. Please try again.