Class: Gritz::ForkGuard

Inherits:
Object
  • Object
show all
Defined in:
/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/fork_guard.rb

Overview

Detects transport resource constructors in the master before workers fork.

Defined Under Namespace

Classes: Violation

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode:, logger:, master_pid:) ⇒ ForkGuard

Returns a new instance of ForkGuard.

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/fork_guard.rb', line 63

def initialize(mode:, logger:, master_pid:)
  raise ArgumentError, "fork guard mode must be :raise, :warn, :off, or :record" unless %i[raise warn off record].include?(mode)

  @mode = mode
  @logger = logger
  @master_pid = master_pid
  @violations = []
end

Class Attribute Details

.current ⇒ Object (readonly)



30
31
32
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/fork_guard.rb', line 30

def current
  @current
end

Instance Attribute Details

#logger ⇒ Object (readonly)



27
28
29
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/fork_guard.rb', line 27

def logger
  @logger
end

#master_pid ⇒ Object (readonly)



27
28
29
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/fork_guard.rb', line 27

def master_pid
  @master_pid
end

#mode ⇒ Object (readonly)



27
28
29
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/fork_guard.rb', line 27

def mode
  @mode
end

#violations ⇒ Object (readonly)



27
28
29
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/fork_guard.rb', line 27

def violations
  @violations
end

Class Method Details

.activate(mode: :raise, logger: nil, master_pid: Process.pid) ⇒ Object



32
33
34
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/fork_guard.rb', line 32

def activate(mode: :raise, logger: nil, master_pid: Process.pid)
  @current = new(mode:, logger:, master_pid:)
end

.check!(klass) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/fork_guard.rb', line 48

def check!(klass)
  guard = @current
  return unless guard && guard.mode != :off && guard.master?

  locations = caller_locations(1, 8).reject { |location| location.path == __FILE__ }
  violation = Violation.new(klass, locations)
  guard.violations << violation
  case guard.mode
  when :raise then raise violation
  when :warn
    guard.logger ? guard.logger.warn(violation.message) : Kernel.warn(violation.message)
  end
end

.deactivate ⇒ Object



36
37
38
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/fork_guard.rb', line 36

def deactivate
  @current = nil
end

.install(klass) ⇒ Object



40
41
42
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/fork_guard.rb', line 40

def install(klass)
  klass.singleton_class.prepend(ConstructorHook) unless klass.singleton_class.ancestors.include?(ConstructorHook)
end

.master? ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/fork_guard.rb', line 44

def master?
  @current&.master? || false
end

Instance Method Details

#master? ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/fork_guard.rb', line 72

def master?
  Process.pid == @master_pid
end