2019-12-22 04:07:51 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
# Provides routines to identify the current runtime as which the application
|
|
|
|
# executes, such as whether it is an application server and which one.
|
|
|
|
module Runtime
|
2019-12-27 13:08:12 -05:00
|
|
|
IdentificationError = Class.new(RuntimeError)
|
|
|
|
AmbiguousProcessError = Class.new(IdentificationError)
|
|
|
|
UnknownProcessError = Class.new(IdentificationError)
|
2019-12-22 04:07:51 -05:00
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
AVAILABLE_RUNTIMES = [
|
|
|
|
:console,
|
|
|
|
:geo_log_cursor,
|
|
|
|
:puma,
|
2020-02-15 01:09:11 -05:00
|
|
|
:rails_runner,
|
2020-02-06 13:08:54 -05:00
|
|
|
:rake,
|
|
|
|
:sidekiq,
|
2021-05-26 17:10:49 -04:00
|
|
|
:test_suite
|
2020-02-06 13:08:54 -05:00
|
|
|
].freeze
|
|
|
|
|
2019-12-22 04:07:51 -05:00
|
|
|
class << self
|
|
|
|
def identify
|
2020-02-06 13:08:54 -05:00
|
|
|
matches = AVAILABLE_RUNTIMES.select { |runtime| public_send("#{runtime}?") } # rubocop:disable GitlabSecurity/PublicSend
|
2019-12-22 04:07:51 -05:00
|
|
|
|
|
|
|
if matches.one?
|
|
|
|
matches.first
|
|
|
|
elsif matches.none?
|
2021-05-04 11:10:36 -04:00
|
|
|
raise UnknownProcessError, "Failed to identify runtime for process #{Process.pid} (#{$0})"
|
2019-12-22 04:07:51 -05:00
|
|
|
else
|
2021-05-04 11:10:36 -04:00
|
|
|
raise AmbiguousProcessError, "Ambiguous runtime #{matches} for process #{Process.pid} (#{$0})"
|
2019-12-22 04:07:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-03 13:17:34 -05:00
|
|
|
def safe_identify
|
|
|
|
identify
|
|
|
|
rescue UnknownProcessError, AmbiguousProcessError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2019-12-22 04:07:51 -05:00
|
|
|
def puma?
|
2020-07-08 02:09:13 -04:00
|
|
|
!!defined?(::Puma)
|
2019-12-22 04:07:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def sidekiq?
|
|
|
|
!!(defined?(::Sidekiq) && Sidekiq.server?)
|
|
|
|
end
|
|
|
|
|
2019-12-27 13:08:12 -05:00
|
|
|
def rake?
|
|
|
|
!!(defined?(::Rake) && Rake.application.top_level_tasks.any?)
|
|
|
|
end
|
|
|
|
|
2020-01-31 07:08:33 -05:00
|
|
|
def test_suite?
|
|
|
|
Rails.env.test?
|
2019-12-27 13:08:12 -05:00
|
|
|
end
|
|
|
|
|
2019-12-22 04:07:51 -05:00
|
|
|
def console?
|
|
|
|
!!defined?(::Rails::Console)
|
|
|
|
end
|
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
def geo_log_cursor?
|
|
|
|
!!defined?(::GeoLogCursorOptionParser)
|
|
|
|
end
|
|
|
|
|
2020-02-15 01:09:11 -05:00
|
|
|
def rails_runner?
|
|
|
|
!!defined?(::Rails::Command::RunnerCommand)
|
|
|
|
end
|
|
|
|
|
2022-03-02 10:16:07 -05:00
|
|
|
# Whether we are executing in an actual application context i.e. Puma or Sidekiq.
|
|
|
|
def application?
|
|
|
|
puma? || sidekiq?
|
2020-05-03 17:09:39 -04:00
|
|
|
end
|
|
|
|
|
2022-03-02 10:16:07 -05:00
|
|
|
# Whether we are executing in a multi-threaded environment. For now this is equivalent
|
|
|
|
# to meaning Puma or Sidekiq, but this could change in the future.
|
2019-12-22 04:07:51 -05:00
|
|
|
def multi_threaded?
|
2022-03-02 10:16:07 -05:00
|
|
|
application?
|
2019-12-22 04:07:51 -05:00
|
|
|
end
|
2019-12-27 13:08:12 -05:00
|
|
|
|
2021-02-16 10:09:50 -05:00
|
|
|
def puma_in_clustered_mode?
|
2021-02-22 13:10:55 -05:00
|
|
|
return unless puma?
|
|
|
|
return unless Puma.respond_to?(:cli_config)
|
|
|
|
|
|
|
|
Puma.cli_config.options[:workers].to_i > 0
|
2021-02-16 10:09:50 -05:00
|
|
|
end
|
|
|
|
|
2020-01-15 22:08:47 -05:00
|
|
|
def max_threads
|
2020-07-08 02:09:13 -04:00
|
|
|
threads = 1 # main thread
|
2020-02-17 07:09:20 -05:00
|
|
|
|
2021-05-11 02:10:29 -04:00
|
|
|
if puma? && Puma.respond_to?(:cli_config)
|
2020-07-08 02:09:13 -04:00
|
|
|
threads += Puma.cli_config.options[:max_threads]
|
2020-01-15 22:08:47 -05:00
|
|
|
elsif sidekiq?
|
2021-10-20 17:17:26 -04:00
|
|
|
# 2 extra threads for the pollers in Sidekiq and Sidekiq Cron:
|
2020-02-17 07:09:20 -05:00
|
|
|
# https://github.com/ondrejbartas/sidekiq-cron#under-the-hood
|
2021-10-20 17:17:26 -04:00
|
|
|
#
|
|
|
|
# These threads execute Sidekiq client middleware when jobs
|
|
|
|
# are enqueued and those can access DB / Redis.
|
|
|
|
threads += Sidekiq.options[:concurrency] + 2
|
2020-07-08 02:09:13 -04:00
|
|
|
end
|
|
|
|
|
2022-03-02 10:16:07 -05:00
|
|
|
if puma?
|
2020-07-08 02:09:13 -04:00
|
|
|
threads += Gitlab::ActionCable::Config.worker_pool_size
|
|
|
|
end
|
|
|
|
|
|
|
|
threads
|
2020-01-15 22:08:47 -05:00
|
|
|
end
|
2019-12-22 04:07:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|