Class: GrpcForkSafety::LifeCycle
- Inherits:
-
Object
- Object
- GrpcForkSafety::LifeCycle
- Defined in:
- lib/grpc_fork_safety/no_patch.rb
Instance Method Summary collapse
- #after_enable(&block) ⇒ Object
- #after_fork ⇒ Object
- #before_disable(&block) ⇒ Object
- #before_fork ⇒ Object
-
#initialize(grpc = ::GRPC, process = ::Process) ⇒ LifeCycle
constructor
A new instance of LifeCycle.
- #keep_disabled! ⇒ Object
- #keep_disabled? ⇒ Boolean
- #reenable! ⇒ Object
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
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_fork ⇒ Object
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
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_fork ⇒ Object
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
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 |