Skip to content

Commit

Permalink
[4.3] Support infinity for agent pause time (#6591)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfinke authored and jamesaimonetti committed Jun 22, 2020
1 parent b5c44a5 commit 38ac480
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 29 deletions.
19 changes: 14 additions & 5 deletions applications/acdc/src/acdc_agent_fsm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
,wrapup_ref :: kz_term:api_reference()

,sync_ref :: kz_term:api_reference()
,pause_ref :: kz_term:api_reference()
,pause_ref :: kz_term:api_reference() | 'infinity'

,member_call :: kapps_call:call() | 'undefined'
,member_call_id :: kz_term:api_binary()
Expand Down Expand Up @@ -1578,6 +1578,7 @@ time_left(Ref) when is_reference(Ref) ->
time_left(erlang:read_timer(Ref));
time_left('false') -> 'undefined';
time_left('undefined') -> 'undefined';
time_left('infinity') -> 'infinity';
time_left(Ms) when is_integer(Ms) -> Ms div 1000.

-spec clear_call(state(), atom()) -> state().
Expand Down Expand Up @@ -1668,13 +1669,14 @@ hangup_call(#state{agent_listener=AgentListener
maybe_notify(Ns, ?NOTIFY_HANGUP, State),
wrapup_timer(State).

-spec maybe_stop_timer(kz_term:api_reference()) -> 'ok'.
-spec maybe_stop_timer(kz_term:api_reference() | 'infinity') -> 'ok'.
maybe_stop_timer('undefined') -> 'ok';
maybe_stop_timer('infinity') -> 'ok';
maybe_stop_timer(ConnRef) when is_reference(ConnRef) ->
_ = erlang:cancel_timer(ConnRef),
'ok'.

-spec maybe_stop_timer(kz_term:api_reference(), boolean()) -> 'ok'.
-spec maybe_stop_timer(kz_term:api_reference() | 'infinity', boolean()) -> 'ok'.
maybe_stop_timer(TimerRef, 'true') -> maybe_stop_timer(TimerRef);
maybe_stop_timer(_, 'false') -> 'ok'.

Expand Down Expand Up @@ -1703,6 +1705,7 @@ outbound_hungup(#state{agent_listener=AgentListener
_W ->
case time_left(PRef) of
N when is_integer(N), N > 0 -> apply_state_updates(clear_call(State, 'paused'));
'infinity' -> apply_state_updates(clear_call(State, 'paused'));
_P ->
lager:debug("wrapup left: ~p pause left: ~p", [_W, _P]),
acdc_agent_listener:presence_update(AgentListener, ?PRESENCE_GREEN),
Expand Down Expand Up @@ -1947,6 +1950,7 @@ apply_state_updates(#state{agent_state_updates=Q
_W ->
case time_left(PRef) of
N when is_integer(N), N > 0 -> 'paused';
'infinity' -> 'paused';
_P -> 'ready'
end
end,
Expand Down Expand Up @@ -2025,8 +2029,13 @@ handle_resume(#state{agent_listener=AgentListener
-spec handle_pause(timeout(), state()) -> kz_types:handle_fsm_ret(state()).
handle_pause(Timeout, #state{agent_listener=AgentListener}=State) ->
acdc_agent_listener:presence_update(AgentListener, ?PRESENCE_RED_FLASH),
Ref = start_pause_timer(Timeout),
State1 = State#state{pause_ref=Ref},
State1 = case Timeout of
'infinity' ->
State#state{pause_ref='infinity'};
_ ->
Ref = start_pause_timer(Timeout),
State#state{pause_ref=Ref}
end,
{'next_state', 'paused', State1}.

-spec handle_end_wrapup(atom(), state()) -> kz_types:handle_fsm_ret(state()).
Expand Down
6 changes: 4 additions & 2 deletions applications/acdc/src/acdc_agent_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
-include("acdc.hrl").
-include_lib("kazoo_amqp/include/kapi_conf.hrl").

-define(DEFAULT_PAUSE, kapps_config:get_integer(?CONFIG_CAT, <<"default_agent_pause_timeout">>, 600)).
-define(DEFAULT_PAUSE, kapps_config:get(?CONFIG_CAT, <<"default_agent_pause_timeout">>, 600)).

-spec handle_status_update(kz_json:object(), kz_term:proplist()) -> 'ok'.
handle_status_update(JObj, _Props) ->
Expand All @@ -43,7 +43,7 @@ handle_status_update(JObj, _Props) ->
maybe_stop_agent(AccountId, AgentId, JObj);
<<"pause">> ->
'true' = kapi_acdc_agent:pause_v(JObj),
Timeout = kz_json:get_integer_value(<<"Time-Limit">>, JObj, ?DEFAULT_PAUSE),
Timeout = kz_json:get_value(<<"Time-Limit">>, JObj, ?DEFAULT_PAUSE),
maybe_pause_agent(AccountId, AgentId, Timeout, JObj);
<<"resume">> ->
'true' = kapi_acdc_agent:resume_v(JObj),
Expand Down Expand Up @@ -165,6 +165,8 @@ maybe_stop_agent(AccountId, AgentId, JObj) ->

end.

maybe_pause_agent(AccountId, AgentId, <<"infinity">>, JObj) ->
maybe_pause_agent(AccountId, AgentId, 'infinity', JObj);
maybe_pause_agent(AccountId, AgentId, Timeout, JObj) ->
case acdc_agents_sup:find_agent_supervisor(AccountId, AgentId) of
'undefined' -> lager:debug("agent ~s (~s) not found, nothing to do", [AgentId, AccountId]);
Expand Down
2 changes: 1 addition & 1 deletion applications/acdc/src/acdc_agent_stats.erl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ agent_wrapup(AccountId, AgentId, WaitTime) ->
,fun kapi_acdc_stats:publish_status_wrapup/1
).

-spec agent_paused(kz_term:ne_binary(), kz_term:ne_binary(), kz_term:api_integer()) -> 'ok'.
-spec agent_paused(kz_term:ne_binary(), kz_term:ne_binary(), timeout() | 'undefined') -> 'ok'.
agent_paused(AccountId, AgentId, 'undefined') ->
lager:debug("undefined pause time for ~s(~s)", [AgentId, AccountId]);
agent_paused(AccountId, AgentId, PauseTime) ->
Expand Down
2 changes: 1 addition & 1 deletion applications/acdc/src/acdc_stats.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
,status :: kz_term:api_binary() | '$4' | '_'

,wait_time :: kz_term:api_integer() | '_'
,pause_time :: kz_term:api_integer() | '_'
,pause_time :: timeout() | 'undefined' | '_'
,callid :: kz_term:api_binary() | '_'
,caller_id_name :: kz_term:api_binary() | '_'
,caller_id_number :: kz_term:api_binary() | '_'
Expand Down
9 changes: 5 additions & 4 deletions applications/acdc/src/acdc_stats_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
wait_time(<<"paused">>, _) -> 'undefined';
wait_time(_, JObj) -> kz_json:get_integer_value(<<"Wait-Time">>, JObj).

-spec pause_time(kz_term:ne_binary(), kz_json:object()) -> kz_term:api_integer().
-spec pause_time(kz_term:ne_binary(), kz_json:object()) -> timeout() | 'undefined'.
pause_time(<<"paused">>, JObj) ->
case kz_json:get_integer_value(<<"Pause-Time">>, JObj) of
'undefined' -> kz_json:get_integer_value(<<"Wait-Time">>, JObj);
PT -> PT
case kz_json:get_value(<<"Timeout">>, JObj) of
'undefined' -> 'undefined';
<<"infinity">> -> 'infinity';
Timeout -> kz_term:to_integer(Timeout)
end;
pause_time(_, _JObj) -> 'undefined'.

Expand Down
11 changes: 1 addition & 10 deletions applications/acdc/src/cb_agents.erl
Original file line number Diff line number Diff line change
Expand Up @@ -645,16 +645,7 @@ validate_status_change_params(Context, <<"pause">>) ->
,Context
)
catch
_E:_R ->
lager:debug("bad int for pause: ~s: ~p", [_E, _R]),
cb_context:add_validation_error(<<"timeout">>
,<<"type">>
,kz_json:from_list(
[{<<"message">>, <<"value must be an integer greater than or equal to 0">>}
,{<<"cause">>, Value}
])
,Context
)
_E:_R -> cb_context:set_resp_status(Context, 'success')
end;
validate_status_change_params(Context, _S) ->
lager:debug("great success for ~s", [_S]),
Expand Down
12 changes: 6 additions & 6 deletions applications/acdc/src/cf_acdc_agent.erl
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ logout_agent(Call, AgentId) ->
logout_agent(Call, AgentId, Data) ->
update_agent_status(Call, AgentId, Data, fun kapi_acdc_agent:publish_logout/1).

pause_agent(Call, AgentId, Data, Timeout) when is_integer(Timeout) ->
pause_agent(Call, AgentId, Data, Timeout) ->
_ = play_agent_pause(Call),
update_agent_status(Call, AgentId, Data, fun kapi_acdc_agent:publish_pause/1, Timeout).
pause_agent(Call, AgentId, Data) ->
Timeout = kz_json:get_integer_value(<<"timeout">>
,Data
,kapps_config:get_integer(<<"acdc">>, <<"default_agent_pause_timeout">>, 600)
),
Timeout = kz_json:get_value(<<"timeout">>
,Data
,kapps_config:get(<<"acdc">>, <<"default_agent_pause_timeout">>, 600)
),
lager:info("agent ~s is pausing work for ~b s", [AgentId, Timeout]),
pause_agent(Call, AgentId, Data, Timeout).

Expand All @@ -169,7 +169,7 @@ update_agent_status(Call, AgentId, Data, PubFun) ->
update_agent_status(Call, AgentId, Data, PubFun, Timeout) ->
send_new_status(Call, AgentId, Data, PubFun, Timeout).

-spec send_new_status(kapps_call:call(), kz_term:ne_binary(), kz_json:object(), kz_amqp_worker:publish_fun(), kz_term:api_integer()) -> 'ok'.
-spec send_new_status(kapps_call:call(), kz_term:ne_binary(), kz_json:object(), kz_amqp_worker:publish_fun(), kz_term:api_integer() | kz_term:ne_binary()) -> 'ok'.
send_new_status(Call, AgentId, Data, PubFun, Timeout) ->
Update = props:filter_undefined(
[{<<"Account-ID">>, kapps_call:account_id(Call)}
Expand Down

0 comments on commit 38ac480

Please sign in to comment.