sidekiq-unique-jobs 6.0.25 → 7.1.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

Files changed (127) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +840 -41
  3. data/README.md +814 -284
  4. data/lib/sidekiq_unique_jobs/batch_delete.rb +123 -0
  5. data/lib/sidekiq_unique_jobs/changelog.rb +78 -0
  6. data/lib/sidekiq_unique_jobs/cli.rb +34 -31
  7. data/lib/sidekiq_unique_jobs/config.rb +275 -0
  8. data/lib/sidekiq_unique_jobs/connection.rb +6 -5
  9. data/lib/sidekiq_unique_jobs/constants.rb +45 -25
  10. data/lib/sidekiq_unique_jobs/core_ext.rb +80 -0
  11. data/lib/sidekiq_unique_jobs/deprecation.rb +35 -0
  12. data/lib/sidekiq_unique_jobs/digests.rb +71 -100
  13. data/lib/sidekiq_unique_jobs/exceptions.rb +87 -12
  14. data/lib/sidekiq_unique_jobs/job.rb +41 -12
  15. data/lib/sidekiq_unique_jobs/json.rb +40 -0
  16. data/lib/sidekiq_unique_jobs/key.rb +93 -0
  17. data/lib/sidekiq_unique_jobs/lock/base_lock.rb +100 -79
  18. data/lib/sidekiq_unique_jobs/lock/client_validator.rb +28 -0
  19. data/lib/sidekiq_unique_jobs/lock/server_validator.rb +27 -0
  20. data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +34 -15
  21. data/lib/sidekiq_unique_jobs/lock/until_executed.rb +19 -7
  22. data/lib/sidekiq_unique_jobs/lock/until_executing.rb +16 -2
  23. data/lib/sidekiq_unique_jobs/lock/until_expired.rb +20 -16
  24. data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
  25. data/lib/sidekiq_unique_jobs/lock/while_executing.rb +19 -10
  26. data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +3 -3
  27. data/lib/sidekiq_unique_jobs/lock.rb +325 -0
  28. data/lib/sidekiq_unique_jobs/lock_args.rb +123 -0
  29. data/lib/sidekiq_unique_jobs/lock_config.rb +126 -0
  30. data/lib/sidekiq_unique_jobs/lock_digest.rb +79 -0
  31. data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
  32. data/lib/sidekiq_unique_jobs/lock_timeout.rb +62 -0
  33. data/lib/sidekiq_unique_jobs/lock_ttl.rb +77 -0
  34. data/lib/sidekiq_unique_jobs/locksmith.rb +275 -102
  35. data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
  36. data/lib/sidekiq_unique_jobs/logging.rb +179 -33
  37. data/lib/sidekiq_unique_jobs/lua/delete.lua +51 -0
  38. data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +42 -0
  39. data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
  40. data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
  41. data/lib/sidekiq_unique_jobs/lua/lock.lua +93 -0
  42. data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
  43. data/lib/sidekiq_unique_jobs/lua/queue.lua +87 -0
  44. data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +94 -0
  45. data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +40 -0
  46. data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
  47. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +22 -0
  48. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +18 -0
  49. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_process_set.lua +53 -0
  50. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +43 -0
  51. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
  52. data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
  53. data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
  54. data/lib/sidekiq_unique_jobs/lua/unlock.lua +95 -0
  55. data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
  56. data/lib/sidekiq_unique_jobs/lua/upgrade.lua +68 -0
  57. data/lib/sidekiq_unique_jobs/middleware/client.rb +40 -0
  58. data/lib/sidekiq_unique_jobs/middleware/server.rb +29 -0
  59. data/lib/sidekiq_unique_jobs/middleware.rb +29 -43
  60. data/lib/sidekiq_unique_jobs/normalizer.rb +4 -4
  61. data/lib/sidekiq_unique_jobs/on_conflict/log.rb +9 -5
  62. data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +1 -1
  63. data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +1 -1
  64. data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +61 -15
  65. data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +54 -14
  66. data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +16 -5
  67. data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +25 -6
  68. data/lib/sidekiq_unique_jobs/on_conflict.rb +23 -10
  69. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +34 -29
  70. data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
  71. data/lib/sidekiq_unique_jobs/orphans/manager.rb +213 -0
  72. data/lib/sidekiq_unique_jobs/orphans/null_reaper.rb +24 -0
  73. data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
  74. data/lib/sidekiq_unique_jobs/orphans/reaper.rb +114 -0
  75. data/lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb +170 -0
  76. data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +213 -0
  77. data/lib/sidekiq_unique_jobs/redis/entity.rb +112 -0
  78. data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
  79. data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
  80. data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
  81. data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +86 -0
  82. data/lib/sidekiq_unique_jobs/redis/string.rb +49 -0
  83. data/lib/sidekiq_unique_jobs/redis.rb +11 -0
  84. data/lib/sidekiq_unique_jobs/reflectable.rb +17 -0
  85. data/lib/sidekiq_unique_jobs/reflections.rb +68 -0
  86. data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
  87. data/lib/sidekiq_unique_jobs/rspec/matchers.rb +26 -0
  88. data/lib/sidekiq_unique_jobs/script/caller.rb +127 -0
  89. data/lib/sidekiq_unique_jobs/script.rb +15 -0
  90. data/lib/sidekiq_unique_jobs/server.rb +49 -0
  91. data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +92 -65
  92. data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +241 -35
  93. data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +11 -15
  94. data/lib/sidekiq_unique_jobs/testing.rb +62 -21
  95. data/lib/sidekiq_unique_jobs/timer_task.rb +78 -0
  96. data/lib/sidekiq_unique_jobs/timing.rb +58 -0
  97. data/lib/sidekiq_unique_jobs/unlockable.rb +20 -4
  98. data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
  99. data/lib/sidekiq_unique_jobs/upgrade_locks.rb +155 -0
  100. data/lib/sidekiq_unique_jobs/version.rb +3 -1
  101. data/lib/sidekiq_unique_jobs/version_check.rb +23 -4
  102. data/lib/sidekiq_unique_jobs/web/helpers.rb +128 -13
  103. data/lib/sidekiq_unique_jobs/web/views/_paging.erb +4 -4
  104. data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +54 -0
  105. data/lib/sidekiq_unique_jobs/web/views/lock.erb +108 -0
  106. data/lib/sidekiq_unique_jobs/web/views/locks.erb +54 -0
  107. data/lib/sidekiq_unique_jobs/web.rb +57 -27
  108. data/lib/sidekiq_unique_jobs.rb +52 -7
  109. data/lib/tasks/changelog.rake +5 -5
  110. metadata +121 -177
  111. data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
  112. data/lib/sidekiq_unique_jobs/scripts.rb +0 -118
  113. data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -46
  114. data/lib/sidekiq_unique_jobs/timeout/calculator.rb +0 -63
  115. data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
  116. data/lib/sidekiq_unique_jobs/unique_args.rb +0 -150
  117. data/lib/sidekiq_unique_jobs/util.rb +0 -103
  118. data/lib/sidekiq_unique_jobs/web/views/unique_digest.erb +0 -28
  119. data/lib/sidekiq_unique_jobs/web/views/unique_digests.erb +0 -46
  120. data/redis/acquire_lock.lua +0 -21
  121. data/redis/convert_legacy_lock.lua +0 -13
  122. data/redis/delete.lua +0 -14
  123. data/redis/delete_by_digest.lua +0 -23
  124. data/redis/delete_job_by_digest.lua +0 -60
  125. data/redis/lock.lua +0 -62
  126. data/redis/release_stale_locks.lua +0 -90
  127. data/redis/unlock.lua +0 -35
