Skip to content

Commit

Permalink
Merge pull request #1527 from yeti-switch/1526-CallsMonitoring-teardo…
Browse files Browse the repository at this point in the history
…wn-rules

config to control the behavior of the CallsMonitoring job
  • Loading branch information
dmitry-sinina authored Sep 3, 2024
2 parents bcd4f46 + 31cb475 commit 3be8897
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 12 deletions.
45 changes: 34 additions & 11 deletions app/jobs/jobs/calls_monitoring.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,12 @@ def execute
detect_vendors_calls_to_reject
end

log_time('detect_gateway_calls_to_reject') do
detect_gateway_calls_to_reject
log_time('detect_orig_gateway_calls_to_reject') do
detect_orig_gateway_calls_to_reject
end

log_time('detect_term_gateway_calls_to_reject') do
detect_term_gateway_calls_to_reject
end

log_time('detect_random_calls_to_reject') do
Expand All @@ -188,8 +192,6 @@ def before_finish
end
end

private

# random_disconnect_enable | f
# random_disconnect_length | 7000
def detect_random_calls_to_reject
Expand Down Expand Up @@ -273,9 +275,18 @@ def detect_vendors_calls_to_reject
end
end

def teardown_enabled?(config_key)
value = YetiConfig.calls_monitoring.send(config_key)
return true if value.nil? # Default behavior

value
end

# drop calls where `customer_auth_id`
# is linked to `CustomersAuth#reject_calls = true`
def detect_customers_auth_calls_to_reject
return unless teardown_enabled?(:teardown_on_disabled_customer_auth)

flatten_calls.each do |call|
customers_auth_id = call[:customer_auth_id]
customers_auth = active_customers_auths_reject_calls[customers_auth_id]
Expand All @@ -286,14 +297,22 @@ def detect_customers_auth_calls_to_reject
end
end

# detect gateway is disabled by orig_gw_id and term_gw_id
def detect_gateway_calls_to_reject
flatten_calls.each do |call|
if disabled_orig_gw_active_calls.key?(call[:orig_gw_id]) || disabled_term_gw_active_calls.key?(call[:term_gw_id])
local_tag = call[:local_tag]
@terminate_calls[local_tag] = call
end
def detect_orig_gateway_calls_to_reject
return unless teardown_enabled?(:teardown_on_disabled_orig_gw)

calls_to_terminate = flatten_calls.select do |call|
disabled_orig_gw_active_calls.key?(call[:orig_gw_id])
end
terminate_calls(calls_to_terminate)
end

def detect_term_gateway_calls_to_reject
return unless teardown_enabled?(:teardown_on_disabled_term_gw)

calls_to_terminate = flatten_calls.select do |call|
disabled_term_gw_active_calls.key?(call[:term_gw_id])
end
terminate_calls(calls_to_terminate)
end

# @see ActiveCallsCollector#collect
Expand Down Expand Up @@ -416,6 +435,10 @@ def terminate_calls!
end
end

def terminate_calls(calls)
@terminate_calls.merge!(calls.index_by { |call| call[:local_tag] })
end

def flatten_calls
@flatten_calls ||= active_calls.values.flatten
end
Expand Down
3 changes: 3 additions & 0 deletions config/initializers/_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def self.setting_files(config_root, _env)
required(:calls_monitoring).schema do
required(:write_account_stats).value(:bool?)
required(:write_gateway_stats).value(:bool?)
required(:teardown_on_disabled_customer_auth).value(:bool?)
required(:teardown_on_disabled_term_gw).value(:bool?)
required(:teardown_on_disabled_orig_gw).value(:bool?)
end

