Class: GrpcForkSafety::LifeCycle

Inherits:
Object
  • Object
show all
Defined in:
lib/grpc_fork_safety/no_patch.rb

Instance Method Summary collapse

Constructor Details

#initialize(grpc = ::GRPC, process = ::Process) ⇒ LifeCycle

Returns a new instance of LifeCycle.



9
10
11
12
13
14
15
16
# File 'lib/grpc_fork_safety/no_patch.rb', line 9

def initialize(grpc = ::GRPC, process = ::Process)
  @grpc = grpc
  @process = process
  @shutdown_by = nil
  @keep_disabled = false
  @before_disable = []
  @after_enable = []
end

Instance Method Details

#after_enable(&block) ⇒ Object

Raises:



24
25
26
27
28
# File 'lib/grpc_fork_safety/no_patch.rb', line 24

def after_enable(&block)
  raise Error, "No block given" unless block_given?

  @after_enable << block
end

#after_forkObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/grpc_fork_safety/no_patch.rb', line 53

def after_fork
  if @shutdown_by.nil?
    # noop
  elsif @shutdown_by == @process.pid # In parent
    unless @keep_disabled
      @grpc.postfork_parent
      @shutdown_by = nil

      @after_enable.each do |cb|
        cb.call(false)
      end
    end
  else
    @keep_disabled = false
    @shutdown_by = nil
    @grpc.postfork_child
    @after_enable.each do |cb|
      cb.call(true)
    end
  end
end

#before_disable(&block) ⇒ Object

Raises:



18
19
20
21
22
# File 'lib/grpc_fork_safety/no_patch.rb', line 18

def before_disable(&block)
  raise Error, "No block given" unless block_given?

  @before_disable << block
end

#before_forkObject



44
45
46
47
48
49
50
51
# File 'lib/grpc_fork_safety/no_patch.rb', line 44

def before_fork
  return if @shutdown_by

  @before_disable.each(&:call)

  @grpc.prefork
  @shutdown_by = @process.pid
end

#keep_disabled!Object



30
31
32
33
# File 'lib/grpc_fork_safety/no_patch.rb', line 30

def keep_disabled!
  @keep_disabled = true
  before_fork
end

#keep_disabled?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/grpc_fork_safety/no_patch.rb', line 35

def keep_disabled?
  @keep_disabled
end

#reenable!Object



39
40
41
42
# File 'lib/grpc_fork_safety/no_patch.rb', line 39

def reenable!
  @keep_disabled = false
  after_fork
end