module Worker::Killswitch

Constants

KILLSWITCH_ENABLED_KEY
RETRY_AFTER
VERSION

Public Class Methods

cache() click to toggle source
# File lib/worker/killswitch.rb, line 24
def cache
  config.cache
end
config() click to toggle source
# File lib/worker/killswitch.rb, line 12
def config
  config ||= Config.new
end
configure() { |config| ... } click to toggle source
# File lib/worker/killswitch.rb, line 16
def configure(&block)
  yield config
end
disable() click to toggle source
# File lib/worker/killswitch.rb, line 39
def disable
  metrics_provider&.count("Killswitch::Toggle.disabled", 1)
  metrics_provider&.event("workers_killswitch_disabled", "Worker Killswitch Disabled")

  cache.write(KILLSWITCH_ENABLED_KEY, false)
end
enable() click to toggle source
# File lib/worker/killswitch.rb, line 32
def enable
  metrics_provider&.count("Killswitch::Toggle.enabled", 1)
  metrics_provider&.event("workers_killswitch_enabled", "Worker Killswitch Enabled")

  cache.write(KILLSWITCH_ENABLED_KEY, true)
end
enabled?() click to toggle source
# File lib/worker/killswitch.rb, line 46
def enabled?
  status = cache.fetch(KILLSWITCH_ENABLED_KEY)
  status ? status : false
rescue StandardError => e
  # If there are any cache connectivity issues, the switch fails open.
  metrics_provider&.increment("Killswitch::Toggle.cache_failure", tags: { exception: e.class.to_s })
  false
end
logger() click to toggle source
# File lib/worker/killswitch.rb, line 20
def logger
  config.logger
end
metrics_provider() click to toggle source
# File lib/worker/killswitch.rb, line 28
def metrics_provider
  config.metrics_provider
end
random_sub_sleep() click to toggle source

So that we aren’t having all workers polling at exactly the same time

# File lib/worker/killswitch.rb, line 60
def random_sub_sleep
  Kernel.rand(0.0..1.0)
end
wait_for_resume() click to toggle source
# File lib/worker/killswitch.rb, line 55
def wait_for_resume
  Kernel.sleep(RETRY_AFTER + random_sub_sleep)
end