Class: Check

Inherits:
ApplicationRecord show all
Defined in:
app/models/check.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.percentage_activeObject


16
17
18
19
20
21
22
# File 'app/models/check.rb', line 16

def self.percentage_active
  if ! self.active.empty?
    ((active.count * 1.0 / count * 1.0) * 100.0).round(0)
  else
    0.0
  end
end

.percentage_healthyObject


24
25
26
27
28
29
30
# File 'app/models/check.rb', line 24

def self.percentage_healthy
  if ! self.active.empty?
    ((healthy.count * 1.0 / active.count * 1.0) * 100.0).round(0)
  else
    0.0
  end
end

Instance Method Details

#create_jobObject


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/check.rb', line 32

def create_job
  job =
    Rufus::Scheduler.singleton.cron self.cron, :job => true do
      url = URI.parse(self.url)
      begin
        response = Net::HTTP.get_response(url)
        http_code = response.code
      rescue *NET_HTTP_ERRORS => e
        status = e.to_s
      end
      status = http_code unless e
      last_contact_at = Time.current
      Rails.logger.info "ciao-scheduler Checked '#{url}' at '#{last_contact_at}' and got '#{status}'"
      status_before = status_after = ""
      ActiveRecord::Base.connection_pool.with_connection do
        status_before = self.status
        self.update_columns(status: status, last_contact_at: last_contact_at, next_contact_at: job.next_times(1).first.to_local_time)
        status_after = self.status
      end
      if status_before != status_after
        CheckMailer.with(name: self.name, status_before: status_before, status_after: status_after).change_status_mail.deliver
        Rails.logger.info "ciao-scheduler Sent 'changed_status' notification mail"
      end
    end
  if job
    Rails.logger.info "ciao-scheduler Created job '#{job.id}'"
    self.update_columns(job: job.id, next_contact_at: job.next_times(1).first.to_local_time)
  else
    Rails.logger.error "ciao-scheduler Could not create job"
  end
  return job
end

#unschedule_jobObject


65
66
67
68
69
70
71
72
73
# File 'app/models/check.rb', line 65

def unschedule_job
  job = Rufus::Scheduler.singleton.job(self.job)
  if job
    job.unschedule
    Rails.logger.info "ciao-scheduler Unscheduled job '#{job.id}'"
  else
    Rails.logger.info "ciao-scheduler Could not unschedule job: '#{self.job}' not found"
  end
end