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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

109 lines
2.9 KiB
Ruby
Raw Permalink 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,
2021-09-04 01:27:46 +05:30
:test_suite
2020-03-13 15:44:24 +05:30
].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?
2022-11-25 23:54:43 +05:30
raise UnknownProcessError, "Failed to identify runtime for process #{Process.pid} (#{$PROGRAM_NAME})"
2020-03-13 15:44:24 +05:30
else
2022-11-25 23:54:43 +05:30
raise AmbiguousProcessError, "Ambiguous runtime #{matches} for process #{Process.pid} (#{$PROGRAM_NAME})"
2020-03-13 15:44:24 +05:30
end
end
2022-04-04 11:22:00 +05:30
def safe_identify
identify
rescue UnknownProcessError, AmbiguousProcessError
nil
end
2020-03-13 15:44:24 +05:30
def puma?
2023-07-09 08:55:56 +05:30
!!defined?(::Puma::Server)
2020-03-13 15:44:24 +05:30
end
def sidekiq?
2023-01-13 00:05:48 +05:30
!!(defined?(::Sidekiq) && Sidekiq.try(:server?))
2020-03-13 15:44:24 +05:30
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
2022-05-07 20:08:51 +05:30
# Whether we are executing in an actual application context i.e. Puma or Sidekiq.
def application?
puma? || sidekiq?
2020-05-24 23:13:21 +05:30
end
2022-05-07 20:08:51 +05:30
# 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.
2020-03-13 15:44:24 +05:30
def multi_threaded?
2022-05-07 20:08:51 +05:30
application?
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?
2021-12-11 22:18:48 +05:30
# 2 extra threads for the pollers in Sidekiq and Sidekiq Cron:
2020-03-13 15:44:24 +05:30
# https://github.com/ondrejbartas/sidekiq-cron#under-the-hood
2021-12-11 22:18:48 +05:30
#
# These threads execute Sidekiq client middleware when jobs
# are enqueued and those can access DB / Redis.
2023-01-13 00:05:48 +05:30
threads += Sidekiq[:concurrency] + 2
2020-07-28 23:09:34 +05:30
end
2022-05-07 20:08:51 +05:30
if puma?
2020-07-28 23:09:34 +05:30
threads += Gitlab::ActionCable::Config.worker_pool_size
end
threads
2020-03-13 15:44:24 +05:30
end
end
end
end