mirror of
https://github.com/discourse/discourse.git
synced 2025-06-05 14:07:30 +08:00
DEV: Add DB backed problem checks to support perform_every config (#25834)
As part of problem checks refactoring, we're moving some data to be DB backed. In this PR it's the tracking of problem check execution. When was it last run, when was the last problem, when should it run next, how many consecutive checks had problems, etc. This allows us to implement the perform_every feature in scheduled problem checks for checks that don't need to be run every 10 minutes.
This commit is contained in:
47
app/models/problem_check_tracker.rb
Normal file
47
app/models/problem_check_tracker.rb
Normal file
@ -0,0 +1,47 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ProblemCheckTracker < ActiveRecord::Base
|
||||
validates :identifier, presence: true, uniqueness: true
|
||||
validates :blips, presence: true, numericality: { greater_than_or_equal_to: 0 }
|
||||
|
||||
def self.[](identifier)
|
||||
find_or_create_by(identifier:)
|
||||
end
|
||||
|
||||
def ready_to_run?
|
||||
next_run_at.blank? || next_run_at.past?
|
||||
end
|
||||
|
||||
def problem!(next_run_at: nil)
|
||||
now = Time.current
|
||||
|
||||
update!(blips: blips + 1, last_run_at: now, last_problem_at: now, next_run_at:)
|
||||
end
|
||||
|
||||
def no_problem!(next_run_at: nil)
|
||||
now = Time.current
|
||||
|
||||
update!(blips: 0, last_run_at: now, last_success_at: now, next_run_at:)
|
||||
end
|
||||
|
||||
def check
|
||||
ProblemCheck[identifier]
|
||||
end
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: problem_check_trackers
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# identifier :string not null
|
||||
# blips :integer default(0), not null
|
||||
# last_run_at :datetime
|
||||
# next_run_at :datetime
|
||||
# last_success_at :datetime
|
||||
# last_problem_at :datetime
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_problem_check_trackers_on_identifier (identifier) UNIQUE
|
||||
#
|
Reference in New Issue
Block a user