Module: Webhookdb::Async::Autoscaler

Includes:
Appydays::Configurable, Appydays::Loggable
Defined in:
lib/webhookdb/async/autoscaler.rb

Constant Summary collapse

AVAILABLE_PROVIDERS =
["heroku"].freeze

Class Method Summary collapse

Class Method Details

._check_provider!Object



14
15
16
17
18
# File 'lib/webhookdb/async/autoscaler.rb', line 14

def self._check_provider!
  return if AVAILABLE_PROVIDERS.include?(self.provider)
  return if !self.enabled && self.provider.blank?
  raise "invalid AUTOSCALER_PROVIDER: '#{self.provider}', one of: #{AVAILABLE_PROVIDERS.join(', ')}"
end

.enabled?Boolean

Returns:

  • (Boolean)


36
# File 'lib/webhookdb/async/autoscaler.rb', line 36

def enabled? = self.enabled

.scale_down(depth:, duration:) ⇒ Object



79
80
81
82
# File 'lib/webhookdb/async/autoscaler.rb', line 79

def scale_down(depth:, duration:, **)
  scale_action = @impl.scale_down(depth:, duration:, **)
  self.logger.warn("high_latency_queues_resolved", depth:, duration:, scale_action:)
end

.scale_up(names_and_latencies, depth:, duration:) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/webhookdb/async/autoscaler.rb', line 69

def scale_up(names_and_latencies, depth:, duration:, **)
  scale_action = @impl.scale_up(names_and_latencies, depth:, duration:, **)
  kw = {queues: names_and_latencies, depth:, duration:, scale_action:}
  self.logger.warn("high_latency_queues_event", **kw)
  Sentry.with_scope do |scope|
    scope&.set_extras(**kw)
    Sentry.capture_message("Some queues have a high latency")
  end
end

.startObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/webhookdb/async/autoscaler.rb', line 38

def start
  raise "already started" unless @instance.nil?
  case self.provider
    when "heroku"
      opts = {heroku: Webhookdb::Heroku.client, max_additional_workers: self.max_additional_workers}
      (opts[:app_id_or_app_name] = self.heroku_app_id_or_app_name) if
        self.heroku_app_id_or_app_name
      (opts[:formation_id_or_formation_type] = self.heroku_formation_id_or_formation_type) if
        self.heroku_formation_id_or_formation_type
      @impl = Amigo::Autoscaler::Heroku.new(**opts)
    else
      self._check_provider!
  end
  @instance = Amigo::Autoscaler.new(
    poll_interval: self.poll_interval,
    latency_threshold: self.latency_threshold,
    hostname_regex: self.hostname_regex,
    handlers: [self.method(:scale_up)],
    alert_interval: self.alert_interval,
    latency_restored_threshold: self.latency_restored_threshold,
    latency_restored_handlers: [self.method(:scale_down)],
    log: ->(level, msg, kw={}) { self.logger.send(level, msg, kw) },
  )
  return @instance.start
end

.stopObject



64
65
66
67
# File 'lib/webhookdb/async/autoscaler.rb', line 64

def stop
  raise "not started" if @instance.nil?
  @instance.stop
end