@@ -5,10 +5,33 @@ module SidekiqUniqueJobs
5
5
  # Abstract base class for locks
6
6
  #
7
7
  # @abstract
8
- # @author Mikael Henriksson <mikael@zoolutions.se>
8
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
9
9
  class BaseLock
10
+ extend Forwardable
11
+
12
+ # includes "SidekiqUniqueJobs::Logging"
13
+ # @!parse include SidekiqUniqueJobs::Logging
10
14
  include SidekiqUniqueJobs::Logging
11
15
 
16
+ # includes "SidekiqUniqueJobs::Reflectable"
17
+ # @!parse include SidekiqUniqueJobs::Reflectable
18
+ include SidekiqUniqueJobs::Reflectable
19
+
20
+ #
21
+ # Validates that the sidekiq_options for the worker is valid
22
+ #
23
+ # @param [Hash] options the sidekiq_options given to the worker
24
+ #
25
+ # @return [void]
26
+ #
27
+ def self.validate_options(options = {})
28
+ Validator.validate(options)
29
+ end
30
+
31
+ # NOTE: Mainly used for a clean testing API
32
+ #
33
+ def_delegators :locksmith, :locked?
34
+
12
35
  # @param [Hash] item the Sidekiq job hash
13
36
  # @param [Proc] callback the callback to use after unlock
