blazer.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. def self.time_zone=(time_zone)
  31. @time_zone = time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone[time_zone.to_s]
  32. end
  33. def self.settings
  34. @settings ||= begin
  35. path = Rails.root.join("config", "blazer.yml").to_s
  36. if File.exist?(path)
  37. YAML.load(ERB.new(File.read(path)).result)
  38. else
  39. {}
  40. end
  41. end
  42. end
  43. def self.data_sources
  44. @data_sources ||= begin
  45. ds = Hash[
  46. settings["data_sources"].map do |id, s|
  47. [id, Blazer::DataSource.new(id, s)]
  48. end
  49. ]
  50. ds.default = ds.values.first
  51. ds
  52. end
  53. end
  54. def self.run_checks(schedule: nil)
  55. checks = Blazer::Check.includes(:query)
  56. checks = checks.where(schedule: schedule) if schedule
  57. checks.find_each do |check|
  58. rows = nil
  59. error = nil
  60. tries = 1
  61. ActiveSupport::Notifications.instrument("run_check.blazer", check_id: check.id, query_id: check.query.id, state_was: check.state) do |instrument|
  62. # try 3 times on timeout errors
  63. while tries <= 3
  64. rows, error, cached_at = data_sources[check.query.data_source].run_statement(check.query.statement, refresh_cache: true)
  65. if error == Blazer::TIMEOUT_MESSAGE
  66. Rails.logger.info "[blazer timeout] query=#{check.query.name}"
  67. tries += 1
  68. sleep(10)
  69. else
  70. break
  71. end
  72. end
  73. check.update_state(rows, error)
  74. # TODO use proper logfmt
  75. Rails.logger.info "[blazer check] query=#{check.query.name} state=#{check.state} rows=#{rows.try(:size)} error=#{error}"
  76. instrument[:state] = check.state
  77. instrument[:rows] = rows.try(:size)
  78. instrument[:error] = error
  79. instrument[:tries] = tries
  80. end
  81. end
  82. end
  83. def self.send_failing_checks
  84. emails = {}
  85. Blazer::Check.includes(:query).where(state: %w[failing error]).find_each do |check|
  86. check.split_emails.each do |email|
  87. (emails[email] ||= []) << check
  88. end
  89. end
  90. emails.each do |email, checks|
  91. Blazer::CheckMailer.failing_checks(email, checks).deliver_later
  92. end
  93. end
  94. end