sidekiq-unique-jobs 6.0.5 → 6.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of sidekiq-unique-jobs might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d4687b0a098ec2c297ad6d46a8b9c3fa50d024d79fe0f241a9bd635bb0c99fd
4
- data.tar.gz: 2539c5773a5da7bfd1f3ce09bc7b217a9f16b90bc2775e9f64406e68ba3ae509
3
+ metadata.gz: f82a371b5f835c77c75c753533472d6b7654cb5f403ebeb686502046a0884681
4
+ data.tar.gz: 982ad72a4c7f7a164c9dadc7e5979b26562b53d7a934a6a04c2baf0bd35f7732
5
5
  SHA512:
6
- metadata.gz: ebf3ce0a04fe2d9c9ced23bfdd5c3b0cb58efb559bc81c4c2db96b8406ab83c6d86b7c14f46d2954ac1ea25d92a496334f338c559173b5125cfe3ccb01d209a9
7
- data.tar.gz: ed0913b11a73d2036159a36d087044d53c9b95fe1b4c239f1181e1dffc681e1bafa9b5eed0449f92967909846afce3af6355c86195027993bf35b60cff8385d9
6
+ metadata.gz: 6e6e9740d60600bdf0ab87b1c24dca5c680f866bbdee55449dcfe2dd143956b85ecf8db33c5bbdbc1c4ec59dcd9ab8ee481af06c93b234419612005ce1628f62
7
+ data.tar.gz: 58e048744c22ebf4b288863919dad091a122f9511b7d578b06327060e88e4c377542ebae00311381ec2cbda4276d8c6e08cf51033c6369532499aa2c2e49815b
@@ -4,10 +4,7 @@ module SidekiqUniqueJobs
4
4
  # Lock manager class that handles all the various locks
5
5
  #
6
6
  # @author Mikael Henriksson <mikael@zoolutions.se>
7
- class Locksmith # rubocop:disable ClassLength
8
- API_VERSION = '1'
9
- EXPIRES_IN = 10
10
-
7
+ class Locksmith
11
8
  include SidekiqUniqueJobs::Connection
12
9
 
13
10
  # @param [Hash] item a Sidekiq job hash
@@ -23,26 +20,6 @@ module SidekiqUniqueJobs
23
20
  @redis_pool = redis_pool
24
21
  end
25
22
 
26
- # Creates the necessary keys in redis to attempt a lock
27
- # @return [String] the Sidekiq job_id
28
- def create
29
- Scripts.call(
30
- :create,
31
- redis_pool,
32
- keys: [exists_key, grabbed_key, available_key, version_key, UNIQUE_SET, unique_digest],
33
- argv: [jid, expiration, API_VERSION, concurrency],
34
- )
35
- end
36
-
37
- def expire
38
- Scripts.call(
39
- :expire,
40
- redis_pool,
41
- keys: [exists_key, available_key, version_key],
42
- argv: [expiration, jid],
43
- )
44
- end
45
-
46
23
  # Checks if the exists key is created in redis
47
24
  # @return [true, false]
48
25
  def exists?
@@ -78,48 +55,49 @@ module SidekiqUniqueJobs
78
55
  # @yield the block to execute if a lock is successful
79
56
  # @return the Sidekiq job_id (jid)
80
57
  def lock(timeout = nil, &block)
81
- create
58
+ Scripts.call(:lock, redis_pool,
59
+ keys: [exists_key, grabbed_key, available_key, UNIQUE_SET, unique_digest],
60
+ argv: [jid, expiration])
82
61
 
83
62
  grab_token(timeout) do |token|
84
- expire
85
63
  touch_grabbed_token(token)
86
64
  return_token_or_block_value(token, &block)
87
65
  end
88
66
  end
89
67
  alias wait lock
90
68
 
91
- # Removes the lock keys from Redis
69
+ # Removes the lock keys from Redis if locked by the provided jid/token
92
70
  # @return [false] unless locked?
93
71
  # @return [String] Sidekiq job_id (jid) if successful
94
72
  def unlock(token = nil)
95
- return false unless locked?(token)
96
- signal(token)
97
- end
98
-
99
- # Checks if this instance is considered locked
100
- # @param [String] token the unique token to check for a lock.
101
- # nil will default to the jid provided in the initializer
102
- # @return [true, false]
103
- def locked?(token = nil)
104
73
  token ||= jid
105
- redis(redis_pool) { |conn| conn.hexists(grabbed_key, token) }
74
+ return false unless locked?(token)
75
+ unlock!(token)
106
76
  end
