-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7901b52
commit 93fb0ba
Showing
8 changed files
with
509 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
%%%------------------------------------------------------------------- | ||
%%% @copyright (C) 2012, VoIP INC | ||
%%% @doc | ||
%%% | ||
%%% @end | ||
%%% @contributors | ||
%%% James Aimonetti | ||
%%% Karl Anderson | ||
%%%------------------------------------------------------------------- | ||
-module(acdc_agent_sup). | ||
|
||
-behaviour(supervisor). | ||
|
||
-include_lib("whistle/include/wh_types.hrl"). | ||
|
||
-export([start_link/0]). | ||
-export([new/3]). | ||
-export([workers/0]). | ||
-export([init/1]). | ||
|
||
%% =================================================================== | ||
%% API functions | ||
%% =================================================================== | ||
|
||
%%-------------------------------------------------------------------- | ||
%% @public | ||
%% @doc | ||
%% Starts the supervisor | ||
%% @end | ||
%%-------------------------------------------------------------------- | ||
-spec start_link/0 :: () -> startlink_ret(). | ||
start_link() -> | ||
supervisor:start_link({local, ?MODULE}, ?MODULE, []). | ||
|
||
-spec new/3 :: (ne_binary(), ne_binary(), wh_json:json_object()) -> sup_startchild_ret(). | ||
new(AccountDb, AgentId, Queues) -> | ||
Agent = {AgentId | ||
,{acdc_agent, start_link, [AccountDb, AgentId, Queues]} | ||
,permanent, 5000, worker | ||
,[acdc_agent] | ||
}, | ||
supervisor:start_child(?MODULE, Agent). | ||
|
||
-spec workers/0 :: () -> [pid(),...] | []. | ||
workers() -> | ||
[ Pid || {_, Pid, worker, [_]} <- supervisor:which_children(?MODULE)]. | ||
|
||
%% =================================================================== | ||
%% Supervisor callbacks | ||
%% =================================================================== | ||
|
||
%%-------------------------------------------------------------------- | ||
%% @public | ||
%% @doc | ||
%% Whenever a supervisor is started using supervisor:start_link/[2,3], | ||
%% this function is called by the new process to find out about | ||
%% restart strategy, maximum restart frequency and child | ||
%% specifications. | ||
%% @end | ||
%%-------------------------------------------------------------------- | ||
-spec init([]) -> sup_init_ret(). | ||
init([]) -> | ||
RestartStrategy = one_for_one, | ||
MaxRestarts = 5, | ||
MaxSecondsBetweenRestarts = 5, | ||
|
||
SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts}, | ||
|
||
{ok, {SupFlags, []}}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
%%%------------------------------------------------------------------- | ||
%%% @copyright (C) 2012, VoIP INC | ||
%%% @doc | ||
%%% | ||
%%% @end | ||
%%% @contributors | ||
%%% James Aimonetti | ||
%%% Karl Anderson | ||
%%%------------------------------------------------------------------- | ||
-module(acdc_agents). | ||
|
||
-behaviour(gen_server). | ||
|
||
-export([start_link/0]). | ||
-export([next_agent/0]). | ||
-export([reload_agents/0]). | ||
|
||
-export([init/1 | ||
,handle_call/3 | ||
,handle_cast/2 | ||
,handle_info/2 | ||
,terminate/2 | ||
,code_change/3 | ||
]). | ||
|
||
-include("acdc.hrl"). | ||
|
||
-define(SERVER, ?MODULE). | ||
|
||
%%%=================================================================== | ||
%%% API | ||
%%%=================================================================== | ||
|
||
%%-------------------------------------------------------------------- | ||
%% @doc | ||
%% Starts the server | ||
%% | ||
%% @spec start_link() -> {ok, Pid} | ignore | {error, Error} | ||
%% @end | ||
%%-------------------------------------------------------------------- | ||
start_link() -> | ||
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). | ||
|
||
-spec reload_agents/0 :: () -> 'ok'. | ||
reload_agents() -> | ||
gen_server:cast(?SERVER, reload_agents). | ||
|
||
-spec next_agent/0 :: () -> {'ok', pid()} | {'error', 'no_agents'}. | ||
next_agent() -> | ||
gen_server:call(?SERVER, next_agent). | ||
|
||
%%%=================================================================== | ||
%%% gen_server callbacks | ||
%%%=================================================================== | ||
|
||
%%-------------------------------------------------------------------- | ||
%% @private | ||
%% @doc | ||
%% Initializes the server | ||
%% | ||
%% @spec init(Args) -> {ok, State} | | ||
%% {ok, State, Timeout} | | ||
%% ignore | | ||
%% {stop, Reason} | ||
%% @end | ||
%%-------------------------------------------------------------------- | ||
init([]) -> | ||
put(callid, ?LOG_SYSTEM_ID), | ||
lager:debug("acdc agents starting"), | ||
{ok, []}. | ||
|
||
%%-------------------------------------------------------------------- | ||
%% @private | ||
%% @doc | ||
%% Handling call messages | ||
%% | ||
%% @spec handle_call(Request, From, State) -> | ||
%% {reply, Reply, State} | | ||
%% {reply, Reply, State, Timeout} | | ||
%% {noreply, State} | | ||
%% {noreply, State, Timeout} | | ||
%% {stop, Reason, Reply, State} | | ||
%% {stop, Reason, State} | ||
%% @end | ||
%%-------------------------------------------------------------------- | ||
handle_call(next_agent, _From, []) -> | ||
{reply, {error, no_agents}, []}; | ||
handle_call(next_agent, _From, [Agent|Agents]) -> | ||
%% this is currently just round-robin but eventually there could | ||
%% be lists for each queue and the pids are drawn via a strategy | ||
%% maybe.... | ||
{reply, {ok, Agent}, lists:reverse([Agent|lists:reverse(Agents)])}; | ||
handle_call(_Request, _From, State) -> | ||
{reply, {error, not_implemented}, State}. | ||
|
||
%%-------------------------------------------------------------------- | ||
%% @private | ||
%% @doc | ||
%% Handling cast messages | ||
%% | ||
%% @spec handle_cast(Msg, State) -> {noreply, State} | | ||
%% {noreply, State, Timeout} | | ||
%% {stop, Reason, State} | ||
%% @end | ||
%%-------------------------------------------------------------------- | ||
handle_cast(reload_agents, _) -> | ||
lager:debug("reloading list of agent workers"), | ||
{noreply, acdc_agent_sup:workers()}; | ||
handle_cast(_, State) -> | ||
{noreply, State}. | ||
|
||
%%-------------------------------------------------------------------- | ||
%% @private | ||
%% @doc | ||
%% Handling all non call/cast messages | ||
%% | ||
%% @spec handle_info(Info, State) -> {noreply, State} | | ||
%% {noreply, State, Timeout} | | ||
%% {stop, Reason, State} | ||
%% @end | ||
%%-------------------------------------------------------------------- | ||
handle_info(_Info, State) -> | ||
{noreply, State}. | ||
|
||
%%-------------------------------------------------------------------- | ||
%% @private | ||
%% @doc | ||
%% This function is called by a gen_server when it is about to | ||
%% terminate. It should be the opposite of Module:init/1 and do any | ||
%% necessary cleaning up. When it returns, the gen_server terminates | ||
%% with Reason. The return value is ignored. | ||
%% | ||
%% @spec terminate(Reason, State) -> void() | ||
%% @end | ||
%%-------------------------------------------------------------------- | ||
terminate(_Reason, _State) -> | ||
lager:debug("acdc agents terminating: ~p", [_Reason]). | ||
|
||
%%-------------------------------------------------------------------- | ||
%% @private | ||
%% @doc | ||
%% Convert process state when code is changed | ||
%% | ||
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState} | ||
%% @end | ||
%%-------------------------------------------------------------------- | ||
code_change(_OldVsn, State, _Extra) -> | ||
{ok, State}. | ||
|
||
%%%=================================================================== | ||
%%% Internal functions | ||
%%%=================================================================== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.