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
end
def enable!
return if recent_failures == 0 && disabled_until.nil? && backoff_count == 0
update!(recent_failures: 0, disabled_until: nil, backoff_count: 0)
end
......
......@@ -268,11 +268,29 @@ RSpec.describe WebHook do
end
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
expect { hook.enable! }.to change(hook, :executable?).from(false).to(true)
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
describe 'backoff!' do
......@@ -298,6 +316,7 @@ RSpec.describe WebHook do
it 'does not allow the failure count to exceed the maximum value' do
hook.recent_failures = described_class::MAX_FAILURES
expect(hook).not_to receive(:update!)
expect { hook.failed! }.not_to change(hook, :recent_failures)
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