Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.3] acdc - PISTON-893: acdc_agent_listener doesn't need route.req, don't bind it #6556

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 23 additions & 40 deletions applications/acdc/src/acdc_agent_fsm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ ready('cast', {'member_connect_win', JObj, 'same_node'}, #state{agent_listener=A

lager:debug("trying to ring agent ~s to connect to caller in queue ~s", [AgentId, QueueId]),

case get_endpoints(OrigEPs, AgentListener, Call, AgentId, QueueId) of
case get_endpoints(OrigEPs, Call, AgentId, QueueId) of
{'error', 'no_endpoints'} ->
lager:info("agent ~s has no endpoints assigned; logging agent out", [AgentId]),
acdc_agent_stats:agent_logged_out(AccountId, AgentId),
Expand Down Expand Up @@ -628,7 +628,7 @@ ready('cast', {'member_connect_win', JObj, 'different_node'}, #state{agent_liste
RecordingUrl = recording_url(JObj),

%% Only start monitoring if the agent can actually take the call
case get_endpoints(OrigEPs, AgentListener, Call, AgentId, QueueId) of
case get_endpoints(OrigEPs, Call, AgentId, QueueId) of
{'error', 'no_endpoints'} ->
lager:info("agent ~s has no endpoints assigned; logging agent out", [AgentId]),
{'next_state', 'paused', State};
Expand Down Expand Up @@ -1445,7 +1445,7 @@ handle_event('load_endpoints', StateName, #state{agent_id=AgentId
%% Inform us of things with us as owner
catch gproc:reg(?OWNER_UPDATE_REG(AccountId, AgentId)),

case get_endpoints([], AgentListener, Call, AgentId, 'undefined') of
case get_endpoints([], Call, AgentId, 'undefined') of
{'error', 'no_endpoints'} -> {'next_state', StateName, State};
{'ok', EPs} -> {'next_state', StateName, State#state{endpoints=EPs}};
{'error', E} -> {'stop', E, State}
Expand All @@ -1467,42 +1467,39 @@ handle_info({'member_connect_win', _, 'different_node'}, StateName, State) ->
handle_info({'endpoint_edited', EP}, StateName, #state{endpoints=EPs
,account_id=AccountId
,agent_id=AgentId
,agent_listener=AgentListener
}=State) ->
EPId = kz_doc:id(EP),
case kz_json:get_value(<<"owner_id">>, EP) of
AgentId ->
lager:debug("device ~s edited, we're the owner, maybe adding it", [EPId]),
{'next_state', StateName, State#state{endpoints=maybe_add_endpoint(EPId, EP, EPs, AccountId, AgentListener)}, 'hibernate'};
{'next_state', StateName, State#state{endpoints=maybe_add_endpoint(EPId, EP, EPs, AccountId)}, 'hibernate'};
_OwnerId ->
lager:debug("device ~s edited, owner now ~s, maybe removing it", [EPId, _OwnerId]),
{'next_state', StateName, State#state{endpoints=maybe_remove_endpoint(EPId, EPs, AccountId, AgentListener)}, 'hibernate'}
{'next_state', StateName, State#state{endpoints=maybe_remove_endpoint(EPId, EPs, AccountId)}, 'hibernate'}
end;
handle_info({'endpoint_deleted', EP}, StateName, #state{endpoints=EPs
,account_id=AccountId
,agent_listener=AgentListener
}=State) ->
EPId = kz_doc:id(EP),
lager:debug("device ~s deleted, maybe removing it", [EPId]),
{'next_state', StateName, State#state{endpoints=maybe_remove_endpoint(EPId, EPs, AccountId, AgentListener)}, 'hibernate'};
{'next_state', StateName, State#state{endpoints=maybe_remove_endpoint(EPId, EPs, AccountId)}, 'hibernate'};
handle_info({'endpoint_created', EP}, StateName, #state{endpoints=EPs
,account_id=AccountId
,agent_id=AgentId
,agent_listener=AgentListener
}=State) ->
EPId = kz_doc:id(EP),
case kz_json:get_value(<<"owner_id">>, EP) of
AgentId ->
lager:debug("device ~s created, we're the owner, maybe adding it", [EPId]),
{'next_state', StateName, State#state{endpoints=maybe_add_endpoint(EPId, EP, EPs, AccountId, AgentListener)}, 'hibernate'};
{'next_state', StateName, State#state{endpoints=maybe_add_endpoint(EPId, EP, EPs, AccountId)}, 'hibernate'};
_OwnerId ->
lager:debug("device ~s created, owner is ~s, maybe ignoring", [EPId, _OwnerId]),

case kz_json:get_value([<<"hotdesk">>, <<"users">>, AgentId], EP) of
'undefined' -> {'next_state', StateName, State};
_ ->
lager:debug("device ~s created, we're a hotdesk user, maybe adding it", [EPId]),
{'next_state', StateName, State#state{endpoints=maybe_add_endpoint(EPId, EP, EPs, AccountId, AgentListener)}, 'hibernate'}
{'next_state', StateName, State#state{endpoints=maybe_add_endpoint(EPId, EP, EPs, AccountId)}, 'hibernate'}
end
end;
handle_info(?NEW_CHANNEL_FROM(_CallId), StateName, State) ->
Expand Down Expand Up @@ -1761,69 +1758,55 @@ find_endpoint_id(EP) ->
find_endpoint_id(EP, 'undefined') -> kz_json:get_value(<<"Endpoint-ID">>, EP);
find_endpoint_id(_EP, EPId) -> EPId.

-spec monitor_endpoint(kz_json:object(), kz_term:ne_binary(), kz_types:server_ref()) -> any().
monitor_endpoint('undefined', _, _) -> 'ok';
monitor_endpoint(EP, AccountId, AgentListener) ->
-spec monitor_endpoint(kz_json:api_object(), kz_term:ne_binary()) -> any().
monitor_endpoint('undefined', _) -> 'ok';
monitor_endpoint(EP, AccountId) ->
Username = find_username(EP),

%% Bind for outbound call requests
acdc_agent_listener:add_endpoint_bindings(AgentListener
,kz_endpoint:get_sip_realm(EP, AccountId)
,find_username(EP)
),

%% Inform us of device changes
catch gproc:reg(?ENDPOINT_UPDATE_REG(AccountId, find_endpoint_id(EP))),
catch gproc:reg(?NEW_CHANNEL_REG(AccountId, Username)),
catch gproc:reg(?DESTROYED_CHANNEL_REG(AccountId, Username)).

-spec unmonitor_endpoint(kz_json:object(), kz_term:ne_binary(), kz_types:server_ref()) -> any().
unmonitor_endpoint(EP, AccountId, AgentListener) ->
-spec unmonitor_endpoint(kz_json:object(), kz_term:ne_binary()) -> any().
unmonitor_endpoint(EP, AccountId) ->
Username = find_username(EP),

%% Bind for outbound call requests
acdc_agent_listener:remove_endpoint_bindings(AgentListener
,kz_endpoint:get_sip_realm(EP, AccountId)
,find_username(EP)
),

%% Inform us of device changes
catch gproc:unreg(?ENDPOINT_UPDATE_REG(AccountId, find_endpoint_id(EP))),
catch gproc:unreg(?NEW_CHANNEL_REG(AccountId, Username)),
catch gproc:unreg(?DESTROYED_CHANNEL_REG(AccountId, Username)).

-spec maybe_add_endpoint(kz_term:ne_binary(), kz_json:object(), kz_json:objects(), kz_term:ne_binary(), kz_types:server_ref()) -> any().
maybe_add_endpoint(EPId, EP, EPs, AccountId, AgentListener) ->
-spec maybe_add_endpoint(kz_term:ne_binary(), kz_json:object(), kz_json:objects(), kz_term:ne_binary()) -> any().
maybe_add_endpoint(EPId, EP, EPs, AccountId) ->
case lists:partition(fun(E) -> find_endpoint_id(E) =:= EPId end, EPs) of
{[], _} ->
lager:debug("endpoint ~s not in our list, adding it", [EPId]),
[begin monitor_endpoint(EP, AccountId, AgentListener), EP end | EPs];
[begin monitor_endpoint(EP, AccountId), EP end | EPs];
{_, _} -> EPs
end.

-spec maybe_remove_endpoint(kz_term:ne_binary(), kz_json:objects(), kz_term:ne_binary(), kz_types:server_ref()) -> kz_json:objects().
maybe_remove_endpoint(EPId, EPs, AccountId, AgentListener) ->
-spec maybe_remove_endpoint(kz_term:ne_binary(), kz_json:objects(), kz_term:ne_binary()) -> kz_json:objects().
maybe_remove_endpoint(EPId, EPs, AccountId) ->
case lists:partition(fun(EP) -> find_endpoint_id(EP) =:= EPId end, EPs) of
{[], _} -> EPs; %% unknown endpoint
{[RemoveEP], EPs1} ->
lager:debug("endpoint ~s in our list, removing it", [EPId]),
_ = unmonitor_endpoint(RemoveEP, AccountId, AgentListener),
_ = unmonitor_endpoint(RemoveEP, AccountId),
EPs1
end.

-spec get_endpoints(kz_json:objects(), kz_types:server_ref(), kapps_call:call(), kz_term:api_binary(), kz_term:api_binary()) ->
-spec get_endpoints(kz_json:objects(), kapps_call:call(), kz_term:api_binary(), kz_term:api_binary()) ->
{'ok', kz_json:objects()} |
{'error', any()}.
get_endpoints(OrigEPs, AgentListener, Call, AgentId, QueueId) ->
get_endpoints(OrigEPs, Call, AgentId, QueueId) ->
case catch acdc_util:get_endpoints(Call, AgentId) of
[] ->
{'error', 'no_endpoints'};
[_|_]=EPs ->
AccountId = kapps_call:account_id(Call),

{Add, Rm} = changed_endpoints(OrigEPs, EPs),
_ = [monitor_endpoint(EP, AccountId, AgentListener) || EP <- Add],
_ = [unmonitor_endpoint(EP, AccountId, AgentListener) || EP <- Rm],
_ = [monitor_endpoint(EP, AccountId) || EP <- Add],
_ = [unmonitor_endpoint(EP, AccountId) || EP <- Rm],

{'ok', [kz_json:set_value([<<"Custom-Channel-Vars">>, <<"Queue-ID">>], QueueId, EP) || EP <- EPs]};
{'EXIT', E} ->
Expand Down
17 changes: 0 additions & 17 deletions applications/acdc/src/acdc_agent_listener.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
,rm_acdc_queue/2
,call_status_req/1, call_status_req/2
,fsm_started/2
,add_endpoint_bindings/3, remove_endpoint_bindings/3
,outbound_call_id/2
,remove_cdr_urls/2
,logout_agent/1
Expand Down Expand Up @@ -309,22 +308,6 @@ call_status_req(Srv, CallId) ->
fsm_started(Srv, FSM) ->
gen_listener:cast(Srv, {'fsm_started', FSM}).

-spec add_endpoint_bindings(pid(), kz_term:ne_binary(), kz_term:api_ne_binary()) -> 'ok'.
add_endpoint_bindings(_Srv, _Realm, 'undefined') ->
lager:debug("ignoring adding endpoint bindings for undefined user @ ~s", [_Realm]);
add_endpoint_bindings(Srv, Realm, User) ->
lager:debug("adding route bindings to ~p for endpoint ~s@~s", [Srv, User, Realm]),
gen_listener:add_binding(Srv, 'route', [{'realm', Realm}
,{'user', User}
]).

-spec remove_endpoint_bindings(pid(), kz_term:ne_binary(), kz_term:ne_binary()) -> 'ok'.
remove_endpoint_bindings(Srv, Realm, User) ->
lager:debug("removing route bindings to ~p for endpoint ~s@~s", [Srv, User, Realm]),
gen_listener:rm_binding(Srv, 'route', [{'realm', Realm}
,{'user', User}
]).

-spec remove_cdr_urls(pid(), kz_term:ne_binary()) -> 'ok'.
remove_cdr_urls(Srv, CallId) -> gen_listener:cast(Srv, {'remove_cdr_urls', CallId}).

Expand Down