14
37
  # @param [Sidekiq::RedisConnection, ConnectionPool] redis_pool the redis connection
@@ -16,21 +39,22 @@ module SidekiqUniqueJobs
16
39
  @item = item
17
40
  @callback = callback
18
41
  @redis_pool = redis_pool
19
- add_uniqueness_when_missing # Used to ease testing
42
+ @attempt = 0
43
+ prepare_item # Used to ease testing
44
+ @lock_config = LockConfig.new(item)
20
45
  end
21
46
 
22
- # Handles locking of sidekiq jobs.
23
- # Will call a conflict strategy if lock can't be achieved.
24
- # @return [String] the sidekiq job id
47
+ #
48
+ # Locks a sidekiq job
49
+ #
50
+ # @note Will call a conflict strategy if lock can't be achieved.
51
+ #
52
+ # @return [String, nil] the locked jid when properly locked, else nil.
53
+ #
54
+ # @yield to the caller when given a block
55
+ #
25
56
  def lock
26
- @attempt = 0
27
- return item[JID_KEY] if locked?
28
-
29
- if (token = locksmith.lock(item[LOCK_TIMEOUT_KEY]))
30
- token
31
- else
32
- call_strategy
33
- end
57
+ raise NotImplementedError, "##{__method__} needs to be implemented in #{self.class}"
34
58
  end
35
59
 
36
60
  # Execute the job in the Sidekiq server processor
@@ -39,97 +63,94 @@ module SidekiqUniqueJobs
39
63
  raise NotImplementedError, "##{__method__} needs to be implemented in #{self.class}"
40
64
  end
41
65
 
42
- # Unlocks the job from redis
43
- # @return [String] sidekiq job id when successful
44
- # @return [false] when unsuccessful
45
- def unlock
46
- locksmith.unlock(item[JID_KEY]) # Only signal to release the lock
47
- end
48
-
49
- # Deletes the job from redis if it is locked.
50
- def delete
51
- locksmith.delete # Soft delete (don't forcefully remove when expiration is set)
52
- end
53
-
54
- # Forcefully deletes the job from redis.
55
- # This is good for jobs when a previous lock was not unlocked
56
- def delete!
57
- locksmith.delete! # Force delete the lock
58
- end
59
-
60
- # Checks if the item has achieved a lock
61
- # @return [true] when this jid has locked the job
62
- # @return [false] when this jid has not locked the job
63
- def locked?
64
- locksmith.locked?(item[JID_KEY])
66
+ #
67
+ # The lock manager/client
68
+ #
69
+ # @api private
70
+ # @return [SidekiqUniqueJobs::Locksmith] the locksmith for this sidekiq job
71
+ #
72
+ def locksmith
73
+ @locksmith ||= SidekiqUniqueJobs::Locksmith.new(item, redis_pool)
65
74
  end
66
75
 
67
76
  private
68
77
 
69
- def add_uniqueness_when_missing
70
- return if item.key?(UNIQUE_DIGEST_KEY)
78
+ # @!attribute [r] item
79
+ # @return [Hash<String, Object>] the Sidekiq job hash
80
+ attr_reader :item
81
+ # @!attribute [r] lock_config
82
+ # @return [LockConfig] a lock configuration
83
+ attr_reader :lock_config
84
+ # @!attribute [r] redis_pool
85
+ # @return [Sidekiq::RedisConnection, ConnectionPool, NilClass] the redis connection
86
+ attr_reader :redis_pool
87
+ # @!attribute [r] callback
88
+ # @return [Proc] the block to call after unlock
89
+ attr_reader :callback
90
+ # @!attribute [r] attempt
91
+ # @return [Integer] the current locking attempt
92
+ attr_reader :attempt
93
+
94
+ def prepare_item
95
+ return if item.key?(LOCK_DIGEST)
71
96
 
72
97
  # The below should only be done to ease testing
73
98
  # in production this will be done by the middleware
74
- SidekiqUniqueJobs::Job.add_uniqueness(item)
99
+ SidekiqUniqueJobs::Job.prepare(item)
75
100
  end
