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

Class Method Details

.application?Boolean

Whether we are executing in an actual application context i.e. Puma or Sidekiq.

Returns:

  • (Boolean)


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

def application?
  puma? || sidekiq?
end

.console?Boolean

Returns:

  • (Boolean)


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

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

.geo_log_cursor?Boolean

Returns:

  • (Boolean)


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

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

.identifyObject



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_threadsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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.options[:max_threads]
  elsif sidekiq?
    # 2 extra threads for the pollers in Sidekiq and Sidekiq Cron:
    # https://github.com/ondrejbartas/sidekiq-cron#under-the-hood
    #
    # These threads execute Sidekiq client middleware when jobs
    # are enqueued and those can access DB / Redis.
    threads += Sidekiq[:concurrency] + 2
  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.

Returns:

  • (Boolean)


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

def multi_threaded?
  application?
end

.puma?Boolean

Returns:

  • (Boolean)


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

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

.puma_in_clustered_mode?Boolean

Returns:

  • (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.options[:workers].to_i > 0
end

.rails_runner?Boolean

Returns:

  • (Boolean)


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

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

.rake?Boolean

Returns:

  • (Boolean)


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

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

.safe_identifyObject



34
35
36
37
38
# File 'lib/gitlab/runtime.rb', line 34

def safe_identify
  identify
rescue UnknownProcessError, AmbiguousProcessError
  nil
end

.sidekiq?Boolean

Returns:

  • (Boolean)


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

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

.test_suite?Boolean

Returns:

  • (Boolean)


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

def test_suite?
  Rails.env.test?
end