blazer.rb 3.7 KB

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