76
101
 
77
- def call_strategy
78
- @attempt += 1
79
- strategy.call { lock if replace? }
102
+ #
103
+ # Handle when lock failed
104
+ #
105
+ # @param [Symbol] location: :client or :server
106
+ #
107
+ # @return [void]
108
+ #
109
+ def lock_failed(origin: :client)
110
+ reflect(:lock_failed, item)
111
+ call_strategy(origin: origin)
80
112
  end
81
113
 
82
- def replace?
83
- strategy.replace? && attempt < 2
84
- end
85
-
86
- # The sidekiq job hash
87
- # @return [Hash] the Sidekiq job hash
88
- attr_reader :item
89
-
90
- # The sidekiq redis pool
91
- # @return [Sidekiq::RedisConnection, ConnectionPool, NilClass] the redis connection
92
- attr_reader :redis_pool
93
-
94
- # The sidekiq job hash
95
- # @return [Proc] the callback to use after unlock
96
- attr_reader :callback
97
-
98
- # The current attempt to lock the job
99
- # @return [Integer] the numerical value of the attempt
100
- attr_reader :attempt
114
+ def call_strategy(origin:)
115
+ @attempt += 1
101
116
 
102
- # The interface to the locking mechanism
103
- # @return [SidekiqUniqueJobs::Locksmith]
104
- def locksmith
105
- @locksmith ||= SidekiqUniqueJobs::Locksmith.new(item, redis_pool)
117
+ case origin
118
+ when :client
119
+ client_strategy.call { lock if replace? }
120
+ when :server
121
+ server_strategy.call { lock if replace? }
122
+ else
123
+ raise SidekiqUniqueJobs::InvalidArgument,
124
+ "either `for: :server` or `for: :client` needs to be specified"
125
+ end
106
126
  end
107
127
 
108
- def with_cleanup
109
- yield
110
- rescue Sidekiq::Shutdown
111
- log_info("Sidekiq is shutting down, the job `should` be put back on the queue. Keeping the lock!")
112
- raise
113
- else
114
- unlock_with_callback
128
+ def replace?
129
+ client_strategy.replace? && attempt < 2
115
130
  end
116
131
 
117
- def unlock_with_callback
118
- return log_warn("might need to be unlocked manually") unless unlock
132
+ def unlock_and_callback
133
+ return callback_safely if locksmith.unlock
119
134
 
120
- callback_safely
135
+ reflect(:unlock_failed, item)
121
136
  end
122
137
 
123
138
  def callback_safely
124
139
  callback&.call
125
- item[JID_KEY]
140
+ item[JID]
126
141
  rescue StandardError
127
- log_warn("unlocked successfully but the #after_unlock callback failed!")
142
+ reflect(:after_unlock_callback_failed, item)
128
143
  raise
129
144
  end
130
145
 
131
- def strategy
132
- @strategy ||= OnConflict.find_strategy(item[ON_CONFLICT_KEY]).new(item)
146
+ def client_strategy
147
+ @client_strategy ||=
148
+ OnConflict.find_strategy(lock_config.on_client_conflict).new(item, redis_pool)
149
+ end
150
+
151
+ def server_strategy
152
+ @server_strategy ||=
153
+ OnConflict.find_strategy(lock_config.on_server_conflict).new(item, redis_pool)
133
154
  end
134
155
  end
135
156
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ class Lock
5
+ #
6
+ # Validates the sidekiq options for the Sidekiq client process
7
+ #
8
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
9
+ #
10
+ class ClientValidator
11
+ #
12
+ # @return [Array<Symbol>] a collection of invalid conflict resolutions
13
+ INVALID_ON_CONFLICTS = [:raise, :reject, :reschedule].freeze
14
+
15
+ #
16
+ # Validates the sidekiq options for the Sidekiq client process
17
+ #
18
+ #
19
+ def self.validate(lock_config)
20
+ on_conflict = lock_config.on_client_conflict
21
+ return lock_config unless INVALID_ON_CONFLICTS.include?(on_conflict)
22
+
23
+ lock_config.errors[:on_client_conflict] = "#{on_conflict} is incompatible with the client process"
24
+ lock_config
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ class Lock
5
+ #
6
+ # Validates the sidekiq options for the Sidekiq server process
7
+ #
8
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
9
+ #
10
+ class ServerValidator
11
+ #
12
+ # @return [Array<Symbol>] a collection of invalid conflict resolutions
13
+ INVALID_ON_CONFLICTS = [:replace].freeze
14
+
15
+ #
16
+ # Validates the sidekiq options for the Sidekiq server process
17
+ #
18
+ #
19
+ def self.validate(lock_config)
20
+ on_conflict = lock_config.on_server_conflict
21
+ return lock_config unless INVALID_ON_CONFLICTS.include?(on_conflict)
22
+
23
+ lock_config.errors[:on_server_conflict] = "#{on_conflict} is incompatible with the server process"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -11,36 +11,55 @@ module SidekiqUniqueJobs
11
11
  # See {#lock} for more information about the client.
