Commit 7b1aa603 authored by Alex Kalderimis's avatar Alex Kalderimis

Avoid some unnecessary DB writes

Enabling an enabled webhook should be a no-op. This change eliminates
some unnecessary writes to the DB.
parent 7cc81d93
...@@ -73,6 +73,8 @@ class WebHook < ApplicationRecord ...@@ -73,6 +73,8 @@ class WebHook < ApplicationRecord
end end
def enable! def enable!
return if recent_failures == 0 && disabled_until.nil? && backoff_count == 0
update!(recent_failures: 0, disabled_until: nil, backoff_count: 0) update!(recent_failures: 0, disabled_until: nil, backoff_count: 0)
end end
......
...@@ -268,11 +268,29 @@ RSpec.describe WebHook do ...@@ -268,11 +268,29 @@ RSpec.describe WebHook do
end end
describe '#enable!' do describe '#enable!' do
it 'makes a hook executable' do it 'makes a hook executable if it was marked as failed' do
hook.recent_failures = 1000 hook.recent_failures = 1000
expect { hook.enable! }.to change(hook, :executable?).from(false).to(true) expect { hook.enable! }.to change(hook, :executable?).from(false).to(true)
end end
it 'makes a hook executable if it is currently backed off' do
hook.disabled_until = 1.hour.from_now
expect { hook.enable! }.to change(hook, :executable?).from(false).to(true)
end
it 'does not update hooks unless necessary' do
expect(hook).not_to receive(:update!)
hook.enable!
end
it 'is idempotent on executable hooks' do
expect(hook).not_to receive(:update!)
expect { hook.enable! }.not_to change(hook, :executable?)
end
end end
describe 'backoff!' do describe 'backoff!' do
...@@ -298,6 +316,7 @@ RSpec.describe WebHook do ...@@ -298,6 +316,7 @@ RSpec.describe WebHook do
it 'does not allow the failure count to exceed the maximum value' do it 'does not allow the failure count to exceed the maximum value' do
hook.recent_failures = described_class::MAX_FAILURES hook.recent_failures = described_class::MAX_FAILURES
expect(hook).not_to receive(:update!)
expect { hook.failed! }.not_to change(hook, :recent_failures) expect { hook.failed! }.not_to change(hook, :recent_failures)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment