canvas_sync 0.17.23.beta7 → 0.17.23.beta8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ffd1eae28ffc49cdba7673f1648fbf82853a57768697df38d2a100c05c7ab3f
4
- data.tar.gz: 4ab9d6f8c7ce264b69fae4da982d16dfedf40e5b1d1f64fbfd40831f16465bdf
3
+ metadata.gz: 9bed2b1c5be095c602aca19968b0155ff48026a1c5136cb65b5f31e5decbcb40
4
+ data.tar.gz: ae6112e2694f0118a43619cf3de053d7eee7ff1873e0dface27b0b3558727132
5
5
  SHA512:
6
- metadata.gz: 9619fbc749887158498c00761798cd0b10018ce5962262f27c9ff69c0615c66ffe1ebfa004bdc2ca7374308e53598ee81175f2372e2cde69527ce4672c288626
7
- data.tar.gz: a9459bd07489e9c68abd692977e6ca1bc01b06d7dd960560f85e7f38bff4240ac8334aff15b91fb7f7a84897c7fcbd828e623d3b098fa34126b1b4e9be255222
6
+ metadata.gz: 6261b328341a102a9ea6c5f91315444604a3556ccbc040fb38122d18db557bd2c5d2cbc7e5b3c74a52d0d48eb0315793232a3cdff0be21964af1c2617d4ae22d
7
+ data.tar.gz: 458a2c7b422c2a769ec961bd75bd8f43e5dde63c9231bbd4aab6e2a400ec79ee74d9dc12d93cdbd2cc26ef69bb3611972b84956f55cfc2e2a89a4fc22ec2eafb
@@ -76,14 +76,12 @@ module CanvasSync
76
76
  def on(event, callback, options = {})
77
77
  return unless Callback::VALID_CALLBACKS.include?(event.to_s)
78
78
  callback_key = "#{@bidkey}-callbacks-#{event}"
79
- redis do |r|
80
- r.multi do
81
- r.sadd(callback_key, JSON.unparse({
82
- callback: callback,
83
- opts: options
84
- }))
85
- r.expire(callback_key, BID_EXPIRE_TTL)
86
- end
79
+ redis.multi do |r|
80
+ r.sadd(callback_key, JSON.unparse({
81
+ callback: callback,
82
+ opts: options
83
+ }))
84
+ r.expire(callback_key, BID_EXPIRE_TTL)
87
85
  end
88
86
  end
89
87
 
@@ -93,16 +91,14 @@ module CanvasSync
93
91
  if !@existing && !@initialized
94
92
  parent_bid = Thread.current[:batch]&.bid
95
93
 
96
- redis do |r|
97
- r.multi do
98
- r.hset(@bidkey, "parent_bid", parent_bid.to_s) if parent_bid
99
- r.expire(@bidkey, BID_EXPIRE_TTL)
94
+ redis.multi do |r|
95
+ r.hset(@bidkey, "parent_bid", parent_bid.to_s) if parent_bid
96
+ r.expire(@bidkey, BID_EXPIRE_TTL)
100
97
 
101
- if parent_bid
102
- r.hincrby("BID-#{parent_bid}", "children", 1)
103
- r.expire("BID-#{parent_bid}", BID_EXPIRE_TTL)
104
- r.zadd("BID-#{parent_bid}-bids", created_at, bid)
105
- end
98
+ if parent_bid
99
+ r.hincrby("BID-#{parent_bid}", "children", 1)
100
+ r.expire("BID-#{parent_bid}", BID_EXPIRE_TTL)
101
+ r.zadd("BID-#{parent_bid}-bids", created_at, bid)
106
102
  end
107
103
  end
108
104
 
@@ -131,15 +127,11 @@ module CanvasSync
131
127
  end
132
128
 
133
129
  def invalidate_all
134
- redis do |r|
135
- r.setex("invalidated-bid-#{bid}", BID_EXPIRE_TTL, 1)
136
- end
130
+ redis.setex("invalidated-bid-#{bid}", BID_EXPIRE_TTL, 1)
137
131
  end
138
132
 
139
133
  def parent_bid
140
- redis do |r|
141
- r.hget(@bidkey, "parent_bid")
142
- end
134
+ redis.hget(@bidkey, "parent_bid")
143
135
  end
144
136
 
145
137
  def parent
@@ -149,7 +141,7 @@ module CanvasSync
149
141
  end
150
142
 
151
143
  def valid?(batch = self)
152
- valid = !redis { |r| r.exists?("invalidated-bid-#{batch.bid}") }
144
+ valid = !redis.exists?("invalidated-bid-#{batch.bid}")
153
145
  batch.parent ? valid && valid?(batch.parent) : valid
154
146
  end
155
147
 
@@ -162,23 +154,19 @@ module CanvasSync
162
154
  let_close!
163
155
  end
164
156
  else
165
- redis do |r|
166
- r.hset(@bidkey, 'keep_open', true)
167
- end
157
+ redis.hset(@bidkey, 'keep_open', true)
168
158
  end
169
159
  end
170
160
 
171
161
  def let_close!