12
12
  # See {#execute} for more information about the server
13
13
  #
14
- # @author Mikael Henriksson <mikael@zoolutions.se>
14
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
15
15
  class UntilAndWhileExecuting < BaseLock
16
+ #
17
+ # Locks a sidekiq job
18
+ #
19
+ # @note Will call a conflict strategy if lock can't be achieved.
20
+ #
21
+ # @return [String, nil] the locked jid when properly locked, else nil.
22
+ #
23
+ # @yield to the caller when given a block
24
+ #
25
+ def lock(origin: :client)
26
+ return lock_failed(origin: origin) unless (token = locksmith.lock)
27
+ return yield token if block_given?
28
+
29
+ token
30
+ end
31
+
16
32
  # Executes in the Sidekiq server process
17
33
  # @yield to the worker class perform method
18
34
  def execute
19
- if unlock
20
- lock_on_failure do
21
- runtime_lock.execute { return yield }
22
- end
35
+ if locksmith.unlock
36
+ # ensure_relocked do
37
+ runtime_lock.execute { return yield }
38
+ # end
23
39
  else
24
- log_warn "couldn't unlock digest: #{item[UNIQUE_DIGEST_KEY]} #{item[JID_KEY]}"
40
+ reflect(:unlock_failed, item)
25
41
  end
26
- ensure
27
- runtime_lock.delete!
28
- end
42
+ rescue Exception # rubocop:disable Lint/RescueException
43
+ reflect(:execution_failed, item)
44
+ locksmith.lock(wait: 2)
29
45
 
30
- def runtime_lock
31
- @runtime_lock ||= SidekiqUniqueJobs::Lock::WhileExecuting.new(item, callback, redis_pool)
46
+ raise
32
47
  end
33
48
 
34
49
  private
35
50
 
36
- def lock_on_failure
51
+ def ensure_relocked
37
52
  yield
38
- runtime_lock.delete!
39
53
  rescue Exception # rubocop:disable Lint/RescueException
40
- log_error("Failed to execute job, restoring lock")
41
- lock
54
+ reflect(:execution_failed, item)
55
+ locksmith.lock
56
+
42
57
  raise
43
58
  end
59
+
60
+ def runtime_lock
61
+ @runtime_lock ||= SidekiqUniqueJobs::Lock::WhileExecuting.new(item.dup, callback, redis_pool)
62
+ end
44
63
  end
45
64
  end
46
65
  end
@@ -6,18 +6,30 @@ module SidekiqUniqueJobs
6
6
  # - Locks on perform_in or perform_async
7
7
  # - Unlocks after yielding to the worker's perform method
8
8
  #
9
- # @author Mikael Henriksson <mikael@zoolutions.se>
9
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
10
10
  class UntilExecuted < BaseLock
11
- OK ||= "OK"
11
+ #
12
+ # Locks a sidekiq job
13
+ #
14
+ # @note Will call a conflict strategy if lock can't be achieved.
15
+ #
16
+ # @return [String, nil] the locked jid when properly locked, else nil.
17
+ #
18
+ # @yield to the caller when given a block
19
+ #
20
+ def lock
21
+ return lock_failed(origin: :client) unless (token = locksmith.lock)
22
+ return yield token if block_given?
23
+
24
+ token
25
+ end
12
26
 
13
27
  # Executes in the Sidekiq server process
14
28
  # @yield to the worker class perform method
15
29
  def execute
16
- if locked?
17
- with_cleanup { yield }
18
- else
19
- log_warn "the unique_key: #{item[UNIQUE_DIGEST_KEY]} is not locked, allowing job to silently complete"
20
- nil
30
+ locksmith.execute do
31
+ yield
32
+ unlock_and_callback
21
33
  end
