Class: Sidekiq::HeartbeatMonitor::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/heartbeat_monitor/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_queue_size: nil, on_backed_up: nil, on_slowed_down: nil, dont_repeat_for: nil, slack_notifier_url: nil, max_heartbeat_delay: nil) ⇒ Config

Returns a new instance of Config.

Parameters:

  • max_queue_size (Integer) (defaults to: nil)

    The maximum queue size (default: 5000 or SIDEKIQ_MONITOR_MAX_QUEUE_SIZE environment value)

  • on_backed_up (Proc|Array<Proc>) (defaults to: nil)

    On backed up run this or these procs

  • on_slowed_down (Proc|Array<Proc>) (defaults to: nil)

    On slowed down run this or these procs

  • dont_repeat_for (Integer) (defaults to: nil)

    The don’t repeat for (optional, default: 5.minutes)

  • slack_notifier_url (String) (defaults to: nil)

    The slack notifier url (optional)

  • max_heartbeat_delay (Integer) (defaults to: nil)

    The maximum heartbeat delay (default: 5 minute, max: 5 days)



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sidekiq/heartbeat_monitor/config.rb', line 14

def initialize(max_queue_size: nil, on_backed_up: nil, on_slowed_down: nil, dont_repeat_for: nil, slack_notifier_url: nil, max_heartbeat_delay: nil)
  @max_queue_size = max_queue_size ||  ENV.fetch('SIDEKIQ_MONITOR_MAX_QUEUE_SIZE', 5000).to_i

  @dont_repeat_for = dont_repeat_for unless dont_repeat_for.nil?

  @max_heartbeat_delay = max_heartbeat_delay || 5.minutes

  @on_backed_up = (on_backed_up.is_a?(Enumerable) ? on_backed_up : [on_backed_up]) unless on_backed_up.nil?
  @on_slowed_down = (on_slowed_down.is_a?(Enumerable) ? on_slowed_down : [on_slowed_down]) unless on_slowed_down.nil?

  setup_slack_notifier!(slack_notifier_url) if slack_notifier_url.present?

  install_cron_job!
end

Instance Attribute Details

#max_heartbeat_delayObject

Returns the value of attribute max_heartbeat_delay.



5
6
7
# File 'lib/sidekiq/heartbeat_monitor/config.rb', line 5

def max_heartbeat_delay
  @max_heartbeat_delay
end

#max_queue_sizeObject

Returns the value of attribute max_queue_size.



5
6
7
# File 'lib/sidekiq/heartbeat_monitor/config.rb', line 5

def max_queue_size
  @max_queue_size
end

Class Method Details

.install_cron_job!(output: false) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sidekiq/heartbeat_monitor/config.rb', line 65

def self.install_cron_job!(output: false)
  job = Sidekiq::Cron::Job.find("sidekiq_monitor")

  target_job = Sidekiq::Cron::Job.new(
    name: 'sidekiq_monitor', 
    cron: '* * * * *', 
    klass: Sidekiq::HeartbeatMonitor::Scheduler
  )

  if job.present?
    if job.cron != target_job.cron && job.klass.to_s != target_job.klass
      unless job.destroy
        puts "ERROR: An existing cron job was found with the same name but incorrect configuration. An attempt to delete it (to create a new, correctly configured one) failed." if output
        return false
      end

      if target_job.save
        puts "SUCCESS: An existing crob job was found that had the same name but had outdated configuration, so it was deleted and a new one was installed successfully." if output
        true
      else
        puts "ERROR: Sidekiq heartbeat monitor found an existing cron job with the same name but it is configured incorrectly. It was deleted, but a new one could not be created. Run this command again to try adding it manually again and please ensure you can programmatically add sidekiq cron jobs as well." if output
        false
      end
    else
      puts "SUCCESS: Sidekiq heartbeat monitor cron job already exists and appears to be configured properly so nothing was changed."  if output
      true
    end
  else
    if target_job.save
      puts "SUCCESS: New cron task was installed successfully." if output
      true
    else
      puts "ERROR: New cron task could not be saved for some reason. Please ensure you can programmatically add sidekiq cron jobs and try again." if output
      false
    end
  end
end

Instance Method Details

#send_backed_up_alert!(message, q) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sidekiq/heartbeat_monitor/config.rb', line 35

def send_backed_up_alert!(message, q)
  if @on_backed_up.blank?
    puts ("WARNING: No 'on_backed_up' callback defined for sidekiq-heartbeat_monitor but one of the queues are backed up: #{message}")
    return
  end

  if @dont_repeat_for.nil?
    @on_backed_up.to_a.each { |alert| alert.call(message, q) }
  else
    DontRepeatFor.new(@dont_repeat_for, "Sidekiq/HeartbeatMonitor/#{q.name}/send_backed_up_alert") do
      @on_backed_up.to_a.each { |alert| alert.call(message, q) }
    end
  end
end

#send_slowed_down_alert!(message, q) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sidekiq/heartbeat_monitor/config.rb', line 50

def send_slowed_down_alert!(message, q)
  if @on_slowed_down.blank?
    puts ("WARNING: No 'on_slowed_down' callback defined for sidekiq-heartbeat_monitor but one of the queues are backed up: #{message}")
    return
  end

  if @dont_repeat_for.nil?
    @on_slowed_down.to_a.each { |alert| alert.call(message, q) }
  else
    DontRepeatFor.new(@dont_repeat_for, "Sidekiq/HeartbeatMonitor/#{q.name}/send_slowed_down_alert") do
      @on_slowed_down.to_a.each { |alert| alert.call(message, q) }
    end
  end
end

#send_test!Object



29
30
31
32
33
# File 'lib/sidekiq/heartbeat_monitor/config.rb', line 29

def send_test!
  test_queue = Sidekiq::Queue.new('test')
  send_backed_up_alert("Test backed up alert!", test_queue)
  send_slowed_down_alert("Test slowed down alert!", test_queue)
end