Welcome to mirror list, hosted at ThFree Co, Russian Federation.

runtime.rb « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 07a3afb883448c28064db1cb55ddd6fc1cf28825 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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
    class << self
      def name
        matches = []
        matches << :puma if puma?
        matches << :unicorn if unicorn?
        matches << :console if console?
        matches << :sidekiq if sidekiq?

        raise "Ambiguous process match: #{matches}" if matches.size > 1

        matches.first || :unknown
      end

      def puma?
        !!(defined?(::Puma) && bin == 'puma')
      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? && bin == 'sidekiq')
      end

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

      def app_server?
        puma? || unicorn?
      end

      def multi_threaded?
        puma? || sidekiq?
      end

      private

      # Some example values from my system:
      #   puma: /data/cache/bundle-2.5/bin/puma
      #   unicorn: unicorn_rails master -E development -c /tmp/unicorn.rb -l 0.0.0.0:8080
      #   sidekiq: /data/cache/bundle-2.5/bin/sidekiq
      #   thin: bin/rails
      #   console: bin/rails
      def script_name
        $0
      end

      def bin
        File.basename(script_name)
      end
    end
  end
end