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

master_check.rb « health_checks « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b2c3695e6d93c325d56146e43678763617561e02 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true

module Gitlab
  module HealthChecks
    # This check is registered on master,
    # and validated by worker
    class MasterCheck
      extend SimpleAbstractCheck

      class << self
        extend ::Gitlab::Utils::Override

        override :available?
        def available?
          Gitlab::Runtime.puma_in_clustered_mode?
        end

        def register_master
          return unless available?

          # when we fork, we pass the read pipe to child
          # child can then react on whether the other end
          # of pipe is still available
          @pipe_read, @pipe_write = IO.pipe
        end

        def finish_master
          return unless available?

          close_read
          close_write
        end

        def register_worker
          return unless available?

          # fork needs to close the pipe
          close_write
        end

        private

        def close_read
          @pipe_read&.close
          @pipe_read = nil
        end

        def close_write
          @pipe_write&.close
          @pipe_write = nil
        end

        def metric_prefix
          'master_check'
        end

        def successful?(result)
          result
        end

        def check
          # the lack of pipe is a legitimate failure of check
          return false unless @pipe_read

          @pipe_read.read_nonblock(1)

          true
        rescue IO::EAGAINWaitReadable
          # if it is blocked, it means that the pipe is still open
          # and there's no data waiting on it
          true
        rescue EOFError
          # the pipe is closed
          false
        end
      end
    end
  end
end