Class: CI::Queue::Redis::BuildRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/ci/queue/redis/build_record.rb

Constant Summary collapse

TOTAL_KEY =
"___total___"

Instance Method Summary collapse

Constructor Details

#initialize(queue, redis, config) ⇒ BuildRecord

Returns a new instance of BuildRecord.



6
7
8
9
10
# File 'lib/ci/queue/redis/build_record.rb', line 6

def initialize(queue, redis, config)
  @queue = queue
  @redis = redis
  @config = config
end

Instance Method Details

#error_reportsObject



84
85
86
# File 'lib/ci/queue/redis/build_record.rb', line 84

def error_reports
  redis.hgetall(key('error-reports'))
end

#failed_testsObject



20
21
22
# File 'lib/ci/queue/redis/build_record.rb', line 20

def failed_tests
  redis.hkeys(key('error-reports'))
end

#fetch_stats(stat_names) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/ci/queue/redis/build_record.rb', line 92

def fetch_stats(stat_names)
  counts = redis.pipelined do |pipeline|
    stat_names.each { |c| pipeline.hvals(key(c)) }
  end
  sum_counts = counts.map do |values|
    values.map(&:to_f).inject(:+).to_f
  end
  stat_names.zip(sum_counts).to_h
end

#flaky_reportsObject



88
89
90
# File 'lib/ci/queue/redis/build_record.rb', line 88

def flaky_reports
  redis.smembers(key('flaky-reports'))
end

#max_test_failed?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
# File 'lib/ci/queue/redis/build_record.rb', line 78

def max_test_failed?
  return false if config.max_test_failed.nil?

  @queue.test_failures >= config.max_test_failed
end

#pop_warningsObject



31
32
33
34
35
36
37
38
# File 'lib/ci/queue/redis/build_record.rb', line 31

def pop_warnings
  warnings = redis.multi do |transaction|
    transaction.lrange(key('warnings'), 0, -1)
    transaction.del(key('warnings'))
  end.first

  warnings.map { |p| Marshal.load(p) }
end

#progressObject



12
13
14
# File 'lib/ci/queue/redis/build_record.rb', line 12

def progress
  @queue.progress
end

#queue_exhausted?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/ci/queue/redis/build_record.rb', line 16

def queue_exhausted?
  @queue.exhausted?
end

#record_error(id, payload, stats: nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ci/queue/redis/build_record.rb', line 44

def record_error(id, payload, stats: nil)
  redis.pipelined do |pipeline|
    pipeline.hset(
      key('error-reports'),
      id.dup.force_encoding(Encoding::BINARY),
      payload.dup.force_encoding(Encoding::BINARY),
    )
    pipeline.expire(key('error-reports'), config.redis_ttl)
    record_stats(stats, pipeline: pipeline)
  end
  nil
end

#record_flaky(id, stats: nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/ci/queue/redis/build_record.rb', line 67

def record_flaky(id, stats: nil)
  redis.pipelined do |pipeline|
    pipeline.sadd?(
      key('flaky-reports'),
      id.b
    )
    pipeline.expire(key('flaky-reports'), config.redis_ttl)
  end
  nil
end

#record_success(id, stats: nil, skip_flaky_record: false) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/ci/queue/redis/build_record.rb', line 57

def record_success(id, stats: nil, skip_flaky_record: false)
  error_reports_deleted_count, requeued_count, _ = redis.pipelined do |pipeline|
    pipeline.hdel(key('error-reports'), id.dup.force_encoding(Encoding::BINARY))
    pipeline.hget(key('requeues-count'), id.b)
    record_stats(stats, pipeline: pipeline)
  end
  record_flaky(id) if !skip_flaky_record && (error_reports_deleted_count.to_i > 0 || requeued_count.to_i > 0)
  nil
end

#record_warning(type, attributes) ⇒ Object



40
41
42
# File 'lib/ci/queue/redis/build_record.rb', line 40

def record_warning(type, attributes)
  redis.rpush(key('warnings'), Marshal.dump([type, attributes]))
end

#requeued_testsObject



25
26
27
28
29
# File 'lib/ci/queue/redis/build_record.rb', line 25

def requeued_tests
  requeues = redis.hgetall(key('requeues-count'))
  requeues.delete(TOTAL_KEY)
  requeues
end

#reset_stats(stat_names) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/ci/queue/redis/build_record.rb', line 102

def reset_stats(stat_names)
  redis.pipelined do |pipeline|
    stat_names.each do |stat_name|
      pipeline.hdel(key(stat_name), config.worker_id)
    end
  end
end