sidekiq-unique-jobs 6.0.24 → 7.0.4

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 (122) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +707 -25
  3. data/README.md +516 -105
  4. data/lib/sidekiq_unique_jobs.rb +48 -7
  5. data/lib/sidekiq_unique_jobs/batch_delete.rb +123 -0
  6. data/lib/sidekiq_unique_jobs/changelog.rb +78 -0
  7. data/lib/sidekiq_unique_jobs/cli.rb +34 -31
  8. data/lib/sidekiq_unique_jobs/config.rb +263 -0
  9. data/lib/sidekiq_unique_jobs/connection.rb +6 -5
  10. data/lib/sidekiq_unique_jobs/constants.rb +46 -24
  11. data/lib/sidekiq_unique_jobs/core_ext.rb +80 -0
  12. data/lib/sidekiq_unique_jobs/digests.rb +71 -100
  13. data/lib/sidekiq_unique_jobs/exceptions.rb +78 -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.rb +325 -0
  18. data/lib/sidekiq_unique_jobs/lock/base_lock.rb +66 -50
  19. data/lib/sidekiq_unique_jobs/lock/client_validator.rb +28 -0
  20. data/lib/sidekiq_unique_jobs/lock/server_validator.rb +27 -0
  21. data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +7 -10
  22. data/lib/sidekiq_unique_jobs/lock/until_executed.rb +6 -6
  23. data/lib/sidekiq_unique_jobs/lock/until_executing.rb +1 -1
  24. data/lib/sidekiq_unique_jobs/lock/until_expired.rb +4 -21
  25. data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
  26. data/lib/sidekiq_unique_jobs/lock/while_executing.rb +13 -9
  27. data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +3 -3
  28. data/lib/sidekiq_unique_jobs/lock_args.rb +123 -0
  29. data/lib/sidekiq_unique_jobs/lock_config.rb +122 -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 +261 -101
  35. data/lib/sidekiq_unique_jobs/logging.rb +149 -23
  36. data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
  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.rb +29 -31
  58. data/lib/sidekiq_unique_jobs/middleware/client.rb +42 -0
  59. data/lib/sidekiq_unique_jobs/middleware/server.rb +27 -0
  60. data/lib/sidekiq_unique_jobs/normalizer.rb +4 -4
  61. data/lib/sidekiq_unique_jobs/on_conflict.rb +23 -10
  62. data/lib/sidekiq_unique_jobs/on_conflict/log.rb +9 -5
  63. data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +1 -1
  64. data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +1 -1
  65. data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +61 -15
  66. data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +54 -14
  67. data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +12 -5
  68. data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +25 -6
  69. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +41 -27
  70. data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
  71. data/lib/sidekiq_unique_jobs/orphans/manager.rb +212 -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/ruby_reaper.rb +201 -0
  76. data/lib/sidekiq_unique_jobs/redis.rb +11 -0
  77. data/lib/sidekiq_unique_jobs/redis/entity.rb +106 -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/rspec/matchers.rb +26 -0
  84. data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
  85. data/lib/sidekiq_unique_jobs/script.rb +15 -0
  86. data/lib/sidekiq_unique_jobs/script/caller.rb +125 -0
  87. data/lib/sidekiq_unique_jobs/server.rb +48 -0
  88. data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +92 -65
  89. data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +185 -34
  90. data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +11 -5
  91. data/lib/sidekiq_unique_jobs/testing.rb +62 -21
  92. data/lib/sidekiq_unique_jobs/timer_task.rb +78 -0
  93. data/lib/sidekiq_unique_jobs/timing.rb +58 -0
  94. data/lib/sidekiq_unique_jobs/unlockable.rb +20 -4
  95. data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
  96. data/lib/sidekiq_unique_jobs/upgrade_locks.rb +155 -0
  97. data/lib/sidekiq_unique_jobs/version.rb +3 -1
  98. data/lib/sidekiq_unique_jobs/version_check.rb +23 -4
  99. data/lib/sidekiq_unique_jobs/web.rb +50 -27
  100. data/lib/sidekiq_unique_jobs/web/helpers.rb +125 -10
  101. data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +54 -0
  102. data/lib/sidekiq_unique_jobs/web/views/lock.erb +108 -0
  103. data/lib/sidekiq_unique_jobs/web/views/locks.erb +52 -0
  104. data/lib/tasks/changelog.rake +5 -5
  105. metadata +117 -177
  106. data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
  107. data/lib/sidekiq_unique_jobs/scripts.rb +0 -118
  108. data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -46
  109. data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
  110. data/lib/sidekiq_unique_jobs/timeout/calculator.rb +0 -63
  111. data/lib/sidekiq_unique_jobs/unique_args.rb +0 -150
  112. data/lib/sidekiq_unique_jobs/util.rb +0 -103
  113. data/lib/sidekiq_unique_jobs/web/views/unique_digest.erb +0 -28
  114. data/lib/sidekiq_unique_jobs/web/views/unique_digests.erb +0 -46
  115. data/redis/acquire_lock.lua +0 -21
  116. data/redis/convert_legacy_lock.lua +0 -13
  117. data/redis/delete.lua +0 -14
  118. data/redis/delete_by_digest.lua +0 -23
  119. data/redis/delete_job_by_digest.lua +0 -60
  120. data/redis/lock.lua +0 -62
  121. data/redis/release_stale_locks.lua +0 -90
  122. data/redis/unlock.lua +0 -35
