Class: Gitlab::Cluster::LifecycleEvents

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/cluster/lifecycle_events.rb

Overview

LifecycleEvents lets Rails initializers register application startup hooks that are sensitive to forking. For example, to defer the creation of watchdog threads. This lets us abstract away the Unix process lifecycles of Unicorn, Sidekiq, Puma, Puma Cluster, etc.

We have the following lifecycle events.

  • on_before_fork (on master process):

    Unicorn/Puma Cluster: This will be called exactly once,
      on startup, before the workers are forked. This is
      called in the PARENT/MASTER process.
    
    Sidekiq/Puma Single: This is not called.
    
  • on_master_start (on master process):

    Unicorn/Puma Cluster: This will be called exactly once,
      on startup, before the workers are forked. This is
      called in the PARENT/MASTER process.
    
    Sidekiq/Puma Single: This is called immediately.
    
  • on_before_blackout_period (on master process):

    Unicorn/Puma Cluster: This will be called before a blackout
      period when performing graceful shutdown of master.
      This is called on `master` process.
    
    Sidekiq/Puma Single: This is not called.
    
  • on_before_graceful_shutdown (on master process):

    Unicorn/Puma Cluster: This will be called before a graceful
      shutdown  of workers starts happening, but after blackout period.
      This is called on `master` process.
    
    Sidekiq/Puma Single: This is not called.
    
  • on_before_master_restart (on master process):

    Unicorn: This will be called before a new master is spun up.
      This is called on forked master before `execve` to become
      a new masterfor Unicorn. This means that this does not really
      affect old master process.
    
    Puma Cluster: This will be called before a new master is spun up.
      This is called on `master` process.
    
    Sidekiq/Puma Single: This is not called.
    
  • on_worker_start (on worker process):

    Unicorn/Puma Cluster: This is called in the worker process
      exactly once before processing requests.
    
    Sidekiq/Puma Single: This is called immediately.
    

Blocks will be executed in the order in which they are registered.

Class Method Summary collapse

Class Method Details

.do_before_forkObject


117
118
119
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 117

def do_before_fork
  call(@before_fork_hooks)
end

.do_before_graceful_shutdownObject


121
122
123
124
125
126
127
128
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 121

def do_before_graceful_shutdown
  call(@master_blackout_period)

  blackout_seconds = ::Settings.shutdown.blackout_seconds.to_i
  sleep(blackout_seconds) if blackout_seconds > 0

  call(@master_graceful_shutdown)
end

.do_before_master_restartObject Also known as: do_master_restart


130
131
132
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 130

def do_before_master_restart
  call(@master_restart_hooks)
end

.do_worker_startObject

Lifecycle integration methods (called from unicorn.rb, puma.rb, etc.)


113
114
115
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 113

def do_worker_start
  call(@worker_start_hooks)
end

.on_before_blackout_period(&block) ⇒ Object

Read the config/initializers/cluster_events_before_phased_restart.rb


86
87
88
89
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 86

def on_before_blackout_period(&block)
  # Defer block execution
  (@master_blackout_period ||= []) << block
end

.on_before_fork(&block) ⇒ Object


80
81
82
83
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 80

def on_before_fork(&block)
  # Defer block execution
  (@before_fork_hooks ||= []) << block
end

.on_before_graceful_shutdown(&block) ⇒ Object

Read the config/initializers/cluster_events_before_phased_restart.rb


92
93
94
95
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 92

def on_before_graceful_shutdown(&block)
  # Defer block execution
  (@master_graceful_shutdown ||= []) << block
end

.on_before_master_restart(&block) ⇒ Object


97
98
99
100
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 97

def on_before_master_restart(&block)
  # Defer block execution
  (@master_restart_hooks ||= []) << block
end

.on_master_start(&block) ⇒ Object


102
103
104
105
106
107
108
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 102

def on_master_start(&block)
  if in_clustered_environment?
    on_before_fork(&block)
  else
    on_worker_start(&block)
  end
end

.on_worker_start(&block) ⇒ Object

Hook registration methods (called from initializers)


71
72
73
74
75
76
77
78
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 71

def on_worker_start(&block)
  if in_clustered_environment?
    # Defer block execution
    (@worker_start_hooks ||= []) << block
  else
    yield
  end
end

.set_puma_options(options) ⇒ Object

Puma doesn't use singletons (which is good) but this means we need to pass through whether the puma server is running in single mode or cluster mode


140
141
142
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 140

def set_puma_options(options)
  @puma_options = options
end