Module: Gitlab::Runtime
- Defined in:
- lib/gitlab/runtime.rb
Overview
Provides routines to identify the current runtime as which the application executes, such as whether it is an application server and which one.
Constant Summary collapse
- 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 ].freeze
Class Method Summary collapse
-
.application? ⇒ Boolean
Whether we are executing in an actual application context i.e.
- .console? ⇒ Boolean
- .geo_log_cursor? ⇒ Boolean
- .identify ⇒ Object
- .max_threads ⇒ Object
-
.multi_threaded? ⇒ Boolean
Whether we are executing in a multi-threaded environment.
- .puma? ⇒ Boolean
- .puma_in_clustered_mode? ⇒ Boolean
- .rails_runner? ⇒ Boolean
- .rake? ⇒ Boolean
- .safe_identify ⇒ Object
- .sidekiq? ⇒ Boolean
- .test_suite? ⇒ Boolean
Class Method Details
.application? ⇒ Boolean
Whether we are executing in an actual application context i.e. Puma or Sidekiq.
69 70 71 |
# File 'lib/gitlab/runtime.rb', line 69 def application? puma? || sidekiq? end |
.console? ⇒ Boolean
56 57 58 |
# File 'lib/gitlab/runtime.rb', line 56 def console? !!defined?(::Rails::Console) end |
.geo_log_cursor? ⇒ Boolean
60 61 62 |
# File 'lib/gitlab/runtime.rb', line 60 def geo_log_cursor? !!defined?(::GeoLogCursorOptionParser) end |
.identify ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gitlab/runtime.rb', line 22 def identify matches = AVAILABLE_RUNTIMES.select { |runtime| public_send("#{runtime}?") } # rubocop:disable GitlabSecurity/PublicSend if matches.one? matches.first elsif matches.none? raise UnknownProcessError, "Failed to identify runtime for process #{Process.pid} (#{$PROGRAM_NAME})" else raise AmbiguousProcessError, "Ambiguous runtime #{matches} for process #{Process.pid} (#{$PROGRAM_NAME})" end end |
.max_threads ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/gitlab/runtime.rb', line 86 def max_threads threads = 1 # main thread if puma? && ::Puma.respond_to?(:cli_config) threads += ::Puma.cli_config.[:max_threads] elsif sidekiq? # Sidekiq has a internal connection pool to handle heartbeat, scheduled polls, # cron polls and housekeeping. max_threads can match Sidekqi process's concurrency. # # The Sidekiq main thread does not perform GitLab-related logic, so we can ignore it. threads = Sidekiq.default_configuration[:concurrency] end if puma? threads += Gitlab::ActionCable::Config.worker_pool_size end threads end |
.multi_threaded? ⇒ Boolean
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.
75 76 77 |
# File 'lib/gitlab/runtime.rb', line 75 def multi_threaded? application? end |
.puma? ⇒ Boolean
40 41 42 |
# File 'lib/gitlab/runtime.rb', line 40 def puma? !!defined?(::Puma::Server) end |
.puma_in_clustered_mode? ⇒ Boolean
79 80 81 82 83 84 |
# File 'lib/gitlab/runtime.rb', line 79 def puma_in_clustered_mode? return unless puma? return unless ::Puma.respond_to?(:cli_config) ::Puma.cli_config.[:workers].to_i > 0 end |
.rails_runner? ⇒ Boolean
64 65 66 |
# File 'lib/gitlab/runtime.rb', line 64 def rails_runner? !!defined?(::Rails::Command::RunnerCommand) end |
.rake? ⇒ Boolean
48 49 50 |
# File 'lib/gitlab/runtime.rb', line 48 def rake? !!(defined?(::Rake) && Rake.application.top_level_tasks.any?) end |
.safe_identify ⇒ Object
34 35 36 37 38 |
# File 'lib/gitlab/runtime.rb', line 34 def safe_identify identify rescue UnknownProcessError, AmbiguousProcessError nil end |
.sidekiq? ⇒ Boolean
44 45 46 |
# File 'lib/gitlab/runtime.rb', line 44 def sidekiq? !!(defined?(::Sidekiq) && Sidekiq.try(:server?)) end |
.test_suite? ⇒ Boolean
52 53 54 |
# File 'lib/gitlab/runtime.rb', line 52 def test_suite? Rails.env.test? end |