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.
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/ci/queue/redis/base.rb', line 30
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
71
72
73
74
75
|
# File 'lib/ci/queue/redis/base.rb', line 71
def boot_heartbeat_process!
return unless heartbeat_enabled?
heartbeat_process.boot!
end
|
#created_at=(timestamp) ⇒ Object
110
111
112
|
# File 'lib/ci/queue/redis/base.rb', line 110
def created_at=(timestamp)
redis.setnx(key('created-at'), timestamp)
end
|
#custom_config ⇒ Object
84
85
86
87
88
89
|
# File 'lib/ci/queue/redis/base.rb', line 84
def custom_config
return unless config.debug_log
require 'logger'
{ debug_log: Logger.new(config.debug_log) }
end
|
#custom_middlewares ⇒ Object
91
92
93
94
95
|
# File 'lib/ci/queue/redis/base.rb', line 91
def custom_middlewares
return unless config.debug_log
[RedisInstrumentation]
end
|
#ensure_heartbeat_thread_alive! ⇒ Object
64
65
66
67
68
69
|
# File 'lib/ci/queue/redis/base.rb', line 64
def ensure_heartbeat_thread_alive!
return unless heartbeat_enabled?
return if @heartbeat_thread&.alive?
@heartbeat_thread = Thread.start { heartbeat }
end
|
#exhausted? ⇒ Boolean
97
98
99
|
# File 'lib/ci/queue/redis/base.rb', line 97
def exhausted?
queue_initialized? && size == 0
end
|
#expired? ⇒ Boolean
101
102
103
104
105
106
107
108
|
# File 'lib/ci/queue/redis/base.rb', line 101
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
170
171
172
|
# File 'lib/ci/queue/redis/base.rb', line 170
def increment_test_failed
redis.incr(key('test_failed_count'))
end
|
#max_test_failed? ⇒ Boolean
178
179
180
181
182
|
# File 'lib/ci/queue/redis/base.rb', line 178
def max_test_failed?
return false if config.max_test_failed.nil?
test_failed >= config.max_test_failed
end
|
#progress ⇒ Object
136
137
138
|
# File 'lib/ci/queue/redis/base.rb', line 136
def progress
total - size
end
|
#queue_initialized? ⇒ Boolean
159
160
161
162
163
164
|
# File 'lib/ci/queue/redis/base.rb', line 159
def queue_initialized?
@queue_initialized ||= begin
status = master_status
status == 'ready' || status == 'finished'
end
end
|
#queue_initializing? ⇒ Boolean
166
167
168
|
# File 'lib/ci/queue/redis/base.rb', line 166
def queue_initializing?
master_status == 'setup'
end
|
#reconnect_attempts ⇒ Object
47
48
49
50
51
|
# File 'lib/ci/queue/redis/base.rb', line 47
def reconnect_attempts
return [] if ENV["CI_QUEUE_DISABLE_RECONNECT_ATTEMPTS"]
[0, 0, 0.1, 0.5, 1, 3, 5]
end
|
#remaining ⇒ Object
121
122
123
|
# File 'lib/ci/queue/redis/base.rb', line 121
def remaining
redis.llen(key('queue'))
end
|
#running ⇒ Object
125
126
127
|
# File 'lib/ci/queue/redis/base.rb', line 125
def running
redis.zcard(key('running'))
end
|
#size ⇒ Object
114
115
116
117
118
119
|
# File 'lib/ci/queue/redis/base.rb', line 114
def size
redis.multi do |transaction|
transaction.llen(key('queue'))
transaction.zcard(key('running'))
end.inject(:+)
end
|
#stop_heartbeat! ⇒ Object
77
78
79
80
81
82
|
# File 'lib/ci/queue/redis/base.rb', line 77
def stop_heartbeat!
return unless heartbeat_enabled?
heartbeat_state.set(:stop)
heartbeat_process.shutdown!
end
|
#test_failed ⇒ Object
174
175
176
|
# File 'lib/ci/queue/redis/base.rb', line 174
def test_failed
redis.get(key('test_failed_count')).to_i
end
|
#to_a ⇒ Object
129
130
131
132
133
134
|
# File 'lib/ci/queue/redis/base.rb', line 129
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/ci/queue/redis/base.rb', line 140
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
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/ci/queue/redis/base.rb', line 53
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
155
156
157
|
# File 'lib/ci/queue/redis/base.rb', line 155
def workers_count
redis.scard(key('workers'))
end
|