107
77
 
108
- # Signal that the token should be released
109
- # @param [String] token the unique token to check for a lock.
110
- # nil will default to the jid provided in the initializer.
111
- # @return [Integer] the number of available lock resources
112
- def signal(token = nil)
78
+ # Removes the lock keys from Redis
79
+ # @return [false] unless locked?
80
+ # @return [String] Sidekiq job_id (jid) if successful
81
+ def unlock!(token = nil)
113
82
  token ||= jid
114
83
 
115
84
  Scripts.call(
116
- :signal,
85
+ :unlock,
117
86
  redis_pool,
118
87
  keys: [exists_key, grabbed_key, available_key, version_key, UNIQUE_SET, unique_digest],
119
88
  argv: [token, expiration],
120
89
  )
121
90
  end
122
91
 
92
+ # Checks if this instance is considered locked
93
+ # @param [String] token the unique token to check for a lock.
94
+ # nil will default to the jid provided in the initializer
95
+ # @return [true, false]
96
+ def locked?(token = nil)
97
+ token ||= jid
98
+ redis(redis_pool) { |conn| conn.hexists(grabbed_key, token) }
99
+ end
100
+
123
101
  private
124
102
 
125
103
  attr_reader :concurrency, :unique_digest, :expiration, :jid, :redis_pool
@@ -148,7 +126,7 @@ module SidekiqUniqueJobs
148
126
  begin
149
127
  yield token
150
128
  ensure
151
- signal(token)
129
+ unlock(token)
152
130
  end
153
131
  end
154
132
 
@@ -44,7 +44,7 @@ module SidekiqUniqueJobs
44
44
  # @return [Integer] the number of keys deleted
45
45
  def del(pattern = SCAN_PATTERN, count = 0)
46
46
  raise ArgumentError, 'Please provide a number of keys to delete greater than zero' if count.zero?
47
- pattern = "#{pattern}:*" unless pattern.end_with?(':*')
47
+ pattern = suffix(pattern)
48
48
 
49
49
  log_debug { "Deleting keys by: #{pattern}" }
50
50
  keys, time = timed { keys(pattern, count) }
@@ -87,6 +87,11 @@ module SidekiqUniqueJobs
87
87
  "#{unique_prefix}:#{key}"
88
88
  end
89
89
 
90
+ def suffix(key)
91
+ return "#{key}*" unless key.end_with?(':*')
92
+ key
93
+ end
94
+
90
95
  def unique_prefix
91
96
  SidekiqUniqueJobs.config.unique_prefix
92
97
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SidekiqUniqueJobs
4
- VERSION = '6.0.5'
4
+ VERSION = '6.0.6'
5
5
  end
@@ -3,14 +3,20 @@
3
3
  local exists_key = KEYS[1]
4
4
  local grabbed_key = KEYS[2]
5
5
  local available_key = KEYS[3]
6
- local version_key = KEYS[4]
7
- local unique_keys = KEYS[5]
8
- local unique_digest = KEYS[6]
6
+ local unique_keys = KEYS[4]
7
+ local unique_digest = KEYS[5]
9
8
 
10
9
  local job_id = ARGV[1]
11
10
  local expiration = tonumber(ARGV[2])
12
- local api_version = ARGV[3]
13
- local concurrency = tonumber(ARGV[4])
11
+
12
+ local function current_time()
13
+ local time = redis.call('time')
14
+ local s = time[1]
15
+ local ms = time[2]
16
+ local number = tonumber((s .. '.' .. ms))
17
+
18
+ return number
19
+ end
14
20
 
15
21
  -- redis.log(redis.LOG_DEBUG, "create.lua - investigate possibility of locking jid: " .. job_id)
16
22
 
@@ -35,24 +41,18 @@ if old_token then
35
41
  end
36
42
  ----------------------------------------------------------------
37
43
 
38
- -- redis.log(redis.LOG_DEBUG, "create.lua - creating locks for jid: " .. job_id)
39
44
  redis.call('SADD', unique_keys, unique_digest)
40
- redis.call('EXPIRE', exists_key, 5)
41
45
  redis.call('DEL', grabbed_key)
46
+ -- TODO: Move this to LUA when redis 3.2 is the least supported
47
+ -- redis.call('HSET', grabbed_key, job_id, current_time())
48
+ ---------------------------------------------------------------
42
49
  redis.call('DEL', available_key)
50
+ redis.call('RPUSH', available_key, job_id)
43
51
 
