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
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,
reconnect_attempts: reconnect_attempts,
middlewares: custom_middlewares,
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_config ⇒ Object
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_middlewares ⇒ Object
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
104
105
106
|
# File 'lib/ci/queue/redis/base.rb', line 104
def exhausted?
queue_initialized? && size == 0
end
|
#expired? ⇒ 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
true
end
end
|
#increment_test_failed ⇒ Object
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
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
|
#progress ⇒ Object
143
144
145
|
# File 'lib/ci/queue/redis/base.rb', line 143
def progress
total - size
end
|
#queue_initialized? ⇒ 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
173
174
175
|
# File 'lib/ci/queue/redis/base.rb', line 173
def queue_initializing?
master_status == 'setup'
end
|
#reconnect_attempts ⇒ Object
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
|
#remaining ⇒ Object
128
129
130
|
# File 'lib/ci/queue/redis/base.rb', line 128
def remaining
redis.llen(key('queue'))
end
|
#running ⇒ Object
132
133
134
|
# File 'lib/ci/queue/redis/base.rb', line 132
def running
redis.zcard(key('running'))
end
|
#size ⇒ Object
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_failed ⇒ Object
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_a ⇒ Object
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
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_count ⇒ Object
162
163
164
|
# File 'lib/ci/queue/redis/base.rb', line 162
def workers_count
redis.scard(key('workers'))
end
|