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,
|
|
|
|
:test_suite,
|
|
|
|
:unicorn
|
|
|
|
].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?
|
|
|
|
raise UnknownProcessError.new(
|
|
|
|
"Failed to identify runtime for process #{Process.pid} (#{$0})"
|
|
|
|
)
|
|
|
|
else
|
|
|
|
raise AmbiguousProcessError.new(
|
|
|
|
"Ambiguous runtime #{matches} for process #{Process.pid} (#{$0})"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def puma?
|
2020-07-08 02:09:13 -04:00
|
|
|
!!defined?(::Puma)
|
2019-12-22 04:07:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# For unicorn, we need to check for actual server instances to avoid false positives.
|
|
|
|
def unicorn?
|
|
|
|
!!(defined?(::Unicorn) && defined?(::Unicorn::HttpServer))
|
|
|
|
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?
|
2020-07-08 02:09:13 -04:00
|
|
|
puma? || unicorn?
|
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
|
|
|
|
2020-07-08 02:09:13 -04:00
|
|
|
if puma?
|
|
|
|
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
|