diff --git a/app/models/link_monitor.rb b/app/models/link_monitor.rb index 1bf3b4ad5..9a11a0ca9 100644 --- a/app/models/link_monitor.rb +++ b/app/models/link_monitor.rb @@ -1,6 +1,7 @@ class LinkMonitor < ApplicationRecord belongs_to :link_checkable, polymorphic: true, foreign_key: :lcheck_id, foreign_type: :lcheck_type before_create :set_initial_date + after_commit :reindex_resource, on: :update FAILURE_THRESHOLD = 4 @@ -37,4 +38,18 @@ def success! def failing? fail_count >= FAILURE_THRESHOLD end + + def status_changed? + prev_count = fail_count_previously_was || 0 + (prev_count >= FAILURE_THRESHOLD && fail_count == 0) || + (prev_count < FAILURE_THRESHOLD && failing?) + end + + private + + def reindex_resource + return unless TeSS::Config.solr_enabled + return unless status_changed? + link_checkable.solr_index + end end diff --git a/test/models/link_monitor_test.rb b/test/models/link_monitor_test.rb index 01f09fb4f..42f6926a3 100644 --- a/test/models/link_monitor_test.rb +++ b/test/models/link_monitor_test.rb @@ -96,4 +96,28 @@ class LinkMonitorTest < ActiveSupport::TestCase refute @link_monitor.failing? assert_equal 0, @link_monitor.fail_count end + + test 'link monitor status changed' do + refute @link_monitor.status_changed? + + @link_monitor.update_column(:fail_count, 3) + refute @link_monitor.failing? + refute @link_monitor.status_changed? + + @link_monitor.fail!(404) + assert @link_monitor.failing? + assert @link_monitor.status_changed? + + @link_monitor.fail!(404) + assert @link_monitor.failing? + refute @link_monitor.status_changed? + + @link_monitor.success! + refute @link_monitor.failing? + assert @link_monitor.status_changed? + + @link_monitor.success! + refute @link_monitor.failing? + refute @link_monitor.status_changed? + end end