blazer.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. require "csv"
  2. require "yaml"
  3. require "chartkick"
  4. require "blazer/version"
  5. require "blazer/data_source"
  6. require "blazer/engine"
  7. module Blazer
  8. class Error < StandardError; end
  9. class TimeoutNotSupported < Error; end
  10. class << self
  11. attr_accessor :audit
  12. attr_reader :time_zone
  13. attr_accessor :user_name
  14. attr_accessor :user_class
  15. attr_accessor :user_method
  16. attr_accessor :before_action
  17. attr_accessor :from_email
  18. attr_accessor :cache
  19. attr_accessor :transform_statement
  20. attr_accessor :check_schedules
  21. end
  22. self.audit = true
  23. self.user_name = :name
  24. self.check_schedules = ["5 minutes", "1 hour", "1 day"]
  25. TIMEOUT_MESSAGE = "Query timed out :("
  26. TIMEOUT_ERRORS = [
  27. "canceling statement due to statement timeout", # postgres
  28. "cancelled on user's request", # redshift
  29. "system requested abort" # redshift
  30. ]
  31. BELONGS_TO_OPTIONAL = {}
  32. BELONGS_TO_OPTIONAL[:optional] = true if Rails::VERSION::MAJOR >= 5
  33. def self.time_zone=(time_zone)
  34. @time_zone = time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone[time_zone.to_s]
  35. end
  36. def self.settings
  37. @settings ||= begin
  38. path = Rails.root.join("config", "blazer.yml").to_s
  39. if File.exist?(path)
  40. YAML.load(ERB.new(File.read(path)).result)
  41. else
  42. {}
  43. end
  44. end
  45. end
  46. def self.data_sources
  47. @data_sources ||= begin
  48. ds = Hash[
  49. settings["data_sources"].map do |id, s|
  50. [id, Blazer::DataSource.new(id, s)]
  51. end
  52. ]
  53. ds.default = ds.values.first
  54. ds
  55. end
  56. end
  57. def self.run_checks(schedule: nil)
  58. checks = Blazer::Check.includes(:query)
  59. checks = checks.where(schedule: schedule) if schedule
  60. checks.find_each do |check|
  61. rows = nil
  62. error = nil
  63. tries = 1
  64. ActiveSupport::Notifications.instrument("run_check.blazer", check_id: check.id, query_id: check.query.id, state_was: check.state) do |instrument|
  65. # try 3 times on timeout errors
  66. while tries <= 3
  67. columns, rows, error, cached_at = data_sources[check.query.data_source].run_statement(check.query.statement, refresh_cache: true)
  68. if error == Blazer::TIMEOUT_MESSAGE
  69. Rails.logger.info "[blazer timeout] query=#{check.query.name}"
  70. tries += 1
  71. sleep(10)
  72. elsif error.to_s.start_with?("PG::ConnectionBad")
  73. data_sources[check.query.data_source].reconnect
  74. Rails.logger.info "[blazer reconnect] query=#{check.query.name}"
  75. tries += 1
  76. sleep(10)
  77. else
  78. break
  79. end
  80. end
  81. check.update_state(rows, error)
  82. # TODO use proper logfmt
  83. Rails.logger.info "[blazer check] query=#{check.query.name} state=#{check.state} rows=#{rows.try(:size)} error=#{error}"
  84. instrument[:state] = check.state
  85. instrument[:rows] = rows.try(:size)
  86. instrument[:error] = error
  87. instrument[:tries] = tries
  88. end
  89. end
  90. end
  91. def self.send_failing_checks
  92. emails = {}
  93. Blazer::Check.includes(:query).where(state: %w[failing error]).find_each do |check|
  94. check.split_emails.each do |email|
  95. (emails[email] ||= []) << check
  96. end
  97. end
  98. emails.each do |email, checks|
  99. Blazer::CheckMailer.failing_checks(email, checks).deliver_later
  100. end
  101. end
  102. end