22
34
  end
23
35
  end
@@ -6,12 +6,26 @@ module SidekiqUniqueJobs
6
6
  # - Locks on perform_in or perform_async
7
7
  # - Unlocks before yielding to the worker's perform method
8
8
  #
9
- # @author Mikael Henriksson <mikael@zoolutions.se>
9
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
10
10
  class UntilExecuting < BaseLock
11
+ #
12
+ # Locks a sidekiq job
13
+ #
14
+ # @note Will call a conflict strategy if lock can't be achieved.
15
+ #
16
+ # @return [String, nil] the locked jid when properly locked, else nil.
17
+ #
18
+ def lock
19
+ return lock_failed unless (job_id = locksmith.lock)
20
+ return yield job_id if block_given?
21
+
22
+ job_id
23
+ end
24
+
11
25
  # Executes in the Sidekiq server process
12
26
  # @yield to the worker class perform method
13
27
  def execute
14
- unlock_with_callback
28
+ callback_safely if locksmith.unlock
15
29
  yield
16
30
  end
17
31
  end
@@ -2,28 +2,32 @@
2
2
 
3
3
  module SidekiqUniqueJobs
4
4
  class Lock
5
- # Locks jobs until the lock has expired
6
- # - Locks on perform_in or perform_async
7
- # - Unlocks when the expiration is hit
8
5
  #
9
- # See {#lock} for more information about the client.
10
- # See {#execute} for more information about the server
6
+ # UntilExpired locks until the job expires
11
7
  #
12
- # @author Mikael Henriksson <mikael@zoolutions.se>
13
- class UntilExpired < BaseLock
14
- # Prevents these locks from being unlocked
15
- # @return [true] always returns true
16
- def unlock
17
- true
8
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
9
+ #
10
+ class UntilExpired < UntilExecuted
11
+ #
12
+ # Locks a sidekiq job
13
+ #
14
+ # @note Will call a conflict strategy if lock can't be achieved.
15
+ #
16
+ # @return [String, nil] the locked jid when properly locked, else nil.
17
+ #
18
+ # @yield to the caller when given a block
19
+ #
20
+ def lock
21
+ return lock_failed unless (job_id = locksmith.lock)
22
+ return yield job_id if block_given?
23
+
24
+ job_id
18
25
  end
19
26
 
20
27
  # Executes in the Sidekiq server process
21
28
  # @yield to the worker class perform method
22
- def execute
23
- return unless locked?
24
-
25
- yield
26
- # this lock does not handle after_unlock since we don't know when that would happen
29
+ def execute(&block)
30
+ locksmith.execute(&block)
27
31
  end
28
32
  end
29
33
  end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ class Lock
5
+ #
6
+ # Validator base class to avoid some duplication
7
+ #
8
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
9
+ #
10
+ class Validator
11
+ #
12
+ # @return [Hash] a hash mapping of deprecated keys and their new value
13
+ DEPRECATED_KEYS = {
14
+ UNIQUE.to_sym => LOCK.to_sym,
15
+ UNIQUE_ARGS.to_sym => LOCK_ARGS_METHOD.to_sym,
16
+ LOCK_ARGS.to_sym => LOCK_ARGS_METHOD.to_sym,
17
+ UNIQUE_PREFIX.to_sym => LOCK_PREFIX.to_sym,
18
+ }.freeze
19
+
20
+ #
21
+ # Shorthand for `new(options).validate`
22
+ #
23
+ # @param [Hash] options the sidekiq_options for the worker being validated
24
+ #
25
+ # @return [LockConfig] the lock configuration with errors if any
26
+ #
27
+ def self.validate(options)
28
+ new(options).validate
29
+ end
30
+
31
+ #
32
+ # @!attribute [r] lock_config
33
+ # @return [LockConfig] the lock configuration for this worker
34
+ attr_reader :lock_config
35
+
36
+ #
37
+ # Initialize a new validator
38
+ #
39
+ # @param [Hash] options the sidekiq_options for the worker being validated
40
+ #
41
+ def initialize(options)
42
+ @options = options.transform_keys(&:to_sym)
43
+ @lock_config = LockConfig.new(options)
44
+ handle_deprecations
45
+ end
46
+
47
+ #
48
+ # Validate the workers lock configuration
49
+ #
50
+ #
51
+ # @return [LockConfig] the lock configuration with errors if any
52
+ #
53
+ def validate
54
+ case lock_config.type
55
+ when :while_executing
56
+ validate_server
57
+ when :until_executing
58
+ validate_client
59
+ else
60
+ validate_client
61
+ validate_server
62
+ end
63
+
64
+ lock_config
65
+ end
66
+
67
+ #
68
+ # Validate deprecated keys
69
+ # adds useful information about how to proceed with fixing handle_deprecations
70
+ #
71
+ # @return [void]
72
+ #
73
+ def handle_deprecations
74
+ DEPRECATED_KEYS.each do |old, new|
75
+ next unless @options.key?(old)
76
+
77
+ lock_config.errors[old] = "is deprecated, use `#{new}: #{@options[old]}` instead."
78
+ end
79
+ end
80
+
81
+ #
82
+ # Validates the client configuration
83
+ #
84
+ def validate_client
85
+ ClientValidator.validate(lock_config)
86
+ end
87
+
88
+ #
89
+ # Validates the server configuration
90
+ #
91
+ def validate_server
92
+ ServerValidator.validate(lock_config)
93
+ end
94
+ end
95
+ end
96
+ end
@@ -9,10 +9,13 @@ module SidekiqUniqueJobs
9
9
  # See {#lock} for more information about the client.
