Class: Gitlab::Cluster::LifecycleEvents
- Inherits:
-
Object
- Object
- Gitlab::Cluster::LifecycleEvents
- 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 Sidekiq, Puma, Puma Cluster, etc.
We have the following lifecycle events.
-
on_before_fork (on master process):
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):
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):
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):
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):
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):
Puma Cluster: This is called in the worker process exactly once before processing requests. Sidekiq/Puma Single: This is called immediately.
-
on_worker_stop (on worker process):
Puma Cluster: Called in the worker process exactly once after it stops processing requests but before it shuts down. Sidekiq: Called after the scheduler shuts down but before the worker finishes ongoing jobs.
Blocks will be executed in the order in which they are registered.
Constant Summary collapse
- FatalError =
rubocop:disable Lint/InheritException
Class.new(Exception)
- USE_FATAL_LIFECYCLE_EVENTS =
Gitlab::Utils.to_boolean(ENV.fetch('GITLAB_FATAL_LIFECYCLE_EVENTS', 'true'))
Class Method Summary collapse
- .do_before_fork ⇒ Object
- .do_before_graceful_shutdown ⇒ Object
- .do_before_master_restart ⇒ Object (also: do_master_restart)
-
.do_worker_start ⇒ Object
Lifecycle integration methods (called from puma.rb, etc.).
- .do_worker_stop ⇒ Object
-
.on_before_blackout_period(&block) ⇒ Object
Read the config/initializers/cluster_events_before_phased_restart.rb.
- .on_before_fork(&block) ⇒ Object
-
.on_before_graceful_shutdown(&block) ⇒ Object
Read the config/initializers/cluster_events_before_phased_restart.rb.
- .on_before_master_restart(&block) ⇒ Object
- .on_master_start(&block) ⇒ Object
-
.on_worker_start(&block) ⇒ Object
Hook registration methods (called from initializers).
- .on_worker_stop(&block) ⇒ Object
-
.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.
Class Method Details
.do_before_fork ⇒ Object
136 137 138 |
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 136 def do_before_fork call(:before_fork_hooks, @before_fork_hooks) end |
.do_before_graceful_shutdown ⇒ Object
140 141 142 143 144 145 146 147 |
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 140 def do_before_graceful_shutdown call(:master_blackout_period, @master_blackout_period) blackout_seconds = ::Settings.shutdown.blackout_seconds.to_i sleep(blackout_seconds) if blackout_seconds > 0 call(:master_graceful_shutdown, @master_graceful_shutdown) end |
.do_before_master_restart ⇒ Object Also known as: do_master_restart
149 150 151 |
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 149 def do_before_master_restart call(:master_restart_hooks, @master_restart_hooks) end |
.do_worker_start ⇒ Object
Lifecycle integration methods (called from puma.rb, etc.)
132 133 134 |
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 132 def do_worker_start call(:worker_start_hooks, @worker_start_hooks) end |
.do_worker_stop ⇒ Object
153 154 155 |
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 153 def do_worker_stop call(:worker_stop_hooks, @worker_stop_hooks) end |
.on_before_blackout_period(&block) ⇒ Object
Read the config/initializers/cluster_events_before_phased_restart.rb
101 102 103 104 |
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 101 def on_before_blackout_period(&block) # Defer block execution (@master_blackout_period ||= []) << block end |
.on_before_fork(&block) ⇒ Object
95 96 97 98 |
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 95 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
107 108 109 110 |
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 107 def on_before_graceful_shutdown(&block) # Defer block execution (@master_graceful_shutdown ||= []) << block end |
.on_before_master_restart(&block) ⇒ Object
112 113 114 115 |
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 112 def on_before_master_restart(&block) # Defer block execution (@master_restart_hooks ||= []) << block end |
.on_master_start(&block) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 117 def on_master_start(&block) if in_clustered_puma? on_before_fork(&block) else on_worker_start(&block) end end |
.on_worker_start(&block) ⇒ Object
Hook registration methods (called from initializers)
86 87 88 89 90 91 92 93 |
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 86 def on_worker_start(&block) if in_clustered_puma? # Defer block execution (@worker_start_hooks ||= []) << block else yield end end |
.on_worker_stop(&block) ⇒ Object
125 126 127 |
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 125 def on_worker_stop(&block) (@worker_stop_hooks ||= []) << block 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
163 164 165 |
# File 'lib/gitlab/cluster/lifecycle_events.rb', line 163 def () @puma_options = end |