Permalink
Browse files

Added the twitterlinks_accounts service

This service handles starting and stopping of accounts.  This was mainly
created to make sure the accounts are restarted if twitterlinks_sup restarted
it's children.

Eventually I'll back the service by a persistent DB, but for now I'm still
using the sys.config
  • Loading branch information...
ericmoritz committed May 19, 2012
1 parent 6206eae commit 1fa19d4eb29ee67752fd4b99d06afa5858cfb19b
Showing with 81 additions and 0 deletions.
  1. +81 −0 apps/twitterlinks/src/twitterlinks_accounts.erl
@@ -0,0 +1,81 @@
+-module(twitterlinks_accounts).
+-behaviour(gen_server).
+-define(SERVER, ?MODULE).
+
+-record(state, {accounts}).
+
+%% ------------------------------------------------------------------
+%% API Function Exports
+%% ------------------------------------------------------------------
+
+-export([start_link/1, add_account/3, stop_account/1]).
+
+%% ------------------------------------------------------------------
+%% gen_server Function Exports
+%% ------------------------------------------------------------------
+
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
+ terminate/2, code_change/3]).
+
+%% ------------------------------------------------------------------
+%% API Function Definitions
+%% ------------------------------------------------------------------
+
+start_link(Accounts) ->
+ gen_server:start_link({local, ?SERVER}, ?MODULE, [Accounts], []).
+
+add_account(AccountId, TwitCred, DelCred) ->
+ gen_server:call(?SERVER, {add_account, AccountId, TwitCred, DelCred}).
+
+stop_account(AccountId) ->
+ gen_server:call(?SERVER, {stop_account, AccountId}).
+
+%% ------------------------------------------------------------------
+%% gen_server Function Definitions
+%% ------------------------------------------------------------------
+
+init([Accounts]) ->
+ {ok, #state{accounts=Accounts}, 0}.
+
+handle_call({add_account, AccountId, TwitCred, DelCred}, _From, State) ->
+ {DelUser, DelPass} = DelCred,
+ {TwitUser, TwitPass, TwitUID} = TwitCred,
+ twitterlinks_delicious:create(AccountId, DelUser, DelPass),
+ twitterlinks_twitter:create(AccountId, TwitUser, TwitPass, TwitUID),
+ {reply, ok, State};
+handle_call({stop_account, AccountId}, _From, State) ->
+ twitterlinks_twitter:stop(AccountId),
+ twitterlinks_delicious:stop(AccountId),
+ {reply, ok, State};
+handle_call(_Msg, _From, State) ->
+ {noreply, State}.
+
+handle_cast(_Msg, State) ->
+ {noreply, State}.
+
+handle_info(timeout, #state{accounts=Accounts} = State) ->
+ lists:foreach(fun(Account) ->
+ {AccountId, Props} = Account,
+ TwitCred = proplists:get_value(twitter, Props),
+ DelCred = proplists:get_value(delicious, Props),
+
+ handle_call({add_account,
+ AccountId,
+ TwitCred,
+ DelCred}, self(), State)
+
+ end, Accounts),
+ {noreply, State};
+handle_info(_Info, State) ->
+ {noreply, State}.
+
+terminate(_Reason, _State) ->
+ ok.
+
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
+%% ------------------------------------------------------------------
+%% Internal Function Definitions
+%% ------------------------------------------------------------------
+

0 comments on commit 1fa19d4

Please sign in to comment.