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

readiness.rb « probes « health_checks « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 28abf490ffc12919c90ad1dbd31487ac0eab9855 (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
# frozen_string_literal: true

module Gitlab
  module HealthChecks
    module Probes
      class Readiness
        attr_reader :checks

        # This accepts an array of objects implementing `:readiness`
        # that returns `::Gitlab::HealthChecks::Result`
        def initialize(*additional_checks)
          @checks = ::Gitlab::HealthChecks::CHECKS
          @checks += additional_checks
        end

        def execute
          readiness = probe_readiness
          success = all_succeeded?(readiness)

          Probes::Status.new(
            success ? 200 : 503,
            status(success).merge(payload(readiness))
          )
        end

        private

        def all_succeeded?(readiness)
          readiness.all? do |name, probes|
            probes.any?(&:success)
          end
        end

        def status(success)
          { status: success ? 'ok' : 'failed' }
        end

        def payload(readiness)
          readiness.transform_values do |probes|
            probes.map(&:payload)
          end
        end

        def probe_readiness
          checks
            .flat_map(&:readiness)
            .compact
            .group_by(&:name)
        end
      end
    end
  end
end