debian-mirror-gitlab/lib/gitlab/runtime.rb

107 lines
2.7 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# 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
IdentificationError = Class.new(RuntimeError)
AmbiguousProcessError = Class.new(IdentificationError)
UnknownProcessError = Class.new(IdentificationError)
AVAILABLE_RUNTIMES = [
:console,
:geo_log_cursor,
:puma,
:rails_runner,
:rake,
:sidekiq,
:test_suite,
:unicorn
].freeze
class << self
def identify
matches = AVAILABLE_RUNTIMES.select { |runtime| public_send("#{runtime}?") } # rubocop:disable GitlabSecurity/PublicSend
if matches.one?
matches.first
elsif matches.none?
2021-06-08 01:23:25 +05:30
raise UnknownProcessError, "Failed to identify runtime for process #{Process.pid} (#{$0})"
2020-03-13 15:44:24 +05:30
else
2021-06-08 01:23:25 +05:30
raise AmbiguousProcessError, "Ambiguous runtime #{matches} for process #{Process.pid} (#{$0})"
2020-03-13 15:44:24 +05:30
end
end
def puma?
2020-07-28 23:09:34 +05:30
!!defined?(::Puma)
2020-03-13 15:44:24 +05:30
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
def rake?
!!(defined?(::Rake) && Rake.application.top_level_tasks.any?)
end
def test_suite?
Rails.env.test?
end
def console?
!!defined?(::Rails::Console)
end
def geo_log_cursor?
!!defined?(::GeoLogCursorOptionParser)
end
def rails_runner?
!!defined?(::Rails::Command::RunnerCommand)
end
def web_server?
2020-07-28 23:09:34 +05:30
puma? || unicorn?
2020-05-24 23:13:21 +05:30
end
def action_cable?
2020-07-28 23:09:34 +05:30
web_server? && (!!defined?(ACTION_CABLE_SERVER) || Gitlab::ActionCable::Config.in_app?)
2020-03-13 15:44:24 +05:30
end
def multi_threaded?
2020-05-24 23:13:21 +05:30
puma? || sidekiq? || action_cable?
2020-03-13 15:44:24 +05:30
end
2021-03-11 19:13:27 +05:30
def puma_in_clustered_mode?
2021-04-17 20:07:23 +05:30
return unless puma?
return unless Puma.respond_to?(:cli_config)
Puma.cli_config.options[:workers].to_i > 0
2021-03-11 19:13:27 +05:30
end
2020-03-13 15:44:24 +05:30
def max_threads
2020-07-28 23:09:34 +05:30
threads = 1 # main thread
2020-03-13 15:44:24 +05:30
2021-06-08 01:23:25 +05:30
if puma? && Puma.respond_to?(:cli_config)
2020-07-28 23:09:34 +05:30
threads += Puma.cli_config.options[:max_threads]
2020-03-13 15:44:24 +05:30
elsif sidekiq?
# An extra thread for the poller in Sidekiq Cron:
# https://github.com/ondrejbartas/sidekiq-cron#under-the-hood
2020-07-28 23:09:34 +05:30
threads += Sidekiq.options[:concurrency] + 1
end
if action_cable?
threads += Gitlab::ActionCable::Config.worker_pool_size
end
threads
2020-03-13 15:44:24 +05:30
end
end
end
end