Module: Worker::Killswitch

Defined in:
lib/worker/killswitch.rb,
lib/worker/killswitch/config.rb,
lib/worker/killswitch/version.rb,
lib/worker/killswitch/middleware/server.rb

Defined Under Namespace

Modules: Middleware Classes: Config

Constant Summary collapse

KILLSWITCH_ENABLED_KEY =
"worker_killswitch_enabled".freeze
RETRY_AFTER =
5.seconds
VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.cacheObject



24
25
26
# File 'lib/worker/killswitch.rb', line 24

def cache
  config.cache
end

.configObject



12
13
14
# File 'lib/worker/killswitch.rb', line 12

def config
  config ||= Config.new
end

.configure {|config| ... } ⇒ Object

Yields:



16
17
18
# File 'lib/worker/killswitch.rb', line 16

def configure(&block)
  yield config
end

.disableObject



39
40
41
42
43
44
# 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

.enableObject



32
33
34
35
36
37
# 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?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
# 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

.loggerObject



20
21
22
# File 'lib/worker/killswitch.rb', line 20

def logger
  config.logger
end

.metrics_providerObject



28
29
30
# File 'lib/worker/killswitch.rb', line 28

def metrics_provider
  config.metrics_provider
end

.random_sub_sleepObject

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



60
61
62
# File 'lib/worker/killswitch.rb', line 60

def random_sub_sleep
  Kernel.rand(0.0..1.0)
end

.wait_for_resumeObject



55
56
57
# File 'lib/worker/killswitch.rb', line 55

def wait_for_resume
  Kernel.sleep(RETRY_AFTER + random_sub_sleep)
end