10
10
  # See {#execute} for more information about the server
11
11
  #
12
- # @author Mikael Henriksson <mikael@zoolutions.se>
12
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
13
13
  class WhileExecuting < BaseLock
14
14
  RUN_SUFFIX ||= ":RUN"
15
15
 
16
+ include SidekiqUniqueJobs::OptionsWithFallback
17
+ include SidekiqUniqueJobs::Logging::Middleware
18
+
16
19
  # @param [Hash] item the Sidekiq job hash
17
20
  # @param [Proc] callback callback to call after unlock
18
21
  # @param [Sidekiq::RedisConnection, ConnectionPool] redis_pool the redis connection
@@ -26,20 +29,24 @@ module SidekiqUniqueJobs
26
29
  # These locks should only ever be created in the server process.
27
30
  # @return [true] always returns true
28
31
  def lock
29
- true
32
+ job_id = item[JID]
33
+ yield job_id if block_given?
34
+
35
+ job_id
30
36
  end
31
37
 
32
38
  # Executes in the Sidekiq server process.
33
39
  # These jobs are locked in the server process not from the client
34
40
  # @yield to the worker class perform method
35
41
  def execute
36
- return strategy&.call unless locksmith.lock(item[LOCK_TIMEOUT_KEY])
37
-
38
- yield
39
- unlock_with_callback
40
- rescue Exception # rubocop:disable Lint/RescueException
41
- delete!
42
- raise
42
+ with_logging_context do
43
+ call_strategy(origin: :server) unless locksmith.execute do
44
+ yield
45
+ callback_safely if locksmith.unlock
46
+ ensure
47
+ locksmith.unlock
48
+ end
49
+ end
43
50
  end
44
51
 
45
52
  private
@@ -47,7 +54,9 @@ module SidekiqUniqueJobs
47
54
  # This is safe as the base_lock always creates a new digest
48
55
  # The append there for needs to be done every time
49
56
  def append_unique_key_suffix
50
- item[UNIQUE_DIGEST_KEY] = item[UNIQUE_DIGEST_KEY] + RUN_SUFFIX
57
+ return if (lock_digest = item[LOCK_DIGEST]).end_with?(RUN_SUFFIX)
58
+
59
+ item[LOCK_DIGEST] = lock_digest + RUN_SUFFIX
51
60
  end
52
61
  end
53
62
  end
@@ -9,12 +9,12 @@ module SidekiqUniqueJobs
9
9
  # See {#lock} for more information about the client.
10
10
  # See {#execute} for more information about the server
11
11
  #
12
- # @author Mikael Henriksson <mikael@zoolutions.se>
12
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
13
13
  class WhileExecutingReject < WhileExecuting
14
14
  # Overridden with a forced {OnConflict::Reject} strategy
15
15
  # @return [OnConflict::Reject] a reject strategy
16
- def strategy
17
- @strategy ||= OnConflict.find_strategy(:reject).new(item)
16
+ def server_strategy
17
+ @server_strategy ||= OnConflict.find_strategy(:reject).new(item, redis_pool)
18
18
  end
19
19
  end
20
20
  end