172
- _, failed, pending, children, complete, success = redis do |r|
173
- r.multi do
174
- r.hset(@bidkey, 'keep_open', false)
175
-
176
- r.scard("BID-#{bid}-failed")
177
- r.hincrby("BID-#{bid}", "pending", 0)
178
- r.hincrby("BID-#{bid}", "children", 0)
179
- r.scard("BID-#{bid}-batches-complete")
180
- r.scard("BID-#{bid}-batches-success")
181
- end
162
+ _, failed, pending, children, complete, success = redis.multi do |r|
163
+ r.hset(@bidkey, 'keep_open', false)
164
+
165
+ r.scard("BID-#{bid}-failed")
166
+ r.hincrby("BID-#{bid}", "pending", 0)
167
+ r.hincrby("BID-#{bid}", "children", 0)
168
+ r.scard("BID-#{bid}-batches-complete")
169
+ r.scard("BID-#{bid}-batches-success")
182
170
  end
183
171
 
184
172
  all_success = pending.to_i.zero? && children == success
@@ -211,18 +199,14 @@ module CanvasSync
211
199
 
212
200
  def flush_pending_attrs
213
201
  super
214
- redis do |r|
215
- r.zadd("batches", created_at, bid)
216
- end
202
+ redis.zadd("batches", created_at, bid)
217
203
  end
218
204
 
219
205
  private
220
206
 
221
207
  def assert_batch_is_open
222
208
  unless defined?(@closed)
223
- redis do |r|
224
- @closed = r.hget(@bidkey, 'success') == 'true'
225
- end
209
+ @closed = redis.hget(@bidkey, 'success') == 'true'
226
210
  end
227
211
  raise "Cannot add jobs to Batch #{} bid - it has already entered the callback-stage" if @closed
228
212
  end
@@ -234,7 +218,7 @@ module CanvasSync
234
218
  redis do |r|
235
219
  tme = Time.now.utc.to_f
236
220
  added = r.zadd(@bidkey + "-jids", jids.map{|jid| [tme, jid] }, nx: true)
237
- r.multi do
221
+ r.multi do |r|
238
222
  r.hincrby(@bidkey, "pending", added)
239
223
  r.hincrby(@bidkey, "job_count", added)
240
224
  r.expire(@bidkey, BID_EXPIRE_TTL)
@@ -283,11 +267,9 @@ module CanvasSync
283
267
  end
284
268
 
285
269
  if parent_bid
286
- redis do |r|
287
- r.multi do
288
- r.sadd("BID-#{parent_bid}-dead", jid)
289
- r.expire("BID-#{parent_bid}-dead", BID_EXPIRE_TTL)
290
- end
270
+ redis.multi do
271
+ r.sadd("BID-#{parent_bid}-dead", jid)
272
+ r.expire("BID-#{parent_bid}-dead", BID_EXPIRE_TTL)
291
273
  end
292
274
  end
293
275
 
@@ -413,8 +395,21 @@ module CanvasSync
413
395
  cleanup_redis(bid)
414
396
  end
415
397
 
416
- def redis(*args, &blk)
417
- defined?(::Sidekiq) ? ::Sidekiq.redis(*args, &blk) : nil # TODO
398
+ def redis(&blk)
399
+ return RedisProxy.new unless block_given?
400
+
401
+ if Thread.current[:job_batches_redis]
402
+ yield Thread.current[:job_batches_redis]
403
+ elsif defined?(::Sidekiq)
404
+ ::Sidekiq.redis do |r|
405
+ Thread.current[:job_batches_redis] = r
406
+ yield r
407
+ ensure
408
+ Thread.current[:job_batches_redis] = nil
409
+ end
410
+ else
411
+ # TODO
412
+ end
418
413
  end
419
414
 
420
415
  def logger
@@ -433,6 +428,42 @@ module CanvasSync
433
428
  end
434
429
  end
435
430
  end
431
+
432
+ class RedisProxy
433
+ def multi(*args, &block)
434
+ Batch.redis do |r|
435
+ if block.arity == 1
436
+ r.multi(*args) do
437
+ block.call(r)
438
+ end
439
+ else
440
+ r.multi(*args, &block)
441
+ end
442
+ end
443
+ end
444
+
445
+ def pipelined(*args, &block)
446
+ Batch.redis do |r|
447
+ if block.arity == 1
448
+ r.pipelined(*args) do
449
+ block.call(r)
450
+ end
451
+ else
452
+ r.pipelined(*args, &block)
453
+ end
454
+ end
455
+ end
456
+
457
+ def method_missing(method_name, *arguments, &block)
458
+ Batch.redis do |r|
459
+ r.send(method_name, *arguments, &block)
460
+ end
461
+ end
462
+
463
+ def respond_to_missing?(method_name, include_private = false)
464
+ super || Redis.method_defined?(method_name)
465
+ end
466
+ end
436
467
  end
437
468
 
438
469
  ActiveJob::Base.include BatchAwareJob
@@ -64,18 +64,14 @@ module CanvasSync
64
64
  let_close!
65
65
  end
66
66
  else
67
- redis do |r|
68
- r.hset(redis_key, 'keep_open', true)
69
- end
67
+ redis.hset(redis_key, 'keep_open', true)
70
68
  end
