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,
  :unicorn
].freeze

Class Method Summary collapse

Class Method Details

.action_cable?Boolean

Returns:

  • (Boolean)

76
77
78
# File 'lib/gitlab/runtime.rb', line 76

def action_cable?
  web_server? && (!!defined?(ACTION_CABLE_SERVER) || Gitlab::ActionCable::Config.in_app?)
end

.console?Boolean

Returns:

  • (Boolean)

60
61
62
# File 'lib/gitlab/runtime.rb', line 60

def console?
  !!defined?(::Rails::Console)
end

.geo_log_cursor?Boolean

Returns:

  • (Boolean)

64
65
66
# File 'lib/gitlab/runtime.rb', line 64

def geo_log_cursor?
  !!defined?(::GeoLogCursorOptionParser)
end

.identifyObject


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gitlab/runtime.rb', line 23

def identify
  matches = AVAILABLE_RUNTIMES.select { |runtime| public_send("#{runtime}?") } # rubocop:disable GitlabSecurity/PublicSend

  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

.max_threadsObject


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gitlab/runtime.rb', line 84

def max_threads
  threads = 1 # main thread

  if puma?
    threads += Puma.cli_config.options[:max_threads]
  elsif sidekiq?
    # An extra thread for the poller in Sidekiq Cron:
    # https://github.com/ondrejbartas/sidekiq-cron#under-the-hood
    threads += Sidekiq.options[:concurrency] + 1
  end

  if action_cable?
    threads += Gitlab::ActionCable::Config.worker_pool_size
  end

  threads
end

.multi_threaded?Boolean

Returns:

  • (Boolean)

80
81
82
# File 'lib/gitlab/runtime.rb', line 80

def multi_threaded?
  puma? || sidekiq? || action_cable?
end

.puma?Boolean

Returns:

  • (Boolean)

39
40
41
# File 'lib/gitlab/runtime.rb', line 39

def puma?
  !!defined?(::Puma)
end

.rails_runner?Boolean

Returns:

  • (Boolean)

68
69
70
# File 'lib/gitlab/runtime.rb', line 68

def rails_runner?
  !!defined?(::Rails::Command::RunnerCommand)
end

.rake?Boolean

Returns:

  • (Boolean)

52
53
54
# File 'lib/gitlab/runtime.rb', line 52

def rake?
  !!(defined?(::Rake) && Rake.application.top_level_tasks.any?)
end

.sidekiq?Boolean

Returns:

  • (Boolean)

48
49
50
# File 'lib/gitlab/runtime.rb', line 48

def sidekiq?
  !!(defined?(::Sidekiq) && Sidekiq.server?)
end

.test_suite?Boolean

Returns:

  • (Boolean)

56
57
58
# File 'lib/gitlab/runtime.rb', line 56

def test_suite?
  Rails.env.test?
end

.unicorn?Boolean

For unicorn, we need to check for actual server instances to avoid false positives.

Returns:

  • (Boolean)

44
45
46
# File 'lib/gitlab/runtime.rb', line 44

def unicorn?
  !!(defined?(::Unicorn) && defined?(::Unicorn::HttpServer))
end

.web_server?Boolean

Returns:

  • (Boolean)

72
73
74
# File 'lib/gitlab/runtime.rb', line 72

def web_server?
  puma? || unicorn?
end