44
- if concurrency and concurrency > 1 then
45
- for index = 1, concurrency do
46
- redis.call('RPUSH', available_key, index)
47
- end
48
- else
49
- redis.call('RPUSH', available_key, job_id)
52
+ if expiration then
53
+ redis.call('EXPIRE', available_key, expiration)
54
+ redis.call('EXPIRE', exists_key, expiration)
55
+ redis.call('EXPIRE', grabbed_key, expiration)
50
56
  end
51
57
 
52
- -- redis.log(redis.LOG_DEBUG, "create.lua - persisting locks for jid: " .. job_id)
53
- redis.call('PERSIST', exists_key)
54
-
55
- redis.call('GETSET', version_key, api_version)
56
-
57
-
58
58
  return job_id
@@ -17,15 +17,15 @@ if expiration then
17
17
  redis.call('SREM', unique_keys, unique_digest)
18
18
  redis.call('EXPIRE', exists_key, expiration)
19
19
  redis.call('EXPIRE', available_key, expiration)
20
- redis.call('EXPIRE', version_key, expiration)
20
+ redis.call('EXPIRE', version_key, expiration) -- TODO: Legacy support (Remove in v6.1)
21
21
  redis.call('EXPIRE', unique_digest, expiration) -- TODO: Legacy support (Remove in v6.1)
22
22
  else
23
23
  redis.call('DEL', exists_key)
24
24
  redis.call('SREM', unique_keys, unique_digest)
25
25
  redis.call('DEL', grabbed_key)
26
26
  redis.call('DEL', available_key)
27
- redis.call('DEL', version_key)
28
- redis.call('DEL', 'uniquejobs') -- TODO: Old job hash, just drop the darn thing
27
+ redis.call('DEL', version_key) -- TODO: Legacy support (Remove in v6.1)
28
+ redis.call('DEL', 'uniquejobs') -- TODO: Old job hash, just drop the darn thing (Remove in v6.1)
29
29
  redis.call('DEL', unique_digest) -- TODO: Legacy support (Remove in v6.1)
30
30
  end
31
31
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-unique-jobs
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.5
4
+ version: 6.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-07 00:00:00.000000000 Z
11
+ date: 2018-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -310,14 +310,12 @@ files:
310
310
  - lib/sidekiq_unique_jobs/web/views/unique_digest.erb
311
311
  - lib/sidekiq_unique_jobs/web/views/unique_digests.erb
312
312
  - redis/acquire_lock.lua
313
- - redis/create.lua
314
313
  - redis/delete.lua
315
314
  - redis/delete_by_digest.lua
316
315
  - redis/delete_job_by_digest.lua
317
- - redis/expire.lua
318
- - redis/release_lock.lua
316
+ - redis/lock.lua
319
317
  - redis/release_stale_locks.lua
320
- - redis/signal.lua
318
+ - redis/unlock.lua
321
319
  - sidekiq-unique-jobs.gemspec
322
320
  homepage: https://github.com/mhenrixon/sidekiq-unique-jobs
323
321
  licenses:
@@ -339,7 +337,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
339
337
  version: '0'
340
338
  requirements: []
341
339
  rubyforge_project:
342
- rubygems_version: 2.7.6
340
+ rubygems_version: 2.7.7
343
341
  signing_key:
344
342
  specification_version: 4
345
343
  summary: Uniqueness for Sidekiq Jobs
data/redis/expire.lua DELETED
@@ -1,15 +0,0 @@
1
- -- redis.replicate_commands();
2
-
3
- local exists_key = KEYS[1]
4
- local available_key = KEYS[2]
5
- local version_key = KEYS[3]
6
-
7
- local expiration = tonumber(ARGV[1])
8
- local job_id = ARGV[2]
9
-
10
- if expiration then
11
- -- redis.log(redis.LOG_DEBUG, "expire.lua - expiring locks for job_id: " .. job_id)
12
- redis.call('EXPIRE', available_key, expiration)
13
- redis.call('EXPIRE', exists_key, expiration)
14
- redis.call('EXPIRE', version_key, expiration)
15
- end
@@ -1,16 +0,0 @@
1
- local unique_key = KEYS[1]
2
- local job_id = ARGV[1]
3
- local stored_jid = redis.pcall('get', unique_key)
4
-
5
- if stored_jid then
6
- if stored_jid == job_id or stored_jid == '2' then
7
- redis.pcall('del', unique_key)
8
- redis.pcall('HDEL', 'uniquejobs', job_id)
9
- return 1
10
- else
11
- return 0
12
- end
13
- else
14
- return -1
15
- end
16
-