summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Provaznik <jprovaznik@gitlab.com>2019-06-26 17:42:24 +0200
committerJan Provaznik <jprovaznik@gitlab.com>2019-07-01 09:05:31 +0200
commit52223d59515bc16c4c412a26929d2efd76759b4f (patch)
treef8646b865b0dec84cc37559551d480d1adde23a4
parent66313aec761e48f4bf28461b760daa7774025fcf (diff)
downloadgitlab-ce-puma-init-restart.tar.gz
Run before_fork only once on bootpuma-init-restart
This aligns behavior of before_fork with Puma where before_fork is called only on master boot, also none of our code needs to be run before each worker fork, instead we have a use-case for deleting Prometheus files only on boot time.
-rw-r--r--config/initializers/7_prometheus_metrics.rb15
-rw-r--r--config/puma.example.development.rb4
-rw-r--r--config/puma.rb.example4
-rw-r--r--config/unicorn.rb.example15
-rw-r--r--config/unicorn.rb.example.development15
-rw-r--r--lib/gitlab/cluster/lifecycle_events.rb3
6 files changed, 30 insertions, 26 deletions
diff --git a/config/initializers/7_prometheus_metrics.rb b/config/initializers/7_prometheus_metrics.rb
index 68f8487d377..54cdefc2a10 100644
--- a/config/initializers/7_prometheus_metrics.rb
+++ b/config/initializers/7_prometheus_metrics.rb
@@ -43,14 +43,21 @@ if !Rails.env.test? && Gitlab::Metrics.prometheus_metrics_enabled?
end
end
-Gitlab::Cluster::LifecycleEvents.on_master_restart do
+def cleanup_prometheus_multiproc_dir
# The following is necessary to ensure stale Prometheus metrics don't
# accumulate over time. It needs to be done in this hook as opposed to
# inside an init script to ensure metrics files aren't deleted after new
# unicorn workers start after a SIGUSR2 is received.
- prometheus_multiproc_dir = ENV['prometheus_multiproc_dir']
- if prometheus_multiproc_dir
- old_metrics = Dir[File.join(prometheus_multiproc_dir, '*.db')]
+ if dir = ::Prometheus::Client.configuration.multiprocess_files_dir
+ old_metrics = Dir[File.join(dir, '*.db')]
FileUtils.rm_rf(old_metrics)
end
end
+
+Gitlab::Cluster::LifecycleEvents.on_master_start do
+ cleanup_prometheus_multiproc_dir
+end
+
+Gitlab::Cluster::LifecycleEvents.on_master_restart do
+ cleanup_prometheus_multiproc_dir
+end
diff --git a/config/puma.example.development.rb b/config/puma.example.development.rb
index 7cb9fb01b25..9df24bf74e3 100644
--- a/config/puma.example.development.rb
+++ b/config/puma.example.development.rb
@@ -49,10 +49,6 @@ on_restart do
end
before_fork do
- # We want to clean things up (e.g. metric files) at first boot. So make sure
- # Puma calls the same callback at boot as Unicorn.
- Gitlab::Cluster::LifecycleEvents.do_master_restart
-
# Signal to the puma killer
Gitlab::Cluster::PumaWorkerKillerInitializer.start @config.options unless ENV['DISABLE_PUMA_WORKER_KILLER']
diff --git a/config/puma.rb.example b/config/puma.rb.example
index 731c2563150..6558dbc6cfe 100644
--- a/config/puma.rb.example
+++ b/config/puma.rb.example
@@ -44,10 +44,6 @@ on_restart do
end
before_fork do
- # We want to clean things up (e.g. metric files) at first boot. So make sure
- # Puma calls the same callback at boot as Unicorn.
- Gitlab::Cluster::LifecycleEvents.do_master_restart
-
# Signal to the puma killer
Gitlab::Cluster::PumaWorkerKillerInitializer.start @config.options unless ENV['DISABLE_PUMA_WORKER_KILLER']
diff --git a/config/unicorn.rb.example b/config/unicorn.rb.example
index b4da1568511..581fde84c95 100644
--- a/config/unicorn.rb.example
+++ b/config/unicorn.rb.example
@@ -92,17 +92,18 @@ run_once = true
before_fork do |server, worker|
if run_once
+ # There is a difference between Puma and Unicorn:
+ # - Puma calls before_fork once when booting up master process
+ # - Unicorn runs before_fork whenever new work is spawned
+ # To unify this behavior we call before_fork only once (we use
+ # this callback for deleting Prometheus files so for our purposes
+ # it makes sense to align behavior with Puma)
run_once = false
- # The Unicorn before_exec hook only gets called on a Unicorn
- # restart, but we want to clean things up (e.g. metric files) at
- # first boot.
- Gitlab::Cluster::LifecycleEvents.do_master_restart
+ # Signal application hooks that we're about to fork
+ Gitlab::Cluster::LifecycleEvents.do_before_fork
end
- # Signal application hooks that we're about to fork
- Gitlab::Cluster::LifecycleEvents.do_before_fork
-
# The following is only recommended for memory/DB-constrained
# installations. It is not needed if your system can house
# twice as many worker_processes as you have configured.
diff --git a/config/unicorn.rb.example.development b/config/unicorn.rb.example.development
index 1ff55701c14..9a02d5f1007 100644
--- a/config/unicorn.rb.example.development
+++ b/config/unicorn.rb.example.development
@@ -25,17 +25,18 @@ run_once = true
before_fork do |server, worker|
if run_once
+ # There is a difference between Puma and Unicorn:
+ # - Puma calls before_fork once when booting up master process
+ # - Unicorn runs before_fork whenever new work is spawned
+ # To unify this behavior we call before_fork only once (we use
+ # this callback for deleting Prometheus files so for our purposes
+ # it makes sense to align behavior with Puma)
run_once = false
- # The Unicorn before_exec hook only gets called on a Unicorn
- # restart, but we want to clean things up (e.g. metric files) at
- # first boot.
- Gitlab::Cluster::LifecycleEvents.do_master_restart
+ # Signal application hooks that we're about to fork
+ Gitlab::Cluster::LifecycleEvents.do_before_fork
end
- # Signal application hooks that we're about to fork
- Gitlab::Cluster::LifecycleEvents.do_before_fork
-
# The following is only recommended for memory/DB-constrained
# installations. It is not needed if your system can house
# twice as many worker_processes as you have configured.
diff --git a/lib/gitlab/cluster/lifecycle_events.rb b/lib/gitlab/cluster/lifecycle_events.rb
index e0f9eb59924..8f796748199 100644
--- a/lib/gitlab/cluster/lifecycle_events.rb
+++ b/lib/gitlab/cluster/lifecycle_events.rb
@@ -11,6 +11,9 @@ module Gitlab
# We have three lifecycle events.
#
# - before_fork (only in forking processes)
+ # In forking processes (Unicorn and Puma in multiprocess mode) this
+ # will be called exactly once, on startup, before the workers are
+ # forked. This will be called in the parent process.
# - worker_start
# - before_master_restart (only in forking processes)
#