Skip to content

Commit 3835def

Browse files
committed
Reindex resource when link status changes
1 parent d683c6f commit 3835def

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

app/models/link_monitor.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class LinkMonitor < ApplicationRecord
22
belongs_to :link_checkable, polymorphic: true, foreign_key: :lcheck_id, foreign_type: :lcheck_type
33
before_create :set_initial_date
4+
after_commit :reindex_resource, on: :update
45

56
FAILURE_THRESHOLD = 4
67

@@ -37,4 +38,18 @@ def success!
3738
def failing?
3839
fail_count >= FAILURE_THRESHOLD
3940
end
41+
42+
def status_changed?
43+
prev_count = fail_count_previously_was || 0
44+
prev_count >= FAILURE_THRESHOLD && fail_count == 0 ||
45+
prev_count < FAILURE_THRESHOLD && failing?
46+
end
47+
48+
private
49+
50+
def reindex_resource
51+
return unless TeSS::Config.solr_enabled
52+
return unless status_changed?
53+
link_checkable.solr_index
54+
end
4055
end

test/models/link_monitor_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,24 @@ class LinkMonitorTest < ActiveSupport::TestCase
9696
refute @link_monitor.failing?
9797
assert_equal 0, @link_monitor.fail_count
9898
end
99+
100+
test 'link monitor status changed' do
101+
refute @link_monitor.status_changed?
102+
103+
@link_monitor.update_column(:fail_count, 3)
104+
refute @link_monitor.failing?
105+
refute @link_monitor.status_changed?
106+
107+
@link_monitor.fail!(404)
108+
assert @link_monitor.failing?
109+
assert @link_monitor.status_changed?
110+
111+
@link_monitor.fail!(404)
112+
assert @link_monitor.failing?
113+
refute @link_monitor.status_changed?
114+
115+
@link_monitor.success
116+
refute @link_monitor.failing?
117+
assert @link_monitor.status_changed?
118+
end
99119
end

0 commit comments

Comments
 (0)