Skip to content

Commit

Permalink
[4.3] If the control queue isn't ready, wait for it (#6474)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesaimonetti authored Apr 22, 2020
1 parent 8807437 commit 4f11a1d
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions applications/ecallmgr/src/ecallmgr_originate.erl
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ handle_cast({'originate_ready'}, #state{node=_Node}=State) ->
,server_id=ServerId
,queue=Q
}=State1} ->
CtrlQ = gen_listener:queue_name(Pid),
CtrlQ = call_control_queue(Pid),
_ = publish_originate_ready(CtrlQ, UUID, JObj, Q, ServerId),
{'noreply', State1#state{tref=start_abandon_timer()}};
{'error', _E} ->
Expand Down Expand Up @@ -290,7 +290,7 @@ handle_cast({'originate_execute'}, #state{dialstrings=Dialstrings
lager:debug("originate is executing, waiting for completion"),
erlang:monitor_node(Node, 'true'),
bind_to_call_events(CallId),
CtrlQ = ecallmgr_call_control:queue_name(CtrlPid),
CtrlQ = call_control_queue(CtrlPid),
_ = publish_originate_started(ServerId, CallId, JObj, CtrlQ),
{'noreply', State#state{uuid={'api', CallId}}};
{'error', Error} ->
Expand Down Expand Up @@ -595,7 +595,6 @@ update_uuid(OldUUID, NewUUID) ->
bind_to_call_events(NewUUID),
'ok'.


-spec create_uuid(atom()) -> created_uuid().
create_uuid(_Node) -> {'fs', kz_binary:rand_hex(18)}.

Expand All @@ -617,15 +616,17 @@ create_uuid(Endpoint, _JObj, Node) ->
get_unset_vars(JObj) ->
%% Refactor (Karl wishes he had unit tests here for you to use)
ExportProps = [{K, <<>>} || K <- kz_json:get_value(<<"Export-Custom-Channel-Vars">>, JObj, [])],
Export = [K || KV <- lists:foldr(fun ecallmgr_fs_xml:kazoo_var_to_fs_var/2
,[]
,[{<<"Custom-Channel-Vars">>, kz_json:from_list(ExportProps)}]
),
FSVars = lists:foldr(fun ecallmgr_fs_xml:kazoo_var_to_fs_var/2
,[]
,[{<<"Custom-Channel-Vars">>, kz_json:from_list(ExportProps)}]
),
Export = [K || KV <- FSVars,
([K, _] = string:tokens(binary_to_list(KV), "=")) =/= 'undefined'
],
case ["unset:" ++ K
|| KV <- lists:foldr(fun ecallmgr_fs_xml:kazoo_var_to_fs_var/2, [], kz_json:to_proplist(JObj))
,not lists:member(begin [K, _] = string:tokens(binary_to_list(KV), "="), K end, Export)]
|| KV <- lists:foldr(fun ecallmgr_fs_xml:kazoo_var_to_fs_var/2, [], kz_json:to_proplist(JObj)),
not lists:member(begin [K, _] = string:tokens(binary_to_list(KV), "="), K end, Export)
]
of
[] -> "";
Unset ->
Expand Down Expand Up @@ -662,8 +663,7 @@ maybe_fix_fs_auto_answer_bug(Export) ->

-spec maybe_fix_caller_id(kz_term:strings(), kz_json:object()) -> string().
maybe_fix_caller_id(Export, JObj) ->
Fix = [
{lists:member("origination_callee_id_name", Export), kz_json:get_value(<<"Outbound-Callee-ID-Name">>, JObj), "effective_caller_id_name"}
Fix = [{lists:member("origination_callee_id_name", Export), kz_json:get_value(<<"Outbound-Callee-ID-Name">>, JObj), "effective_caller_id_name"}
,{lists:member("origination_callee_id_number", Export), kz_json:get_value(<<"Outbound-Callee-ID-Number">>, JObj), "effective_caller_id_number"}
],
string:join([ "^set:" ++ Key ++ "=" ++ erlang:binary_to_list(Value) || {IsTrue, Value, Key} <- Fix, IsTrue ], ":").
Expand All @@ -674,7 +674,7 @@ publish_error(Error, {_, UUID}, Request, ServerId) ->
publish_error(Error, UUID, Request, ServerId);
publish_error(Error, UUID, Request, ServerId) ->
lager:debug("originate error: ~s", [Error]),
E = [{<<"Msg-ID">>, kz_json:get_value(<<"Msg-ID">>, Request)}
E = [{<<"Msg-ID">>, kz_api:msg_id(Request)}
,{<<"Call-ID">>, UUID}
,{<<"Request">>, Request}
,{<<"Error-Message">>, cleanup_error(Error)}
Expand All @@ -691,7 +691,7 @@ publish_originate_ready(CtrlQ, {_, UUID}, Request, Q, ServerId) ->
publish_originate_ready(CtrlQ, UUID, Request, Q, ServerId);
publish_originate_ready(CtrlQ, UUID, Request, Q, ServerId) ->
lager:debug("originate command is ready, waiting for originate_execute"),
Props = [{<<"Msg-ID">>, kz_json:get_value(<<"Msg-ID">>, Request, UUID)}
Props = [{<<"Msg-ID">>, kz_api:msg_id(Request, UUID)}
,{<<"Call-ID">>, UUID}
,{<<"Control-Queue">>, CtrlQ}
| kz_api:default_headers(Q, ?APP_NAME, ?APP_VERSION)
Expand All @@ -703,7 +703,9 @@ publish_originate_resp('undefined', _) -> 'ok';
publish_originate_resp(ServerId, JObj) ->
Resp = kz_json:set_values([{<<"Event-Category">>, <<"resource">>}
,{<<"Event-Name">>, <<"originate_resp">>}
], JObj),
]
,JObj
),
kapi_resource:publish_originate_resp(ServerId, Resp).

-spec publish_originate_resp(kz_term:api_binary(), kz_json:object(), kz_term:ne_binary()) -> 'ok'.
Expand All @@ -713,15 +715,17 @@ publish_originate_resp(ServerId, JObj, UUID) ->
,{<<"Application-Response">>, <<"SUCCESS">>}
,{<<"Event-Name">>, <<"originate_resp">>}
,{<<"Call-ID">>, UUID}
], JObj),
]
,JObj
),
kapi_resource:publish_originate_resp(ServerId, Resp).

-spec publish_originate_started(kz_term:api_binary(), kz_term:ne_binary(), kz_json:object(), kz_term:ne_binary()) -> 'ok'.
publish_originate_started('undefined', _, _, _) -> 'ok';
publish_originate_started(ServerId, CallId, JObj, CtrlQ) ->
Resp = kz_json:from_list(
[{<<"Call-ID">>, CallId}
,{<<"Msg-ID">>, kz_json:get_value(<<"Msg-ID">>, JObj)}
,{<<"Msg-ID">>, kz_api:msg_id(JObj)}
,{<<"Control-Queue">>, CtrlQ}
| kz_api:default_headers(?APP_NAME, ?APP_VERSION)
]),
Expand All @@ -732,7 +736,7 @@ publish_originate_uuid('undefined', _, _, _) -> 'ok';
publish_originate_uuid(ServerId, UUID, JObj, CtrlQueue) ->
Resp = props:filter_undefined(
[{<<"Outbound-Call-ID">>, UUID}
,{<<"Msg-ID">>, kz_json:get_value(<<"Msg-ID">>, JObj)}
,{<<"Msg-ID">>, kz_api:msg_id(JObj)}
,{<<"Outbound-Call-Control-Queue">>, CtrlQueue}
| kz_api:default_headers(?APP_NAME, ?APP_VERSION)
]),
Expand All @@ -743,9 +747,20 @@ publish_originate_uuid(ServerId, UUID, JObj, CtrlQueue) ->
maybe_send_originate_uuid({_, UUID}, Pid, #state{server_id=ServerId
,originate_req=JObj
}) ->
CtlQ = gen_listener:queue_name(Pid),
CtlQ = call_control_queue(Pid),
publish_originate_uuid(ServerId, UUID, JObj, CtlQ).

-spec call_control_queue(pid()) -> kz_term:ne_binary().
call_control_queue(Pid) ->
call_control_queue(Pid, ecallmgr_call_control:queue_name(Pid)).

-spec call_control_queue(pid(), kz_term:api_ne_binary()) -> kz_term:ne_binary().
call_control_queue(_Pid, <<QueueName/binary>>) -> QueueName;
call_control_queue(Pid, 'undefined') ->
lager:debug("waiting on call control ~p to get setup", [Pid]),
timer:sleep(100),
call_control_queue(Pid).

-spec find_originate_timeout(kz_json:object()) -> pos_integer().
find_originate_timeout(JObj) ->
OTimeout = case kz_json:get_integer_value(<<"Timeout">>, JObj) of
Expand Down

0 comments on commit 4f11a1d

Please sign in to comment.