canvas_sync 0.22.2 → 0.22.3

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: 9c79e88e0f642fadba95afa7f6b6960a511144ad41d6c6333febc010e7fe2e3e
4
- data.tar.gz: 26ab8638f656d00f93cbd0393d46b5e0a711ab7dfa94d60bf98bcb5fb42a1a7b
3
+ metadata.gz: cc62b25843e2b3e57b83e1ca41b4a0e546666ce6e824a8a799f534ee90fb28c5
4
+ data.tar.gz: 1fb1747c3882282894b0aa4e6fef9d7240977a453da6415e3a31460c55833306
5
5
  SHA512:
6
- metadata.gz: d06042a0cb184a7f1efdfaf3b3e755967d97d13c8ed4a9d436eaedb5f7ebe0de0007ab58e8b6f1ad1fc520d4be1e6c139d81b292c7edb79dd8c6d3b6b491c879
7
- data.tar.gz: cf3240e17d1df90858e1216c7e1ad8db3d92835d9ad10a17a3fc0f85a309058462e13a121ef156dee75b08adc05731f68a486de7c22c3b3b78cfab65395dca0f
6
+ metadata.gz: 3ac70536bc476c02ee262487ff7074493e99c05d585cec78a32cddb5569feb9e5ca6407a7104cf7538ed9030e4cb2401be823171de5f5e261de5bd2aeafbc11f
7
+ data.tar.gz: 31178103ff1168d6a54b622c612260857c4f5841dff32c637a95877a1df3f7c4467221e05c19e7688f1ebf3284d79cc8cf5113208f89ac4299983dcaa2ccf51d
@@ -478,20 +478,22 @@ module CanvasSync::JobBatches
478
478
 
479
479
  # Internal method to cleanup a Redis Hash and related keys
480
480
  def cleanup_redis_index_for(key, suffixes = [""])
481
- if r.hget(k, "created_at").present?
482
- r.multi do |r|
483
- suffixes.each do |suffix|
484
- r.expire(key + suffix, BID_EXPIRE_TTL)
481
+ redis do |r|
482
+ if r.hget(k, "created_at").present?
483
+ r.multi do |r|
484
+ suffixes.each do |suffix|
485
+ r.expire(key + suffix, BID_EXPIRE_TTL)
486
+ end
485
487
  end
486
- end
487
- false
488
- else
489
- r.multi do |r|
490
- suffixes.each do |suffix|
491
- r.unlink(key + suffix)
488
+ false
489
+ else
490
+ r.multi do |r|
491
+ suffixes.each do |suffix|
492
+ r.unlink(key + suffix)
493
+ end
492
494
  end
495
+ true
493
496
  end
494
- true
495
497
  end
496
498
  end
497
499
 
@@ -499,14 +501,16 @@ module CanvasSync::JobBatches
499
501
  def cleanup_redis_index!
500
502
  suffixes = ["", "-callbacks-complete", "-callbacks-success", "-failed", "-dead", "-batches-success", "-batches-complete", "-batches-failed", "-bids", "-jids", "-pending_callbacks"]
501
503
 
502
- cleanup_index = ->(index) {
503
- r.zrangebyscore(index, "0", BID_EXPIRE_TTL.seconds.ago.to_i).each do |bid|
504
- r.zrem(index, bid) if cleanup_redis_index_for("BID-#{bid}", suffixes)
505
- end
506
- }
504
+ redis do |r|
505
+ cleanup_index = ->(index) {
506
+ r.zrangebyscore(index, "0", BID_EXPIRE_TTL.seconds.ago.to_i).each do |bid|
507
+ r.zrem(index, bid) if cleanup_redis_index_for("BID-#{bid}", suffixes)
508
+ end
509
+ }
507
510
 
508
- cleanup_index.call("BID-ROOT-bids")
509
- cleanup_index.call("batches")
511
+ cleanup_index.("BID-ROOT-bids")
512
+ cleanup_index.("batches")
513
+ end
510
514
  end
511
515
 
512
516
  def redis(&blk)
@@ -122,14 +122,27 @@ module CanvasSync::JobUniqueness
122
122
 
123
123
  args = @context_data[:args] || []
124
124
  kwargs = @context_data[:kwargs] || {}
125
+ hash = config[:hash]
125
126
  if config[:hash].is_a?(Proc)
126
- base_key << config[:hash].call(*args, **kwargs)
127
+ hash = config[:hash].call(*args, **kwargs)
127
128
  elsif config[:hash].nil?
128
- base_key << OpenSSL::Digest::MD5.hexdigest(JSON.dump([ args, kwargs.sort]))
129
- else
130
- base_key << config[:hash]
129
+ hash = [*args, kwargs]
131
130
  end
132
131
 
132
+ hash = ":#{hash}" if hash.is_a?(Symbol)
133
+
134
+ if hash && !hash.is_a?(String)
135
+ hash = Array(hash)
136
+
137
+ # Normalize the hash to ensure that the order of any Hash keys don't matter
138
+ hash = normalize_hash_chunk(hash)
139
+
140
+ normalized = ActiveJob::Arguments.serialize(hash)
141
+ hash = OpenSSL::Digest::MD5.hexdigest(JSON.dump(normalized))
142
+ end
143
+
144
+ base_key << hash if hash
145
+
133
146
  base_key.join(":")
134
147
  end
135
148
  end
@@ -173,5 +186,14 @@ module CanvasSync::JobUniqueness
173
186
 
174
187
  private
175
188
 
189
+ def normalize_hash_chunk(chunk)
190
+ if chunk.is_a?(Hash)
191
+ chunk.map { |k, v| [k, normalize_hash_chunk(v)] }.sort.to_h
192
+ elsif chunk.is_a?(Array)
193
+ chunk.map { |c| normalize_hash_chunk(c) }
194
+ else
195
+ chunk
196
+ end
197
+ end
176
198
  end
177
199
  end
@@ -1,3 +1,3 @@
1
1
  module CanvasSync
2
- VERSION = "0.22.2".freeze
2
+ VERSION = "0.22.3".freeze
3
3
  end
@@ -24,6 +24,17 @@ RSpec.describe CanvasSync::JobUniqueness::LockContext do
24
24
  TestWorker.unique_job_options[:scope] = scope
25
25
  end
26
26
 
27
+ it 'returns matching keys for equal hashes' do
28
+ context_data[:args] = [{ foo: 'bar', bar: 'foo' }]
29
+ key1 = lock_context.base_key
30
+ lock_context.instance_variable_set(:@base_key, nil)
31
+
32
+ context_data[:args] = [{ bar: 'foo', foo: 'bar' }]
33
+ key2 = lock_context.base_key
34
+
35
+ expect(key1).to eq key2
36
+ end
37
+
27
38
  context 'when scope is a Proc' do
28
39
  let(:scope) { ->(queue:) { "blob" } }
29
40
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: canvas_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.22.2
4
+ version: 0.22.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Instructure CustomDev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-16 00:00:00.000000000 Z
11
+ date: 2024-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails