blazer.rb 3.3 KB

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