Class: Aikido::Zen::Middleware::ForkDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/aikido/zen/middleware/fork_detector.rb

Overview

This middleware is responsible for detecting when a process has forked (e.g., in a Puma or Unicorn worker) and resetting the state of the Aikido Zen agent. It should be inserted early in the middleware stack.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ForkDetector

Returns a new instance of ForkDetector.



10
11
12
13
14
# File 'lib/aikido/zen/middleware/fork_detector.rb', line 10

def initialize(app)
  @app = app

  @pid = Concurrent::AtomicFixnum.new(Process.pid)
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/aikido/zen/middleware/fork_detector.rb', line 16

def call(env)
  new_pid = Process.pid
  old_pid = @pid.value

  if new_pid != old_pid && @pid.compare_and_set(old_pid, new_pid)
    Aikido::Zen.fork!
  end

  @app.call(env)
end