Skip to content

Instantly share code, notes, and snippets.

@pdx91
Created September 28, 2023 18:00
Show Gist options
  • Select an option

  • Save pdx91/32fd0c17e911e69c38b9fe5c137a935e to your computer and use it in GitHub Desktop.

Select an option

Save pdx91/32fd0c17e911e69c38b9fe5c137a935e to your computer and use it in GitHub Desktop.
Sync jobs with Healthchecks.io
module Balance
module Monitor
module Uptime
module Sync
def self.now!
sync_new_jobs
remove_unused_jobs
end
def self.sync_new_jobs
valid = true
uptime_jobs.each do |job|
uptime_job_schedules.each do |_schedule_name, schedule|
next unless schedule["class"] == job
Balance::Monitor.log "Starting to register #{job} with https://healthchecks.io for uptime monitoring."
response = Clients::HealthchecksIo.upsert_check(
name: job,
schedule: schedule["cron"],
grace: job.constantize.uptime_grace_period.to_i
)
Balance::Monitor.log "Finished registering #{job} with https://healthchecks.io for uptime monitoring with response code <#{response&.code}>."
valid &= [200, 201].include?(response&.code&.to_i)
end
end
valid
end
def self.remove_unused_jobs
valid = true
existing_checks.each do |existing_check|
next if uptime_jobs.include?(existing_check["name"])
Balance::Monitor.log "Starting to remove #{existing_check["name"]} from https://healthchecks.io for uptime monitoring."
uuid = get_uuid(existing_check["name"])
next if uuid.blank?
response = Clients::HealthchecksIo.delete_check uuid
Balance::Monitor.log "Finished removing #{existing_check["name"]} from https://healthchecks.io for uptime monitoring with response code <#{response&.code}>."
valid &= [200, 404].include?(response&.code&.to_i)
end
end
def self.existing_checks
@@existing_checks ||= Clients::HealthchecksIo.checks
end
def self.uptime_jobs
@@uptime_jobs ||=
begin
Rails.application.eager_load!
ApplicationJob.descendants.select { |job| job.included_modules.include?(UptimeMonitorable) }.map(&:to_s)
end
end
def self.uptime_job_schedules
@@uptime_job_schedules ||= YAML.load_file(Rails.root.join("config/sidekiq.yml")).dig(:scheduler, :schedule)
end
def self.get_uuid(name)
ping_url = existing_checks.find { |check| check["name"] == name }&.dig("ping_url")
return if ping_url.blank?
ping_url.split("/").last
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment