Class: CI::Queue::Redis::Base
- Inherits:
-
Object
- Object
- CI::Queue::Redis::Base
show all
- Includes:
- Common
- Defined in:
- lib/ci/queue/redis/base.rb
Defined Under Namespace
Modules: RedisInstrumentation
Classes: HeartbeatProcess, State
Constant Summary
collapse
- TEN_MINUTES =
60 * 10
- CONNECTION_ERRORS =
[
::Redis::BaseConnectionError,
::SocketError, ].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
|
# 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,
reconnect_attempts: reconnect_attempts,
middlewares: custom_middlewares,
custom: custom_config,
)
else
@redis = ::Redis.new(url: redis_url)
end
end
|
Instance Method Details
#boot_heartbeat_process! ⇒ Object
73
74
75
76
77
|
# File 'lib/ci/queue/redis/base.rb', line 73
def boot_heartbeat_process!
return unless heartbeat_enabled?
heartbeat_process.boot!
end
|
#created_at=(timestamp) ⇒ Object
112
113
114
|
# File 'lib/ci/queue/redis/base.rb', line 112
def created_at=(timestamp)
redis.setnx(key('created-at'), timestamp)
end
|
#custom_config ⇒ Object
86
87
88
89
90
91
|
# File 'lib/ci/queue/redis/base.rb', line 86
def custom_config
return unless config.debug_log
require 'logger'
{ debug_log: Logger.new(config.debug_log) }
end
|
#custom_middlewares ⇒ Object
93
94
95
96
97
|
# File 'lib/ci/queue/redis/base.rb', line 93
def custom_middlewares
return unless config.debug_log
[RedisInstrumentation]
end
|
#ensure_heartbeat_thread_alive! ⇒ Object
66
67
68
69
70
71
|
# File 'lib/ci/queue/redis/base.rb', line 66
def ensure_heartbeat_thread_alive!
return unless heartbeat_enabled?
return if @heartbeat_thread&.alive?
@heartbeat_thread = Thread.start { heartbeat }
end
|
#exhausted? ⇒ Boolean
99
100
101
|
# File 'lib/ci/queue/redis/base.rb', line 99
def exhausted?
queue_initialized? && size == 0
end
|
#expired? ⇒ Boolean
103
104
105
106
107
108
109
110
|
# File 'lib/ci/queue/redis/base.rb', line 103
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
true
end
end
|
#increment_test_failed ⇒ Object
172
173
174
|
# File 'lib/ci/queue/redis/base.rb', line 172
def increment_test_failed
redis.incr(key('test_failed_count'))
end
|
#max_test_failed? ⇒ Boolean
180
181
182
183
184
|
# File 'lib/ci/queue/redis/base.rb', line 180
def max_test_failed?
return false if config.max_test_failed.nil?
test_failed >= config.max_test_failed
end
|
#progress ⇒ Object
138
139
140
|
# File 'lib/ci/queue/redis/base.rb', line 138
def progress
total - size
end
|
#queue_initialized? ⇒ Boolean
161
162
163
164
165
166
|
# File 'lib/ci/queue/redis/base.rb', line 161
def queue_initialized?
@queue_initialized ||= begin
status = master_status
status == 'ready' || status == 'finished'
end
end
|
#queue_initializing? ⇒ Boolean
168
169
170
|
# File 'lib/ci/queue/redis/base.rb', line 168
def queue_initializing?
master_status == 'setup'
end
|
#reconnect_attempts ⇒ Object
49
50
51
52
53
|
# File 'lib/ci/queue/redis/base.rb', line 49
def reconnect_attempts
return [] if ENV["CI_QUEUE_DISABLE_RECONNECT_ATTEMPTS"]
[0, 0, 0.1, 0.5, 1, 3, 5]
end
|
#remaining ⇒ Object
123
124
125
|
# File 'lib/ci/queue/redis/base.rb', line 123
def remaining
redis.llen(key('queue'))
end
|
#running ⇒ Object
127
128
129
|
# File 'lib/ci/queue/redis/base.rb', line 127
def running
redis.zcard(key('running'))
end
|
#size ⇒ Object
116
117
118
119
120
121
|
# File 'lib/ci/queue/redis/base.rb', line 116
def size
redis.multi do |transaction|
transaction.llen(key('queue'))
transaction.zcard(key('running'))
end.inject(:+)
end
|
#stop_heartbeat! ⇒ Object
79
80
81
82
83
84
|
# File 'lib/ci/queue/redis/base.rb', line 79
def stop_heartbeat!
return unless heartbeat_enabled?
heartbeat_state.set(:stop)
heartbeat_process.shutdown!
end
|
#test_failed ⇒ Object
176
177
178
|
# File 'lib/ci/queue/redis/base.rb', line 176
def test_failed
redis.get(key('test_failed_count')).to_i
end
|
#to_a ⇒ Object
131
132
133
134
135
136
|
# File 'lib/ci/queue/redis/base.rb', line 131
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/ci/queue/redis/base.rb', line 142
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
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/ci/queue/redis/base.rb', line 55
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_count ⇒ Object
157
158
159
|
# File 'lib/ci/queue/redis/base.rb', line 157
def workers_count
redis.scard(key('workers'))
end
|