Class: CI::Queue::Redis::Base

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/ci/queue/redis/base.rb

Direct Known Subclasses

Supervisor, Worker

Defined Under Namespace

Modules: RedisInstrumentation Classes: HeartbeatProcess, State

Constant Summary collapse

TEN_MINUTES =
60 * 10
CONNECTION_ERRORS =
[
  ::Redis::BaseConnectionError,
  ::SocketError, # https://github.com/redis/redis-rb/pull/631
].freeze

Instance Attribute Summary

Attributes included from Common

#config

Instance Method Summary collapse

Methods included from Common

#distributed?, #flaky?, #release!, #report_failure!, #report_success!, #rescue_connection_errors, #retrying?

Constructor Details

#initialize(redis_url, config) ⇒ Base

Returns a new instance of Base.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ci/queue/redis/base.rb', line 32

def initialize(redis_url, config)
  @redis_url = redis_url
  @config = config
  if ::Redis::VERSION > "5.0.0"
    @redis = ::Redis.new(
      url: redis_url,
      # Booting a CI worker is costly, so in case of a Redis blip,
      # it makes sense to retry for a while before giving up.
      reconnect_attempts: reconnect_attempts,
      middlewares: custom_middlewares,
      # Hosted Redis servers use self signed certificates
      # (because they do not own the domain they're running on such as compute-1.amazonaws.com)
      # therefore a full SSL connection verification will fail.
      # ci-queue should not contain any sensitive data, so we can just disable the verification.
      ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE },
      custom: custom_config,
    )
  else
    @redis = ::Redis.new(url: redis_url)
  end
end

Instance Method Details

#boot_heartbeat_process!Object



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

def boot_heartbeat_process!
  return unless heartbeat_enabled?

  heartbeat_process.boot!
end

#created_at=(timestamp) ⇒ Object



117
118
119
# File 'lib/ci/queue/redis/base.rb', line 117

def created_at=(timestamp)
  redis.setnx(key('created-at'), timestamp)
end

#custom_configObject



91
92
93
94
95
96
# File 'lib/ci/queue/redis/base.rb', line 91

def custom_config
  return unless config.debug_log

  require 'logger'
  { debug_log: Logger.new(config.debug_log) }
end

#custom_middlewaresObject



98
99
100
101
102
# File 'lib/ci/queue/redis/base.rb', line 98

def custom_middlewares
  return unless config.debug_log

  [RedisInstrumentation]
end

#ensure_heartbeat_thread_alive!Object



71
72
73
74
75
76
# File 'lib/ci/queue/redis/base.rb', line 71

def ensure_heartbeat_thread_alive!
  return unless heartbeat_enabled?
  return if @heartbeat_thread&.alive?

  @heartbeat_thread = Thread.start { heartbeat }
end

#exhausted?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/ci/queue/redis/base.rb', line 104

def exhausted?
  queue_initialized? && size == 0
end

#expired?Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
# File 'lib/ci/queue/redis/base.rb', line 108

def expired?
  if (created_at = redis.get(key('created-at')))
    (created_at.to_f + config.redis_ttl + TEN_MINUTES) < CI::Queue.time_now.to_f
  else
    # if there is no created at set anymore we assume queue is expired
    true
  end
end

#increment_test_failedObject



177
178
179
# File 'lib/ci/queue/redis/base.rb', line 177

def increment_test_failed
  redis.incr(key('test_failed_count'))
end

#max_test_failed?Boolean

Returns:

  • (Boolean)


185
186
187
188
189
# File 'lib/ci/queue/redis/base.rb', line 185

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

  test_failed >= config.max_test_failed
end

#progressObject



143
144
145
# File 'lib/ci/queue/redis/base.rb', line 143

def progress
  total - size
end

#queue_initialized?Boolean

Returns:

  • (Boolean)


166
167
168
169
170
171
# File 'lib/ci/queue/redis/base.rb', line 166

def queue_initialized?
  @queue_initialized ||= begin
    status = master_status
    status == 'ready' || status == 'finished'
  end
end

#queue_initializing?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/ci/queue/redis/base.rb', line 173

def queue_initializing?
  master_status == 'setup'
end

#reconnect_attemptsObject



54
55
56
57
58
# File 'lib/ci/queue/redis/base.rb', line 54

def reconnect_attempts
  return [] if ENV["CI_QUEUE_DISABLE_RECONNECT_ATTEMPTS"]

  [0, 0, 0.1, 0.5, 1, 3, 5]
end

#remainingObject



128
129
130
# File 'lib/ci/queue/redis/base.rb', line 128

def remaining
  redis.llen(key('queue'))
end

#runningObject



132
133
134
# File 'lib/ci/queue/redis/base.rb', line 132

def running
  redis.zcard(key('running'))
end

#sizeObject



121
122
123
124
125
126
# File 'lib/ci/queue/redis/base.rb', line 121

def size
  redis.multi do |transaction|
    transaction.llen(key('queue'))
    transaction.zcard(key('running'))
  end.inject(:+)
end

#stop_heartbeat!Object



84
85
86
87
88
89
# File 'lib/ci/queue/redis/base.rb', line 84

def stop_heartbeat!
  return unless heartbeat_enabled?

  heartbeat_state.set(:stop)
  heartbeat_process.shutdown!
end

#test_failedObject



181
182
183
# File 'lib/ci/queue/redis/base.rb', line 181

def test_failed
  redis.get(key('test_failed_count')).to_i
end

#to_aObject



136
137
138
139
140
141
# File 'lib/ci/queue/redis/base.rb', line 136

def to_a
  redis.multi do |transaction|
    transaction.lrange(key('queue'), 0, -1)
    transaction.zrange(key('running'), 0, -1)
  end.flatten.reverse.map { |k| index.fetch(k) }
end

#wait_for_master(timeout: 30) ⇒ Object

Raises:



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ci/queue/redis/base.rb', line 147

def wait_for_master(timeout: 30)
  return true if master?
  return true if queue_initialized?

  (timeout * 10 + 1).to_i.times do
    if queue_initialized?
      return true
    else
      sleep 0.1
    end
  end

  raise LostMaster, "The master worker is still `#{master_status}` after #{timeout} seconds waiting."
end

#with_heartbeat(id) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/ci/queue/redis/base.rb', line 60

def with_heartbeat(id)
  if heartbeat_enabled?
    ensure_heartbeat_thread_alive!
    heartbeat_state.set(:tick, id)
  end

  yield
ensure
  heartbeat_state.set(:reset) if heartbeat_enabled?
end

#workers_countObject



162
163
164
# File 'lib/ci/queue/redis/base.rb', line 162

def workers_count
  redis.scard(key('workers'))
end