2018-05-29 10:28:03 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: checks
|
|
|
|
#
|
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# active :boolean default(TRUE), not null
|
|
|
|
# comment :string(255)
|
|
|
|
# domain :string(255) not null
|
|
|
|
# domain_created_at :datetime
|
2018-06-02 17:45:50 +02:00
|
|
|
# domain_expires_at :datetime
|
2018-05-29 10:28:03 +02:00
|
|
|
# domain_updated_at :datetime
|
|
|
|
# kind :integer not null
|
|
|
|
# last_run_at :datetime
|
|
|
|
# last_success_at :datetime
|
|
|
|
# vendor :string(255)
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# user_id :bigint(8)
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_checks_on_user_id (user_id)
|
|
|
|
#
|
|
|
|
# Foreign Keys
|
|
|
|
#
|
|
|
|
# fk_rails_... (user_id => users.id)
|
|
|
|
#
|
|
|
|
|
|
|
|
class Check < ApplicationRecord
|
|
|
|
belongs_to :user
|
2018-05-30 16:50:41 +02:00
|
|
|
has_many :logs, class_name: "CheckLog"
|
2018-06-04 20:39:53 +02:00
|
|
|
has_many :notifications, validate: true, dependent: :destroy
|
|
|
|
accepts_nested_attributes_for :notifications,
|
|
|
|
allow_destroy: true,
|
2018-06-05 22:44:48 +02:00
|
|
|
reject_if: lambda { |at| at["recipient"].blank? && at["interval"].blank? }
|
2018-05-29 10:28:03 +02:00
|
|
|
|
|
|
|
enum kind: [:domain, :ssl]
|
|
|
|
|
|
|
|
self.skip_time_zone_conversion_for_attributes = [
|
|
|
|
:domain_created_at,
|
|
|
|
:domain_updated_at,
|
2018-06-02 17:45:50 +02:00
|
|
|
:domain_expires_at,
|
2018-05-29 10:28:03 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
validates :kind, presence: true
|
|
|
|
validates :domain, presence: true
|
2018-05-29 13:11:22 +02:00
|
|
|
validate :domain_created_at_past
|
|
|
|
validate :domain_updated_at_past
|
|
|
|
validates :comment, length: { maximum: 255 }
|
|
|
|
validates :vendor, length: { maximum: 255 }
|
|
|
|
|
2018-06-04 14:06:37 +02:00
|
|
|
after_update :reset_notifications
|
2018-05-30 18:16:43 +02:00
|
|
|
after_save :enqueue_sync
|
|
|
|
|
2018-06-05 15:24:44 +02:00
|
|
|
scope :active, -> { where(active: true) }
|
|
|
|
scope :last_run_failed, -> {
|
|
|
|
where("(last_success_at IS NULL AND last_run_at IS NOT NULL)
|
|
|
|
OR (last_success_at <= DATE_SUB(last_run_at, INTERVAL 5 MINUTE))")
|
|
|
|
}
|
|
|
|
|
2018-07-03 20:11:52 +02:00
|
|
|
scope :kind, ->(kind) { where(kind: kind) }
|
|
|
|
scope :by_domain, ->(domain) { where("domain LIKE ?", "%#{domain}%") }
|
2018-07-04 11:55:44 +02:00
|
|
|
scope :recurrent_failures, -> {
|
2018-07-04 12:33:50 +02:00
|
|
|
where("last_run_at IS NOT NULL")
|
|
|
|
.where("last_success_at IS NULL OR last_success_at <= DATE_SUB(last_run_at, INTERVAL 3 DAY)")
|
2018-07-04 11:55:44 +02:00
|
|
|
}
|
2018-07-03 20:11:52 +02:00
|
|
|
|
|
|
|
def self.default_sort
|
|
|
|
{ domain_expires_at: :asc }
|
|
|
|
end
|
|
|
|
|
2018-06-04 14:06:37 +02:00
|
|
|
private
|
2018-05-29 13:11:22 +02:00
|
|
|
|
|
|
|
def domain_created_at_past
|
|
|
|
errors.add(:domain_created_at, :past) if domain_created_at.present? && domain_created_at.future?
|
|
|
|
end
|
|
|
|
|
|
|
|
def domain_updated_at_past
|
|
|
|
errors.add(:domain_updated_at, :past) if domain_updated_at.present? && domain_updated_at.future?
|
|
|
|
end
|
2018-05-30 18:16:43 +02:00
|
|
|
|
|
|
|
def enqueue_sync
|
|
|
|
return unless active?
|
|
|
|
return unless saved_changes.key?("domain")
|
|
|
|
|
|
|
|
WhoisSyncJob.perform_later(id) if domain?
|
|
|
|
end
|
2018-06-04 14:06:37 +02:00
|
|
|
|
|
|
|
def reset_notifications
|
|
|
|
return unless (saved_changes.keys & %w[domain domain_expires_at]).present?
|
|
|
|
|
|
|
|
notifications.each(&:reset!)
|
|
|
|
end
|
2018-05-29 10:28:03 +02:00
|
|
|
end
|