Skip to content

Commit

Permalink
Create next month's MODBs on trigger (2600hz#6553)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesaimonetti authored May 30, 2020
1 parent 099cd69 commit 9ad88e0
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 18 deletions.
37 changes: 19 additions & 18 deletions applications/tasks/src/modules/kt_modb_creation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ create_modbs() ->
io:format("creating modbs in ~p~n", [_P]).

-spec create_modbs(kz_time:year(), kz_time:month()) -> 'ok'.
create_modbs(Year, Month) ->
create_modbs(Year, Month, kz_datamgr:get_results_count(?KZ_ACCOUNTS_DB, <<"accounts/listing_by_id">>, [])).
create_modbs(Year, CurrentMonth) ->
create_modbs(Year, CurrentMonth, kz_datamgr:get_results_count(?KZ_ACCOUNTS_DB, <<"accounts/listing_by_id">>, [])).

-spec create_modbs(kz_time:year(), kz_time:month(), {'ok', non_neg_integer()} | kz_datamgr:data_error()) -> 'ok'.
create_modbs(_Year, _Month, {'ok', 0}) -> 'ok';
create_modbs(Year, Month, {'ok', NumAccounts}) ->
NextMonthS = calendar:datetime_to_gregorian_seconds({kz_date:normalize({Year, Month+1, 1}), {0,0,0}}),
create_modbs(_Year, _CurrentMonth, {'ok', 0}) -> 'ok';
create_modbs(Year, CurrentMonth, {'ok', NumAccounts}) ->
NextOne = {NextYear, NextMonth, _} = kz_date:normalize({Year, CurrentMonth+1, 1}),
NextMonthS = calendar:datetime_to_gregorian_seconds({NextOne, {0,0,0}}),
NowS = kz_time:now_s(),
SecondsLeft = NextMonthS - NowS,

Expand All @@ -80,34 +81,34 @@ create_modbs(Year, Month, {'ok', NumAccounts}) ->
lager:info("creating ~p modbs (~p per pass), ~p seconds delay between passes"
,[NumAccounts, AccountsPerPass, SecondsPerPass]
),
create_modbs_metered(Year, Month, AccountsPerPass, SecondsPerPass).
create_modbs_metered(NextYear, NextMonth, AccountsPerPass, SecondsPerPass).

