Module: Ductwork::FaultInjection

Defined in:
lib/ductwork/fault_injection.rb

Constant Summary collapse

ENV_KEY =
"DUCTWORK_FAULT"

Class Method Summary collapse

Class Method Details

.checkpoint(key) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ductwork/fault_injection.rb', line 8

def checkpoint(key)
  return if !ENV.key?(ENV_KEY)

  spec = ENV.fetch(ENV_KEY, "").split(":")

  return if spec[0] != key.to_s

  warn "FAULT FIRING: #{spec[0]} -> #{spec[1]} (pid=#{::Process.pid})"

  case spec[1]
  when "kill" then ::Process.kill(:KILL, ::Process.pid)
  when "raise" then raise "FaultInjection: #{key}"
  when "sleep" then sleep(1)
  when "exit" then exit!(1)
  end
end

.with(key, action) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/ductwork/fault_injection.rb', line 25

def with(key, action)
  previous_value = ENV.fetch(ENV_KEY, nil)
  ENV[ENV_KEY] = "#{key}:#{action}"

  yield
ensure
  ENV[ENV_KEY] = previous_value
end