diff options
author | Kamil Trzciński <ayufan@ayufan.eu> | 2019-07-01 15:32:20 +0000 |
---|---|---|
committer | Kamil Trzciński <ayufan@ayufan.eu> | 2019-07-01 15:32:20 +0000 |
commit | ac2971a2079b317c6004e1f533e35de9cf3e032b (patch) | |
tree | c1a6dd029360ea2d8c2dd3225ce26beccaf6caf5 | |
parent | 59963779362b00723d79ae6190689ad0fccf729e (diff) | |
parent | 606261e8e7d6d7879a2b88c3c217df08b063c9fc (diff) | |
download | gitlab-ce-ac2971a2079b317c6004e1f533e35de9cf3e032b.tar.gz |
Merge branch 'puma-init-restart' into 'master'
Fix cleanup of prometheus files on server start
Closes #62862
See merge request gitlab-org/gitlab-ce!29788
-rw-r--r-- | config/initializers/7_prometheus_metrics.rb | 15 | ||||
-rw-r--r-- | config/unicorn.rb.example | 16 | ||||
-rw-r--r-- | config/unicorn.rb.example.development | 16 | ||||
-rw-r--r-- | lib/gitlab/cluster/lifecycle_events.rb | 3 |
4 files changed, 42 insertions, 8 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/unicorn.rb.example b/config/unicorn.rb.example index 4637eb8bc6e..581fde84c95 100644 --- a/config/unicorn.rb.example +++ b/config/unicorn.rb.example @@ -88,9 +88,21 @@ before_exec do |server| Gitlab::Cluster::LifecycleEvents.do_master_restart end +run_once = true + before_fork do |server, worker| - # Signal application hooks that we're about to fork - Gitlab::Cluster::LifecycleEvents.do_before_fork + 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 + + # Signal application hooks that we're about to fork + Gitlab::Cluster::LifecycleEvents.do_before_fork + end # The following is only recommended for memory/DB-constrained # installations. It is not needed if your system can house diff --git a/config/unicorn.rb.example.development b/config/unicorn.rb.example.development index ae3dc2e37e1..9a02d5f1007 100644 --- a/config/unicorn.rb.example.development +++ b/config/unicorn.rb.example.development @@ -21,9 +21,21 @@ before_exec do |server| Gitlab::Cluster::LifecycleEvents.do_master_restart end +run_once = true + before_fork do |server, worker| - # Signal application hooks that we're about to fork - Gitlab::Cluster::LifecycleEvents.do_before_fork + 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 + + # Signal application hooks that we're about to fork + Gitlab::Cluster::LifecycleEvents.do_before_fork + end # The following is only recommended for memory/DB-constrained # installations. It is not needed if your system can house 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) # |