71
69
  end
72
70
 
73
71
  def let_close!
74
- _, active_count = redis do |r|
75
- r.multi do
76
- r.hset(redis_key, 'keep_open', false)
77
- r.hincrby(redis_key, "active_count", 0)
78
- end
72
+ _, active_count = redis.multi do |r|
73
+ r.hset(redis_key, 'keep_open', false)
74
+ r.hincrby(redis_key, "active_count", 0)
79
75
  end
80
76
 
81
77
  if active_count == 0 && pending_count == 0
@@ -95,35 +91,38 @@ module CanvasSync
95
91
  end
96
92
 
97
93
  def active_count
98
- redis do |r|
99
- r.hincrby(redis_key, "active_count", 0)
100
- end
94
+ redis.hincrby(redis_key, "active_count", 0)
101
95
  end
102
96
 
103
97
  def pending_count
104
98
  jobs_key = "#{redis_key}-jobs"
105
99
  order = self.order || 'fifo'
106
- redis do |r|
107
- case order.to_sym
108
- when :fifo, :lifo
109
- r.llen(jobs_key)
110
- when :random
111
- r.scard(jobs_key)
112
- when :priority
113
- r.zcard(jobs_key)
114
- end
100
+ case order.to_sym
101
+ when :fifo, :lifo
102
+ redis.llen(jobs_key)
103
+ when :random
104
+ redis.scard(jobs_key)
105
+ when :priority
106
+ redis.zcard(jobs_key)
115
107
  end
116
108
  end
117
109
 
118
110
  def job_checked_in(status, options)
119
- active_count = redis do |r|
111
+ active_count, pending_count = redis do |r|
120
112
  return unless r.exists?(redis_key)
121
- r.hincrby(redis_key, "active_count", -1)
113
+
114
+ # Make sure this is loaded outside of the pipeline
115
+ self.order
116
+
117
+ redis.multi do
118
+ r.hincrby(redis_key, "active_count", -1)
119
+ self.pending_count
120
+ end
122
121
  end
123
122
 
124
123
  added_count = refill_allotment
125
- if active_count == 0 && added_count == 0
126
- if clean_when_empty && redis {|r| r.hget(redis_key, 'keep_open') } != 'true'
124
+ if active_count == 0 && added_count == 0 && pending_count == 0
125
+ if clean_when_empty && redis.hget(redis_key, 'keep_open') != 'true'
127
126
  cleanup_redis
128
127
  end
129
128
  end
@@ -175,18 +174,16 @@ module CanvasSync
175
174
  job_json = JSON.unparse(ActiveJob::Arguments.serialize([job_desc]))
176
175
  order = self.order
177
176
 
178
- redis do |r|
179
- r.multi do
180
- case order.to_sym
181
- when :fifo, :lifo
182
- r.rpush(jobs_key, job_json)
183
- when :random
184
- r.sadd(jobs_key, job_json)
185
- when :priority
186
- r.zadd(jobs_key, job_desc[:priority] || 0, job_json)
187
- end
188
- r.expire(jobs_key, Batch::BID_EXPIRE_TTL)
177
+ redis.multi do |r|
178
+ case order.to_sym
179
+ when :fifo, :lifo
180
+ r.rpush(jobs_key, job_json)
181
+ when :random
182
+ r.sadd(jobs_key, job_json)
183
+ when :priority
184
+ r.zadd(jobs_key, job_desc[:priority] || 0, job_json)
189
185
  end
186
+ r.expire(jobs_key, Batch::BID_EXPIRE_TTL)
190
187
  end
191
188
  end
192
189
 
@@ -194,18 +191,15 @@ module CanvasSync
194
191
  jobs_key = "#{redis_key}-jobs"
195
192
  order = self.order
196
193
 
197
- job_json = nil
198
- redis do |r|
199
- job_json = case order.to_sym
200
- when :fifo
201
- r.lpop(jobs_key)
202
- when :lifo
203
- r.rpop(jobs_key)
204
- when :random
205
- r.spop(jobs_key)
206
- when :priority
207
- r.zpopmax(jobs_key)
208
- end
194
+ job_json = case order.to_sym
195
+ when :fifo
196
+ redis.lpop(jobs_key)
197
+ when :lifo
198
+ redis.rpop(jobs_key)
199
+ when :random
200
+ redis.spop(jobs_key)
201
+ when :priority
202
+ redis.zpopmax(jobs_key)
209
203
  end
210
204
 
211
205
  return nil unless job_json.present?
@@ -218,13 +212,9 @@ module CanvasSync
218
212
  end
219
213
  delegate :redis, to: :class
220
214
 
221
- protected
222
-
223
215
  def flush_pending_attrs
224
216
  super
225
- redis do |r|
226
- r.zadd("pools", created_at, pid)
227
- end
217
+ redis.zadd("pools", created_at, pid)
228
218
  end
229
219
 
230
220
  private
@@ -1,3 +1,3 @@
1
1
  module CanvasSync
2
- VERSION = "0.17.23.beta7".freeze
2
+ VERSION = "0.17.23.beta8".freeze
3
3
  end