summaryrefslogtreecommitdiff
path: root/lib/gitlab/git/storage/failure_info.rb
blob: 1d28a8500495f1954541800194bf057f97873a85 (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
module Gitlab
  module Git
    module Storage
      class FailureInfo
        attr_accessor :first_failure, :last_failure, :failure_count

        def self.reset_all!
          Gitlab::Git::Storage.redis.with do |redis|
            all_storage_keys = redis.zrange(Gitlab::Git::Storage::REDIS_KNOWN_KEYS, 0, -1)
            redis.del(*all_storage_keys) unless all_storage_keys.empty?
          end

          Gitlab::SafeRequestStore.delete(:circuitbreaker_cache)
        end

        def self.load(cache_key)
          first_failure, last_failure, failure_count = Gitlab::Git::Storage.redis.with do |redis|
            redis.hmget(cache_key, :first_failure, :last_failure, :failure_count)
          end

          last_failure = Time.at(last_failure.to_i) if last_failure.present?
          first_failure = Time.at(first_failure.to_i) if first_failure.present?

          new(first_failure, last_failure, failure_count.to_i)
        end

        def initialize(first_failure, last_failure, failure_count)
          @first_failure = first_failure
          @last_failure = last_failure
          @failure_count = failure_count
        end

        def no_failures?
          first_failure.blank? && last_failure.blank? && failure_count == 0
        end
      end
    end
  end
end