@@ -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
@@ -33,13 +36,14 @@ module SidekiqUniqueJobs
33
36
  # These jobs are locked in the server process not from the client
34
37
  # @yield to the worker class perform method
35
38
  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
39
+ with_logging_context do
40
+ server_strategy&.call unless locksmith.lock do
41
+ yield
42
+ callback_safely
43
+ end
44
+ end
45
+ ensure
46
+ locksmith.unlock!
43
47
  end
44
48
 
45
49
  private
@@ -47,7 +51,7 @@ module SidekiqUniqueJobs
47
51
  # This is safe as the base_lock always creates a new digest
48
52
  # The append there for needs to be done every time
49
53
  def append_unique_key_suffix
50
- item[UNIQUE_DIGEST_KEY] = item[UNIQUE_DIGEST_KEY] + RUN_SUFFIX
54
+ item[LOCK_DIGEST] = item[LOCK_DIGEST] + RUN_SUFFIX
51
55
  end
52
56
  end
53
57
  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
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ # Handles uniqueness of sidekiq arguments
5
+ #
6
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
7
+ class LockArgs
8
+ include SidekiqUniqueJobs::Logging
9
+ include SidekiqUniqueJobs::SidekiqWorkerMethods
10
+ include SidekiqUniqueJobs::JSON
11
+
12
+ # Convenience method for returning a digest
13
+ # @param [Hash] item a Sidekiq job hash
14
+ # @return [String] a unique digest
15
+ def self.call(item)
16
+ new(item).lock_args
17
+ end
18
+
19
+ # The sidekiq job hash
20
+ # @return [Hash] the Sidekiq job hash
21
+ attr_reader :item
22
+ #
23
+ # @!attribute [r] args
24
+ # @return [Array<Objet>] the arguments passed to `perform_async`
25
+ attr_reader :args
26
+
27
+ # @param [Hash] item a Sidekiq job hash
28
+ def initialize(item)
29
+ @item = item
30
+ @worker_class = item[CLASS]
31
+ @args = item[ARGS]
32
+ end
33
+
34
+ # The unique arguments to use for creating a lock
35
+ # @return [Array] the arguments filters by the {#filtered_args} method if {#lock_args_enabled?}
36
+ def lock_args
37
+ @lock_args ||= filtered_args
38
+ end
39
+
40
+ # Checks if the worker class has enabled lock_args
41
+ # @return [true, false]
42
+ def lock_args_enabled?
43
+ # return false unless lock_args_method_valid?
44
+
45
+ lock_args_method
46
+ end
47
+
48
+ # Validate that the lock_args_method is acceptable
49
+ # @return [true, false]
50
+ def lock_args_method_valid?
51
+ [NilClass, TrueClass, FalseClass].none? { |klass| lock_args_method.is_a?(klass) }
52
+ end
53
+
54
+ # Checks if the worker class has disabled lock_args
55
+ # @return [true, false]
56
+ def lock_args_disabled?
57
+ !lock_args_method
58
+ end
59
+
60
+ # Filters unique arguments by proc or symbol
61
+ # @return [Array] {#filter_by_proc} when {#lock_args_method} is a Proc
62
+ # @return [Array] {#filter_by_symbol} when {#lock_args_method} is a Symbol
63
+ # @return [Array] args unfiltered when neither of the above
64
+ def filtered_args
65
+ return args if lock_args_disabled?
66
+
67
+ json_args = Normalizer.jsonify(args)
68
+
69
+ case lock_args_method
70
+ when Proc
71
+ filter_by_proc(json_args)
72
+ when Symbol
73
+ filter_by_symbol(json_args)
74
+ end
75
+ end
76
+
77
+ # Filters unique arguments by proc configured in the sidekiq worker
78
+ # @param [Array] args the arguments passed to the sidekiq worker
79
+ # @return [Array] with the filtered arguments
80
+ def filter_by_proc(args)
81
+ lock_args_method.call(args)
82
+ end
83
+
84
+ # Filters unique arguments by method configured in the sidekiq worker
85
+ # @param [Array] args the arguments passed to the sidekiq worker
86
+ # @return [Array] unfiltered unless {#worker_method_defined?}
87
+ # @return [Array] with the filtered arguments
88
+ def filter_by_symbol(args)
89
+ return args unless worker_method_defined?(lock_args_method)
90
+
91
+ worker_class.send(lock_args_method, args)
92
+ rescue ArgumentError
93
+ raise SidekiqUniqueJobs::InvalidUniqueArguments,
94
+ given: args,
95
+ worker_class: worker_class,
96
+ lock_args_method: lock_args_method
97
+ end
98
+
99
+ # The method to use for filtering unique arguments
100
+ def lock_args_method
101
+ @lock_args_method ||= worker_options.slice(LOCK_ARGS_METHOD, UNIQUE_ARGS_METHOD).values.first
102
+ @lock_args_method ||= :lock_args if worker_method_defined?(:lock_args)
103
+ @lock_args_method ||= :unique_args if worker_method_defined?(:unique_args)
104
+ @lock_args_method ||= default_lock_args_method
105
+ end
106
+
107
+ # The global worker options defined in Sidekiq directly
108
+ def default_lock_args_method
109
+ default_worker_options[LOCK_ARGS_METHOD] ||
110
+ default_worker_options[UNIQUE_ARGS_METHOD]
111
+ end
112
+
113
+ #
114
+ # The globally default worker options configured from Sidekiq
115
+ #
116
+ #
117
+ # @return [Hash<String, Object>]
118
+ #
119
+ def default_worker_options
120
+ @default_worker_options ||= Sidekiq.default_worker_options.stringify_keys
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ #
5
+ # Gathers all configuration for a lock
6
+ # which helps reduce the amount of instance variables
7
+ #
8
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
9
+ #
10
+ class LockConfig
11
+ #
12
+ # @!attribute [r] type
13
+ # @return [Symbol] the type of lock
14
+ attr_reader :type
15
+ #
16
+ # @!attribute [r] worker
17
+ # @return [Symbol] the worker class
18
+ attr_reader :worker
19
+ #
20
+ # @!attribute [r] limit
21
+ # @return [Integer] the number of simultaneous locks
22
+ attr_reader :limit
23
+ #
24
+ # @!attribute [r] timeout
25
+ # @return [Integer, nil] the time to wait for a lock
26
+ attr_reader :timeout
27
+ #
28
+ # @!attribute [r] ttl
29
+ # @return [Integer, nil] the time (in seconds) to live after successful
30
+ attr_reader :ttl
31
+ #
32
+ # @!attribute [r] ttl
33
+ # @return [Integer, nil] the time (in milliseconds) to live after successful
34
+ attr_reader :pttl
35
+ #
36
+ # @!attribute [r] lock_info
37
+ # @return [Boolean] indicate wether to use lock_info or not
38
+ attr_reader :lock_info
39
+ #
40
+ # @!attribute [r] on_conflict
41
+ # @return [Symbol, Hash<Symbol, Symbol>] the strategies to use as conflict resolution
42
+ attr_reader :on_conflict
43
+ #
44
+ # @!attribute [r] errors
45
+ # @return [Array<Hash<Symbol, Array<String>] a collection of configuration errors
46
+ attr_reader :errors
47
+
48
+ #
49
+ # Instantiate a new lock_config based on sidekiq options in worker
50
+ #
51
+ # @param [Hash] options sidekiq_options for worker
52
+ #
53
+ # @return [LockConfig]
54
+ #
55
+ def self.from_worker(options)
56
+ new(options.deep_stringify_keys)
57
+ end
58
+
59
+ def initialize(job_hash = {})
60
+ @type = job_hash[LOCK]&.to_sym
61
+ @worker = job_hash[CLASS]
62
+ @limit = job_hash.fetch(LOCK_LIMIT, 1)
63
+ @timeout = job_hash.fetch(LOCK_TIMEOUT, 0)
64
+ @ttl = job_hash.fetch(LOCK_TTL) { job_hash.fetch(LOCK_EXPIRATION, nil) }.to_i
65
+ @pttl = ttl * 1_000
66
+ @lock_info = job_hash.fetch(LOCK_INFO) { SidekiqUniqueJobs.config.lock_info }
67
+ @on_conflict = job_hash.fetch(ON_CONFLICT, nil)
68
+ @errors = job_hash.fetch(ERRORS) { {} }
69
+
70
+ @on_client_conflict = job_hash[ON_CLIENT_CONFLICT]
71
+ @on_server_conflict = job_hash[ON_SERVER_CONFLICT]
72
+ end
73
+
74
+ #
75
+ # Indicate if timeout was set
76
+ #
77
+ #
78
+ # @return [true,fakse]
79
+ #
80
+ def wait_for_lock?
81
+ timeout.nil? || timeout.positive?
82
+ end
83
+
84
+ #
85
+ # Is the configuration valid?
86
+ #
87
+ #
88
+ # @return [true,false]
89
+ #
90
+ def valid?
91
+ errors.empty?
92
+ end
93
+
94
+ #
95
+ # Return a nice descriptive message with all validation errors
96
+ #
97
+ #
98
+ # @return [String]
99
+ #
100
+ def errors_as_string
101
+ return if valid?
102
+
103
+ @errors_as_string ||= begin
104
+ error_msg = +"\t"
105
+ error_msg << errors.map { |key, val| "#{key}: :#{val}" }.join("\n\t")
106
+ error_msg
107
+ end
108
+ end
109
+
110
+ # the strategy to use as conflict resolution from sidekiq client
111
+ def on_client_conflict
112
+ @on_client_conflict ||= on_conflict["client"] if on_conflict.is_a?(Hash)
113
+ @on_client_conflict ||= on_conflict
114
+ end
115
+
116
+ # the strategy to use as conflict resolution from sidekiq server
117
+ def on_server_conflict
118
+ @on_server_conflict ||= on_conflict["server"] if on_conflict.is_a?(Hash)
119
+ @on_server_conflict ||= on_conflict
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+
5
+ module SidekiqUniqueJobs
6
+ # Handles uniqueness of sidekiq arguments
7
+ #
8
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
9
+ class LockDigest
10
+ include SidekiqUniqueJobs::Logging
11
+ include SidekiqUniqueJobs::JSON
12
+ include SidekiqUniqueJobs::SidekiqWorkerMethods
13
+
14
+ #
15
+ # Generates a new digest
16
+ #
17
+ # @param [Hash] item a sidekiq job hash
18
+ #
19
+ # @return [String] a unique digest for the given arguments
20
+ #
21
+ def self.call(item)
22
+ new(item).lock_digest
23
+ end
24
+
25
+ # The sidekiq job hash
26
+ # @return [Hash] the Sidekiq job hash
27
+ attr_reader :item
28
+ #
29
+ # @!attribute [r] args
30
+ # @return [Array<Objet>] the arguments passed to `perform_async`
31
+ attr_reader :lock_args
32
+ #
33
+ # @!attribute [r] args
34
+ # @return [String] the prefix for the unique key
35
+ attr_reader :lock_prefix
36
+
37
+ # @param [Hash] item a Sidekiq job hash
38
+ def initialize(item)
39
+ @item = item
40
+ @worker_class = item[CLASS]
41
+ @lock_args = item.slice(LOCK_ARGS, UNIQUE_ARGS).values.first # TODO: Deprecate UNIQUE_ARGS
42
+ @lock_prefix = item.slice(LOCK_PREFIX, UNIQUE_PREFIX).values.first # TODO: Deprecate UNIQUE_PREFIX
43
+ end
44
+
45
+ # Memoized lock_digest
46
+ # @return [String] a unique digest
47
+ def lock_digest
48
+ @lock_digest ||= create_digest
49
+ end
50
+
51
+ # Creates a namespaced unique digest based on the {#digestable_hash} and the {#lock_prefix}
52
+ # @return [String] a unique digest
53
+ def create_digest
54
+ digest = OpenSSL::Digest::MD5.hexdigest(dump_json(digestable_hash))
55
+ "#{lock_prefix}:#{digest}"
56
+ end
57
+
58
+ # Filter a hash to use for digest
59
+ # @return [Hash] to use for digest
60
+ def digestable_hash
61
+ @item.slice(CLASS, QUEUE, LOCK_ARGS, APARTMENT).tap do |hash|
62
+ hash.delete(QUEUE) if unique_across_queues?
63
+ hash.delete(CLASS) if unique_across_workers?
64
+ end
65
+ end
66
+
67
+ # Checks if we should disregard the queue when creating the unique digest
68
+ # @return [true, false]
69
+ def unique_across_queues?
70
+ item[UNIQUE_ACROSS_QUEUES] || worker_options[UNIQUE_ACROSS_QUEUES]
71
+ end
72
+
73
+ # Checks if we should disregard the worker when creating the unique digest
74
+ # @return [true, false]
75
+ def unique_across_workers?
76
+ item[UNIQUE_ACROSS_WORKERS] || worker_options[UNIQUE_ACROSS_WORKERS]
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ #
5
+ # Class Info provides information about a lock
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ #
9
+ class LockInfo < Redis::String
10
+ #
11
+ # Returns the value for this key as a hash
12
+ #
13
+ #
14
+ # @return [Hash]
15
+ #
16
+ def value
17
+ @value ||= load_json(super)
18
+ end
19
+
20
+ #
21
+ # Check if this redis string is blank
22
+ #
23
+ #
24
+ # @return [Boolean]
25
+ #
26
+ def none?
27
+ value.nil? || value.empty?
28
+ end
29
+
30
+ #
31
+ # Check if this redis string has a value
32
+ #
33
+ #
34
+ # @return [Boolean]
35
+ #
36
+ def present?
37
+ !none?
38
+ end
39
+
40
+ #
41
+ # Quick access to the hash members for the value
42
+ #
43
+ # @param [String, Symbol] key the key who's value to retrieve
44
+ #
45
+ # @return [Object]
46
+ #
47
+ def [](key)
48
+ value[key.to_s] if value.is_a?(Hash)
49
+ end
50
+
51
+ #
52
+ # Writes the lock info to redis
53
+ #
54
+ # @param [Hash] obj the information to store at key
55
+ #
56
+ # @return [Hash]
57
+ #
58
+ def set(obj)
59
+ return unless SidekiqUniqueJobs.config.lock_info
60
+ raise InvalidArgument, "argument `obj` (#{obj}) needs to be a hash" unless obj.is_a?(Hash)
61
+
62
+ json = dump_json(obj)
63
+ @value = load_json(json)
64
+ super(json)
65
+ value
66
+ end
67
+ end
68
+ end