Skip to content

Commit

Permalink
PISTON-221: only record in ACDc once per call (#6736)
Browse files Browse the repository at this point in the history
- prevent multiple overlapping recordings if caller re-enters a queue
  • Loading branch information
danielfinke authored Sep 29, 2021
1 parent c1a8734 commit cd49225
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 5 deletions.
8 changes: 4 additions & 4 deletions applications/acdc/src/acdc_agent_listener.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1188,10 +1188,10 @@ maybe_start_recording(Call, 'true', Url) ->
,{<<"url">>, Url}
]),
lager:debug("starting recording listener for ~s", [Url]),
case acdc_recordings_sup:new(Call, RecordingJObj) of
{'ok', _P} ->
lager:debug("recording tracked in ~p", [_P]);
_E -> lager:debug("failed to start recording: ~p", [_E])
try acdc_recordings_map_srv:register(Call, RecordingJObj) of
_P -> lager:debug("recording tracked in ~p", [_P])
catch
'exit':_E -> lager:debug("failed to start recording: ~p", [_E])
end.

recording_format() ->
Expand Down
122 changes: 122 additions & 0 deletions applications/acdc/src/acdc_recordings_map_srv.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
%%%-----------------------------------------------------------------------------
%%% @copyright (C) 2016-2021, 2600Hz
%%% @doc
%%% @author Daniel Finke
%%% @end
%%%-----------------------------------------------------------------------------
-module(acdc_recordings_map_srv).

-behaviour(gen_server).

%% API
-export([start_link/0]).
-export([register/2]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).

-include("acdc.hrl").

-define(SERVER, ?MODULE).

-record(state, {table_id :: ets:tid()}).
-type state() :: #state{}.

%%%=============================================================================
%%% API
%%%=============================================================================

%%------------------------------------------------------------------------------
%% @doc Starts the server
%%
%% @end
%%------------------------------------------------------------------------------
-spec start_link() -> kz_types:startlink_ret().
start_link() ->
gen_server:start_link({'local', ?SERVER}, ?MODULE, [], []).

-spec register(kapps_call:call(), kz_json:object()) -> pid().
register(Call, RecordingJObj) ->
gen_server:call(?SERVER, {'register', Call, RecordingJObj}).

%%%=============================================================================
%%% gen_server callbacks
%%%=============================================================================

%%------------------------------------------------------------------------------
%% @doc Initializes the server
%%
%% @end
%%------------------------------------------------------------------------------
-spec init([]) -> {'ok', state()}.
init([]) ->
process_flag('trap_exit', 'true'),
TabId = ets:new('map', []),
{'ok', #state{table_id=TabId}}.

%%------------------------------------------------------------------------------
%% @doc Handling call messages
%%
%% @end
%%------------------------------------------------------------------------------
-spec handle_call(any(), kz_term:pid_ref(), state()) -> kz_types:handle_call_ret_state(state()).
handle_call({'register', Call, RecordingJObj}, _From, #state{table_id=TabId}=State) ->
case ets:lookup(TabId, kapps_call:call_id(Call)) of
[] ->
{'ok', Pid} = acdc_recordings_sup:new(Call, RecordingJObj),
link(Pid),
ets:insert(TabId, {kapps_call:call_id(Call), Pid}),
Pid;
[{_, Pid}] -> Pid
end,
{'reply', Pid, State};
handle_call(_Request, _From, State) ->
Reply = 'ok',
{'reply', Reply, State}.

%%------------------------------------------------------------------------------
%% @doc Handling cast messages
%%
%% @end
%%------------------------------------------------------------------------------
-spec handle_cast(any(), state()) -> kz_types:handle_cast_ret_state(state()).
handle_cast(_Msg, State) ->
{'noreply', State}.

%%------------------------------------------------------------------------------
%% @doc Handling all non call/cast messages
%%
%% @end
%%------------------------------------------------------------------------------
-spec handle_info(any(), state()) -> kz_types:handle_info_ret_state(state()).
handle_info({'EXIT', Pid, _Reason}, #state{table_id=TabId}=State) ->
ets:match_delete(TabId, {'$1', Pid}),
{'noreply', State};
handle_info(_Info, State) ->
{'noreply', State}.

%%------------------------------------------------------------------------------
%% @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.
%%
%% @end
%%------------------------------------------------------------------------------
-spec terminate(any(), state()) -> 'ok'.
terminate(_Reason, _State) ->
'ok'.

%%------------------------------------------------------------------------------
%% @doc Convert process state when code is changed
%%
%% @end
%%------------------------------------------------------------------------------
-spec code_change(any(), state(), any()) -> {'ok', state()}.
code_change(_OldVsn, State, _Extra) ->
{'ok', State}.

%%%=============================================================================
%%% Internal functions
%%%=============================================================================
2 changes: 1 addition & 1 deletion applications/acdc/src/acdc_recordings_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

-define(SERVER, ?MODULE).

-define(CHILDREN, [?WORKER_TYPE('kz_media_recording', 'transient')]).
-define(CHILDREN, [?WORKER_TYPE('kzc_recording', 'transient')]).

%%%=============================================================================
%%% API functions
Expand Down
1 change: 1 addition & 0 deletions applications/acdc/src/acdc_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
,?WORKER('acdc_agent_manager')
,?WORKER('acdc_init')
,?WORKER('acdc_listener')
,?WORKER('acdc_recordings_map_srv')
]).

%%==============================================================================
Expand Down

0 comments on commit cd49225

Please sign in to comment.