create_modbs_metered(Year, Month, AccountsPerPass, SecondsPerPass) ->
create_modbs_metered(Year, Month, AccountsPerPass, SecondsPerPass
create_modbs_metered(NextYear, NextMonth, AccountsPerPass, SecondsPerPass) ->
create_modbs_metered(NextYear, NextMonth, AccountsPerPass, SecondsPerPass
,get_page(AccountsPerPass, 'undefined')
).

create_modbs_metered(Year, Month, _AccountsPerPass, _SecondsPerPass, {'ok', Accounts, 'undefined'}) ->
_ = [create_modb(Year, Month, Account) || Account <- Accounts],
create_modbs_metered(NextYear, NextMonth, _AccountsPerPass, _SecondsPerPass, {'ok', Accounts, 'undefined'}) ->
_ = [create_modb(NextYear, NextMonth, Account) || Account <- Accounts],
lager:info("finished creating account MODBs");
create_modbs_metered(Year, Month, AccountsPerPass, SecondsPerPass, {'ok', Accounts, NextPageKey}) ->
create_modbs_metered(NextYear, NextMonth, AccountsPerPass, SecondsPerPass, {'ok', Accounts, NextPageKey}) ->
NowS = kz_time:now_s(),
_ = [create_modb(Year, Month, Account) || Account <- Accounts],
_ = [create_modb(NextYear, NextMonth, Account) || Account <- Accounts],
ElapsedS = kz_time:elapsed_s(NowS),
WaitS = SecondsPerPass - ElapsedS,
lager:info("created ~p modb(s), waiting ~ps for next pass", [length(Accounts), WaitS]),
timer:sleep(WaitS * ?MILLISECONDS_IN_SECOND),
create_modbs_metered(Year, Month, AccountsPerPass, SecondsPerPass
create_modbs_metered(NextYear, NextMonth, AccountsPerPass, SecondsPerPass
,get_page(AccountsPerPass, NextPageKey)
);
create_modbs_metered(_Year, _Month, _AccountsPerPass, _SecondsPerPass, {'error', _E}) ->
create_modbs_metered(_NextYear, _NextMonth, _AccountsPerPass, _SecondsPerPass, {'error', _E}) ->
lager:info("error paginating accounts: ~p", [_E]).

-spec create_modb(kz_time:year(), kz_time:month(), kz_json:object()) -> boolean().
create_modb(Year, Month, AccountView) ->
-spec create_modb(kz_time:year(), kz_time:month(), kz_json:object()) -> pid().
create_modb(NextYear, NextMonth, AccountView) ->
AccountId = kz_doc:id(AccountView),
AccountMODB = kz_util:format_account_id(AccountId, Year, Month),
kazoo_modb:maybe_create(AccountMODB).
AccountMODB = kz_util:format_account_id(AccountId, NextYear, NextMonth),
kz_util:spawn(fun kazoo_modb:maybe_create/1, [AccountMODB]).

-spec get_page(pos_integer(), kz_json:api_json_term()) -> kz_datamgr:paginated_results().
get_page(AccountsPerPass, 'undefined') ->
Expand Down
85 changes: 85 additions & 0 deletions core/kazoo_proper/src/pqc_kt_modb_creation.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
%%%-----------------------------------------------------------------------------
%%% @copyright (C) 2013-2020, 2600Hz
%%% @doc Test creating MODBs ahead of time
%%% @end
%%%-----------------------------------------------------------------------------
-module(pqc_kt_modb_creation).

-export([seq/0
,cleanup/0
]).

-include("kazoo_proper.hrl").

-define(ACCOUNT_NAMES, [<<?MODULE_STRING>>]).

-spec seq() -> 'ok'.
seq() ->
API = pqc_cb_api:init_api(['crossbar']
,['cb_accounts']
),
AccountId = create_account(API),

{Year, Month, Day} = erlang:date(),
CurrentMODB = kz_util:format_account_id(AccountId, Year, Month),

'true' = kz_datamgr:db_exists(CurrentMODB),

%% ensure we trigger the task
_ = kapps_config:set_default(<<"tasks.modb_creation">>, <<"creation_day">>, Day),
%% create all MODBs in the first time unit
_ = kapps_config:set_default(<<"tasks.modb_creation">>, <<"create_in_parallel">>, 1000),

{NextYear, NextMonth, _} = kz_date:normalize({Year, Month+1, 1}),
NextMODB = kz_util:format_account_id(AccountId, NextYear, NextMonth),

'false' = check_for_creation(NextMODB, 0),
lager:info("modb ~s not created yet", [NextMODB]),

%% trigger the task
kt_modb_creation:handle_req(),

lager:info("checking for '~s'", [NextMODB]),
'true' = check_for_creation(NextMODB, 5 * ?MILLISECONDS_IN_SECOND),

lager:info("CLEANUP TIME, EVERYBODY HELPS"),
cleanup(API).

check_for_creation(NextMODB, WaitForMs) ->
check_for_creation(NextMODB, WaitForMs, kz_time:start_time()).

check_for_creation(NextMODB, WaitForMs, StartTime) ->
case kz_datamgr:db_exists(NextMODB) of
'true' ->
lager:info("found ~s", [NextMODB]),
'true';
'false' ->
ElapsedMs = kz_time:elapsed_ms(StartTime),
case ElapsedMs > WaitForMs of
'true' ->
lager:info("failed to find ~s in ~p(~p)", [NextMODB, WaitForMs, ElapsedMs]),
'false';
'false' ->
timer:sleep(50),
check_for_creation(NextMODB, WaitForMs, StartTime)
end
end.

create_account(API) ->
AccountResp = pqc_cb_accounts:create_account(API, hd(?ACCOUNT_NAMES)),
?INFO("created account: ~s", [AccountResp]),

kz_json:get_value([<<"data">>, <<"id">>], kz_json:decode(AccountResp)).

-spec cleanup() -> 'ok'.
cleanup() ->
_ = pqc_cb_accounts:cleanup_accounts(?ACCOUNT_NAMES),
cleanup_system().

cleanup(API) ->
?INFO("CLEANUP TIME, EVERYBODY HELPS"),
_ = pqc_cb_accounts:cleanup_accounts(API, ?ACCOUNT_NAMES),
_ = pqc_cb_api:cleanup(API),
cleanup_system().

cleanup_system() -> 'ok'.

0 comments on commit 9ad88e0

Please sign in to comment.