summaryrefslogtreecommitdiff
path: root/app/models/ci/build_trace_chunks/redis.rb
blob: fdb6065e2a032c2e36ba7b4f1565bf209505d1a5 (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
module Ci
  module BuildTraceChunks
    class Redis
      CHUNK_REDIS_TTL = 1.week

      def available?
        true
      end

      def data(model)
        Gitlab::Redis::SharedState.with do |redis|
          redis.get(key(model))
        end
      end

      def set_data(model, data)
        Gitlab::Redis::SharedState.with do |redis|
          redis.set(key(model), data, ex: CHUNK_REDIS_TTL)
        end
      end

      def delete_data(model)
        delete_keys([[model.build_id, model.chunk_index]])
      end

      def keys(relation)
        relation.pluck(:build_id, :chunk_index)
      end

      def delete_keys(keys)
        return if keys.empty?

        keys = keys.map { |key| key_raw(*key) }

        Gitlab::Redis::SharedState.with do |redis|
          redis.del(keys)
        end
      end

      private

      def key(model)
        key_raw(model.build_id, model.chunk_index)
      end

      def key_raw(build_id, chunk_index)
        "gitlab:ci:trace:#{build_id.to_i}:chunks:#{chunk_index.to_i}"
      end
    end
  end
end