required(:api).schema do
Expand Down
3 changes: 3 additions & 0 deletions config/yeti_web.yml.ci
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ site_title_image: "yeti.png"
calls_monitoring:
write_account_stats: true
write_gateway_stats: true
teardown_on_disabled_customer_auth: false # Teardown calls if customer authentication is disabled
teardown_on_disabled_term_gw: false # Teardown calls if termination gateway is disabled
teardown_on_disabled_orig_gw: false # Teardown calls if origination gateway is disabled
api:
token_lifetime: 600 # jwt token lifetime in seconds, empty string means permanent tokens
cdr_export:
Expand Down
3 changes: 3 additions & 0 deletions config/yeti_web.yml.development
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ site_title_image: "yeti.png"
calls_monitoring:
write_account_stats: true
write_gateway_stats: true
teardown_on_disabled_customer_auth: false # Teardown calls if customer authentication is disabled
teardown_on_disabled_term_gw: false # Teardown calls if termination gateway is disabled
teardown_on_disabled_orig_gw: false # Teardown calls if origination gateway is disabled
api:
token_lifetime: 600 # jwt token lifetime in seconds, empty string means permanent tokens
cdr_export:
Expand Down
3 changes: 3 additions & 0 deletions config/yeti_web.yml.distr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ site_title_image: "yeti.png"
calls_monitoring:
write_account_stats: true
write_gateway_stats: true
teardown_on_disabled_customer_auth: false # Teardown calls if customer authentication is disabled
teardown_on_disabled_term_gw: false # Teardown calls if termination gateway is disabled
teardown_on_disabled_orig_gw: false # Teardown calls if origination gateway is disabled
api:
token_lifetime: 600 # jwt token lifetime in seconds, empty string means permanent tokens
cdr_export:
Expand Down
5 changes: 4 additions & 1 deletion spec/config/yeti_web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
site_title_image: be_kind_of(String),
calls_monitoring: {
write_account_stats: be_one_of(true, false),
write_gateway_stats: be_one_of(true, false)
write_gateway_stats: be_one_of(true, false),
teardown_on_disabled_customer_auth: be_one_of(true, false),
teardown_on_disabled_term_gw: be_one_of(true, false),
teardown_on_disabled_orig_gw: be_one_of(true, false)
},
api: {
token_lifetime: be_kind_of(Integer)
Expand Down
90 changes: 90 additions & 0 deletions spec/jobs/jobs/calls_monitoring_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -419,31 +419,99 @@
context 'when origin gw disabled for origination' do
before do
origin_gateway.update!(allow_origination: false)
allow(YetiConfig.calls_monitoring).to receive(:teardown_on_disabled_orig_gw).and_return(nil)
end
include_examples :drop_calls
end

context 'when origin gw disabled for origination and teardown_on_disabled_orig_gw is true' do
before do
origin_gateway.update!(allow_origination: false)
allow(YetiConfig.calls_monitoring).to receive(:teardown_on_disabled_orig_gw).and_return(true)
end
include_examples :drop_calls
end

context 'when origin gw disabled for origination and teardown_on_disabled_orig_gw is false' do
before do
origin_gateway.update!(allow_origination: false)
allow(YetiConfig.calls_monitoring).to receive(:teardown_on_disabled_orig_gw).and_return(false)
end
include_examples :keep_calls
end

context 'when term gw disabled' do
before do
term_gateway.disable!
allow(YetiConfig.calls_monitoring).to receive(:teardown_on_disabled_term_gw).and_return(nil)
end
include_examples :drop_calls
end

context 'when term gw disabled and teardown_on_disabled_term_gw is true' do
before do
term_gateway.disable!
allow(YetiConfig.calls_monitoring).to receive(:teardown_on_disabled_term_gw).and_return(true)
end
include_examples :drop_calls
end

context 'when term gw disabled teardown_on_disabled_term_gw is false' do
before do
term_gateway.disable!
allow(YetiConfig.calls_monitoring).to receive(:teardown_on_disabled_term_gw).and_return(false)
end
include_examples :keep_calls
end

context 'when term gw disabled for termination' do
before do
term_gateway.update!(allow_termination: false)
allow(YetiConfig.calls_monitoring).to receive(:teardown_on_disabled_term_gw).and_return(nil)
end
include_examples :drop_calls
end

context 'when term gw disabled for termination and teardown_on_disabled_term_gw is true' do
before do
term_gateway.update!(allow_termination: false)
allow(YetiConfig.calls_monitoring).to receive(:teardown_on_disabled_term_gw).and_return(true)
end
include_examples :drop_calls
end

context 'when term gw disabled for termination and teardown_on_disabled_term_gw is false' do
before do
term_gateway.update!(allow_termination: false)
allow(YetiConfig.calls_monitoring).to receive(:teardown_on_disabled_term_gw).and_return(false)
end
include_examples :keep_calls
end

context 'when origin gw disabled' do
before do
origin_gateway.disable!
allow(YetiConfig.calls_monitoring).to receive(:teardown_on_disabled_orig_gw).and_return(nil)
end
include_examples :drop_calls
end

context 'when origin gw disabled and teardown_on_disabled_orig_gw is true' do
before do
origin_gateway.disable!
allow(YetiConfig.calls_monitoring).to receive(:teardown_on_disabled_orig_gw).and_return(true)
end
include_examples :drop_calls
end

context 'when origin gw disabled and teardown_on_disabled_orig_gw is false' do
before do
origin_gateway.disable!
allow(YetiConfig.calls_monitoring).to receive(:teardown_on_disabled_orig_gw).and_return(false)
end
include_examples :keep_calls
end

context 'when Customer has zero balance' do
let(:account_balance) do
0
Expand Down Expand Up @@ -631,6 +699,28 @@
context 'when CustomersAuth#reject_calls = TRUE' do
let(:customers_auth_reject_calls) { true }

before { allow(YetiConfig.calls_monitoring).to receive(:teardown_on_disabled_customer_auth).and_return(nil) }

it 'drop first call(with customer_auth_id)' do
expect_any_instance_of(Node).to receive(:drop_call).with('normal-call')
expect_any_instance_of(Node).not_to receive(:drop_call).with('reverse-call')
subject
end
end

context 'when CustomersAuth#reject_calls = TRUE and teardown_on_disabled_customer_auth is false' do
let(:customers_auth_reject_calls) { true }

before { allow(YetiConfig.calls_monitoring).to receive(:teardown_on_disabled_customer_auth).and_return(false) }

include_examples :keep_calls
end

context 'when CustomersAuth#reject_calls = TRUE and teardown_on_disabled_customer_auth is true' do
let(:customers_auth_reject_calls) { true }

before { allow(YetiConfig.calls_monitoring).to receive(:teardown_on_disabled_customer_auth).and_return(true) }

it 'drop first call(with customer_auth_id)' do
expect_any_instance_of(Node).to receive(:drop_call).with('normal-call')
expect_any_instance_of(Node).not_to receive(:drop_call).with('reverse-call')
Expand Down

0 comments on commit 3be8897

Please sign in to comment.