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

collection.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: db3ef4834c2d8470c95da77de1882e49c9507658 (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
# frozen_string_literal: true

module Gitlab
  module HealthChecks
    module Probes
      class Collection
        attr_reader :checks

        # This accepts an array of objects implementing `:readiness`
        # that returns `::Gitlab::HealthChecks::Result`
        def initialize(*checks)
          @checks = 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