Commit 606261e8 authored by Jan Provaznik's avatar Jan Provaznik Committed by Kamil Trzciński

Run do_master_restart callback on server start

We want to call master_restart callback on start too - this callback
is used for cleaning up prometheus files. It has been added to
Omnibus already:
https://gitlab.com/gitlab-org/omnibus-gitlab/merge_requests/3350

So make sure that Unicorn example configs are up-to-date and that
the same callback is called for Puma too.
parent 59963779
...@@ -43,14 +43,21 @@ if !Rails.env.test? && Gitlab::Metrics.prometheus_metrics_enabled? ...@@ -43,14 +43,21 @@ if !Rails.env.test? && Gitlab::Metrics.prometheus_metrics_enabled?
end end
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 # 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 # 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 # inside an init script to ensure metrics files aren't deleted after new
# unicorn workers start after a SIGUSR2 is received. # unicorn workers start after a SIGUSR2 is received.
prometheus_multiproc_dir = ENV['prometheus_multiproc_dir'] if dir = ::Prometheus::Client.configuration.multiprocess_files_dir
if prometheus_multiproc_dir old_metrics = Dir[File.join(dir, '*.db')]
old_metrics = Dir[File.join(prometheus_multiproc_dir, '*.db')]
FileUtils.rm_rf(old_metrics) FileUtils.rm_rf(old_metrics)
end end
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
...@@ -88,9 +88,21 @@ before_exec do |server| ...@@ -88,9 +88,21 @@ before_exec do |server|
Gitlab::Cluster::LifecycleEvents.do_master_restart Gitlab::Cluster::LifecycleEvents.do_master_restart
end end
run_once = true
before_fork do |server, worker| 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
# Signal application hooks that we're about to fork # Signal application hooks that we're about to fork
Gitlab::Cluster::LifecycleEvents.do_before_fork Gitlab::Cluster::LifecycleEvents.do_before_fork
end
# The following is only recommended for memory/DB-constrained # The following is only recommended for memory/DB-constrained
# installations. It is not needed if your system can house # installations. It is not needed if your system can house
......
...@@ -21,9 +21,21 @@ before_exec do |server| ...@@ -21,9 +21,21 @@ before_exec do |server|
Gitlab::Cluster::LifecycleEvents.do_master_restart Gitlab::Cluster::LifecycleEvents.do_master_restart
end end
run_once = true
before_fork do |server, worker| 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
# Signal application hooks that we're about to fork # Signal application hooks that we're about to fork
Gitlab::Cluster::LifecycleEvents.do_before_fork Gitlab::Cluster::LifecycleEvents.do_before_fork
end
# The following is only recommended for memory/DB-constrained # The following is only recommended for memory/DB-constrained
# installations. It is not needed if your system can house # installations. It is not needed if your system can house
......
...@@ -11,6 +11,9 @@ module Gitlab ...@@ -11,6 +11,9 @@ module Gitlab
# We have three lifecycle events. # We have three lifecycle events.
# #
# - before_fork (only in forking processes) # - 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 # - worker_start
# - before_master_restart (only in forking processes) # - before_master_restart (only in forking processes)
# #
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment