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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-12-22 04:07:51 -05:00
|
|
|
def web_server?
|
2021-05-26 17:10:49 -04:00
|
|
|
puma?
|
2020-05-03 17:09:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def action_cable?
|
2020-07-08 02:09:13 -04:00
|
|
|
web_server? && (!!defined?(ACTION_CABLE_SERVER) || Gitlab::ActionCable::Config.in_app?)
|
2019-12-22 04:07:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def multi_threaded?
|
2020-05-03 17:09:39 -04:00
|
|
|
puma? || sidekiq? || action_cable?
|
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?
|
2020-02-17 07:09:20 -05:00
|
|
|
# An extra thread for the poller in Sidekiq Cron:
|
|
|
|
# https://github.com/ondrejbartas/sidekiq-cron#under-the-hood
|
2020-07-08 02:09:13 -04:00
|
|
|
threads += Sidekiq.options[:concurrency] + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if action_cable?
|
|
|
|
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
|