sidekiq-unique-jobs 6.0.7 → 7.1.33

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1880 -156
  3. data/README.md +818 -238
  4. data/bin/uniquejobs +2 -2
  5. data/lib/sidekiq-unique-jobs.rb +1 -1
  6. data/lib/sidekiq_unique_jobs/batch_delete.rb +124 -0
  7. data/lib/sidekiq_unique_jobs/changelog.rb +78 -0
  8. data/lib/sidekiq_unique_jobs/cli.rb +68 -25
  9. data/lib/sidekiq_unique_jobs/config.rb +319 -0
  10. data/lib/sidekiq_unique_jobs/connection.rb +6 -5
  11. data/lib/sidekiq_unique_jobs/constants.rb +50 -20
  12. data/lib/sidekiq_unique_jobs/core_ext.rb +80 -0
  13. data/lib/sidekiq_unique_jobs/deprecation.rb +65 -0
  14. data/lib/sidekiq_unique_jobs/digests.rb +70 -87
  15. data/lib/sidekiq_unique_jobs/exceptions.rb +88 -12
  16. data/lib/sidekiq_unique_jobs/expiring_digests.rb +14 -0
  17. data/lib/sidekiq_unique_jobs/job.rb +47 -13
  18. data/lib/sidekiq_unique_jobs/json.rb +47 -0
  19. data/lib/sidekiq_unique_jobs/key.rb +98 -0
  20. data/lib/sidekiq_unique_jobs/lock/base_lock.rb +111 -82
  21. data/lib/sidekiq_unique_jobs/lock/client_validator.rb +28 -0
  22. data/lib/sidekiq_unique_jobs/lock/server_validator.rb +27 -0
  23. data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +45 -5
  24. data/lib/sidekiq_unique_jobs/lock/until_executed.rb +30 -4
  25. data/lib/sidekiq_unique_jobs/lock/until_executing.rb +26 -2
  26. data/lib/sidekiq_unique_jobs/lock/until_expired.rb +27 -15
  27. data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
  28. data/lib/sidekiq_unique_jobs/lock/while_executing.rb +26 -7
  29. data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +3 -11
  30. data/lib/sidekiq_unique_jobs/lock.rb +342 -0
  31. data/lib/sidekiq_unique_jobs/lock_args.rb +127 -0
  32. data/lib/sidekiq_unique_jobs/lock_config.rb +126 -0
  33. data/lib/sidekiq_unique_jobs/lock_digest.rb +79 -0
  34. data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
  35. data/lib/sidekiq_unique_jobs/lock_timeout.rb +62 -0
  36. data/lib/sidekiq_unique_jobs/lock_ttl.rb +77 -0
  37. data/lib/sidekiq_unique_jobs/lock_type.rb +37 -0
  38. data/lib/sidekiq_unique_jobs/locksmith.rb +324 -95
  39. data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
  40. data/lib/sidekiq_unique_jobs/logging.rb +208 -30
  41. data/lib/sidekiq_unique_jobs/lua/delete.lua +51 -0
  42. data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +42 -0
  43. data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
  44. data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
  45. data/lib/sidekiq_unique_jobs/lua/lock.lua +99 -0
  46. data/lib/sidekiq_unique_jobs/lua/lock_until_expired.lua +92 -0
  47. data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
  48. data/lib/sidekiq_unique_jobs/lua/queue.lua +87 -0
  49. data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +122 -0
  50. data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +40 -0
  51. data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
  52. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +22 -0
  53. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +18 -0
  54. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_process_set.lua +53 -0
  55. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +43 -0
  56. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
  57. data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
  58. data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
  59. data/lib/sidekiq_unique_jobs/lua/unlock.lua +107 -0
  60. data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
  61. data/lib/sidekiq_unique_jobs/lua/upgrade.lua +68 -0
  62. data/lib/sidekiq_unique_jobs/middleware/client.rb +42 -0
  63. data/lib/sidekiq_unique_jobs/middleware/server.rb +31 -0
  64. data/lib/sidekiq_unique_jobs/middleware.rb +33 -30
  65. data/lib/sidekiq_unique_jobs/normalizer.rb +4 -4
  66. data/lib/sidekiq_unique_jobs/on_conflict/log.rb +9 -5
  67. data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +1 -1
  68. data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +2 -2
  69. data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +63 -17
  70. data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +54 -14
  71. data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +16 -5
  72. data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +26 -7
  73. data/lib/sidekiq_unique_jobs/on_conflict.rb +34 -16
  74. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +39 -36
  75. data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
  76. data/lib/sidekiq_unique_jobs/orphans/manager.rb +241 -0
  77. data/lib/sidekiq_unique_jobs/orphans/null_reaper.rb +24 -0
  78. data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
  79. data/lib/sidekiq_unique_jobs/orphans/reaper.rb +114 -0
  80. data/lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb +170 -0
  81. data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +298 -0
  82. data/lib/sidekiq_unique_jobs/redis/entity.rb +112 -0
  83. data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
  84. data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
  85. data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
  86. data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +86 -0
  87. data/lib/sidekiq_unique_jobs/redis/string.rb +51 -0
  88. data/lib/sidekiq_unique_jobs/redis.rb +11 -0
  89. data/lib/sidekiq_unique_jobs/reflectable.rb +26 -0
  90. data/lib/sidekiq_unique_jobs/reflections.rb +79 -0
  91. data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
  92. data/lib/sidekiq_unique_jobs/rspec/matchers.rb +26 -0
  93. data/lib/sidekiq_unique_jobs/script/caller.rb +127 -0
  94. data/lib/sidekiq_unique_jobs/script.rb +15 -0
  95. data/lib/sidekiq_unique_jobs/server.rb +61 -0
  96. data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +115 -66
  97. data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +304 -0
  98. data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +51 -29
  99. data/lib/sidekiq_unique_jobs/testing.rb +104 -31
  100. data/lib/sidekiq_unique_jobs/timer_task.rb +299 -0
  101. data/lib/sidekiq_unique_jobs/timing.rb +58 -0
  102. data/lib/sidekiq_unique_jobs/unlockable.rb +20 -4
  103. data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
  104. data/lib/sidekiq_unique_jobs/upgrade_locks.rb +155 -0
  105. data/lib/sidekiq_unique_jobs/version.rb +3 -1
  106. data/lib/sidekiq_unique_jobs/version_check.rb +114 -0
  107. data/lib/sidekiq_unique_jobs/web/helpers.rb +140 -15
  108. data/lib/sidekiq_unique_jobs/web/views/_paging.erb +4 -4
  109. data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +54 -0
  110. data/lib/sidekiq_unique_jobs/web/views/lock.erb +110 -0
  111. data/lib/sidekiq_unique_jobs/web/views/locks.erb +54 -0
  112. data/lib/sidekiq_unique_jobs/web.rb +86 -29
  113. data/lib/sidekiq_unique_jobs.rb +78 -105
  114. data/lib/tasks/changelog.rake +23 -0
  115. metadata +154 -218
  116. data/.codeclimate.yml +0 -35
  117. data/.csslintrc +0 -2
  118. data/.dockerignore +0 -4
  119. data/.editorconfig +0 -14
  120. data/.eslintignore +0 -1
  121. data/.eslintrc +0 -213
  122. data/.fasterer.yml +0 -23
  123. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -31
  124. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -17
  125. data/.gitignore +0 -28
  126. data/.reek.yml +0 -87
  127. data/.rspec +0 -2
  128. data/.rubocop.yml +0 -127
  129. data/.simplecov +0 -20
  130. data/.travis.yml +0 -47
  131. data/.yardopts +0 -7
  132. data/Appraisals +0 -29
  133. data/CODE_OF_CONDUCT.md +0 -74
  134. data/Gemfile +0 -24
  135. data/Guardfile +0 -55
  136. data/Rakefile +0 -12
  137. data/_config.yml +0 -1
  138. data/assets/unique_digests_1.png +0 -0
  139. data/assets/unique_digests_2.png +0 -0
  140. data/bin/bench +0 -20
  141. data/examples/another_unique_job.rb +0 -15
  142. data/examples/custom_queue_job.rb +0 -12
  143. data/examples/custom_queue_job_with_filter_method.rb +0 -13
  144. data/examples/custom_queue_job_with_filter_proc.rb +0 -16
  145. data/examples/expiring_job.rb +0 -12
  146. data/examples/inline_worker.rb +0 -12
  147. data/examples/just_a_worker.rb +0 -13
  148. data/examples/long_running_job.rb +0 -14
  149. data/examples/main_job.rb +0 -14
  150. data/examples/my_job.rb +0 -12
  151. data/examples/my_unique_job.rb +0 -15
  152. data/examples/my_unique_job_with_filter_method.rb +0 -21
  153. data/examples/my_unique_job_with_filter_proc.rb +0 -19
  154. data/examples/notify_worker.rb +0 -14
  155. data/examples/plain_class.rb +0 -13
  156. data/examples/simple_worker.rb +0 -15
  157. data/examples/spawn_simple_worker.rb +0 -12
  158. data/examples/test_class.rb +0 -9
  159. data/examples/unique_across_workers_job.rb +0 -20
  160. data/examples/unique_job_on_conflict_raise.rb +0 -14
  161. data/examples/unique_job_on_conflict_reject.rb +0 -14
  162. data/examples/unique_job_on_conflict_reschedule.rb +0 -14
  163. data/examples/unique_job_with_conditional_parameter.rb +0 -18
  164. data/examples/unique_job_with_filter_method.rb +0 -21
  165. data/examples/unique_job_with_nil_unique_args.rb +0 -20
  166. data/examples/unique_job_with_no_unique_args_method.rb +0 -16
  167. data/examples/unique_job_withthout_unique_args_parameter.rb +0 -18
  168. data/examples/unique_on_all_queues_job.rb +0 -16
  169. data/examples/until_and_while_executing_job.rb +0 -17
  170. data/examples/until_executed_2_job.rb +0 -24
  171. data/examples/until_executed_job.rb +0 -25
  172. data/examples/until_executing_job.rb +0 -11
  173. data/examples/until_expired_job.rb +0 -12
  174. data/examples/until_global_expired_job.rb +0 -12
  175. data/examples/while_executing_job.rb +0 -15
  176. data/examples/while_executing_reject_job.rb +0 -14
  177. data/examples/without_argument_job.rb +0 -13
  178. data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
  179. data/lib/sidekiq_unique_jobs/scripts.rb +0 -89
  180. data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -40
  181. data/lib/sidekiq_unique_jobs/timeout/calculator.rb +0 -63
  182. data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
  183. data/lib/sidekiq_unique_jobs/unique_args.rb +0 -149
  184. data/lib/sidekiq_unique_jobs/util.rb +0 -103
  185. data/lib/sidekiq_unique_jobs/web/views/unique_digest.erb +0 -28
  186. data/lib/sidekiq_unique_jobs/web/views/unique_digests.erb +0 -42
  187. data/redis/acquire_lock.lua +0 -21
  188. data/redis/delete.lua +0 -14
  189. data/redis/delete_by_digest.lua +0 -24
  190. data/redis/delete_job_by_digest.lua +0 -60
  191. data/redis/lock.lua +0 -58
  192. data/redis/release_stale_locks.lua +0 -90
  193. data/redis/unlock.lua +0 -32
  194. data/sidekiq-unique-jobs.gemspec +0 -42
@@ -1,114 +1,163 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'sidekiq/api'
3
+ require "sidekiq/api"
4
4
 
5
+ #
6
+ # @api private
7
+ #
5
8
  module Sidekiq
9
+ # See Sidekiq::Api
6
10
  class SortedEntry
11
+ #
12
+ # Provides extensions for unlocking jobs that are removed and deleted
13
+ #
14
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
15
+ #
7
16
  module UniqueExtension
8
- def self.included(base)
9
- base.class_eval do
10
- alias_method :delete_orig, :delete
11
- alias_method :delete, :delete_ext
12
- alias_method :remove_job_orig, :remove_job
13
- alias_method :remove_job, :remove_job_ext
14
- end
15
- end
16
-
17
- def delete_ext
18
- SidekiqUniqueJobs::Unlockable.unlock(item) if delete_orig
17
+ #
18
+ # Wraps the original method to ensure locks for the job are deleted
19
+ #
20
+ #
21
+ # @return [Hash] the deleted sidekiq job hash
22
+ #
23
+ def delete
24
+ SidekiqUniqueJobs::Unlockable.delete!(item) if super
25
+ item
19
26
  end
20
27
 
21
28
  private
22
29
 
23
- def remove_job_ext
24
- remove_job_orig do |message|
25
- SidekiqUniqueJobs::Unlockable.unlock(Sidekiq.load_json(message))
30
+ #
31
+ # Wraps the original method to ensure locks for the job are deleted
32
+ #
33
+ #
34
+ # @yieldparam [Hash] message the sidekiq job hash
35
+ def remove_job
36
+ super do |message|
37
+ SidekiqUniqueJobs::Unlockable.delete!(Sidekiq.load_json(message))
26
38
  yield message
27
39
  end
28
40
  end
29
41
  end
30
42
 
31
- include UniqueExtension
43
+ prepend UniqueExtension
32
44
  end
33
45
 
46
+ # See Sidekiq::Api
34
47
  class ScheduledSet
48
+ #
49
+ # Provides extensions for unlocking jobs that are removed and deleted
50
+ #
51
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
52
+ #
35
53
  module UniqueExtension
36
- def self.included(base)
37
- base.class_eval do
38
- alias_method :delete_orig, :delete
39
- alias_method :delete, :delete_ext
40
- end
41
- end
42
-
43
- def delete_ext
44
- SidekiqUniqueJobs::Unlockable.unlock(item) if delete_orig
54
+ #
55
+ # Wraps the original method to ensure locks for the job are deleted
56
+ #
57
+ #
58
+ # @param [Integer, Float] score the score in the scheduled set
59
+ # @param [String] job_id the Sidekiq JID
60
+ #
61
+ def delete(score, job_id)
62
+ entry = find_job(job_id)
63
+ SidekiqUniqueJobs::Unlockable.delete!(entry.item) if super(score, job_id)
64
+ entry
45
65
  end
46
66
  end
47
- include UniqueExtension
67
+
68
+ prepend UniqueExtension
48
69
  end
49
70
 
50
- class Job
51
- module UniqueExtension
52
- def self.included(base)
53
- base.class_eval do
54
- alias_method :delete_orig, :delete
55
- alias_method :delete, :delete_ext
71
+ if Sidekiq.const_defined?(:JobRecord)
72
+ # See Sidekiq::Api
73
+ class JobRecord
74
+ #
75
+ # Provides extensions for unlocking jobs that are removed and deleted
76
+ #
77
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
78
+ #
79
+ module UniqueExtension
80
+ #
81
+ # Wraps the original method to ensure locks for the job are deleted
82
+ #
83
+ def delete
84
+ SidekiqUniqueJobs::Unlockable.delete!(item)
85
+ super
56
86
  end
57
87
  end
58
88
 
59
- def delete_ext
60
- delete_orig
61
- SidekiqUniqueJobs::Unlockable.unlock(item)
62
- end
89
+ prepend UniqueExtension
63
90
  end
91
+ else
92
+ # See Sidekiq::Api
93
+ class Job
94
+ #
95
+ # Provides extensions for unlocking jobs that are removed and deleted
96
+ #
97
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
98
+ #
99
+ module UniqueExtension
100
+ #
101
+ # Wraps the original method to ensure locks for the job are deleted
102
+ #
103
+ def delete
104
+ SidekiqUniqueJobs::Unlockable.delete!(item)
105
+ super
106
+ end
107
+ end
64
108
 
65
- include UniqueExtension
109
+ prepend UniqueExtension
110
+ end
66
111
  end
67
112
 
113
+ # See Sidekiq::Api
68
114
  class Queue
115
+ #
116
+ # Provides extensions for unlocking jobs that are removed and deleted
117
+ #
118
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
119
+ #
69
120
  module UniqueExtension
70
- def self.included(base)
71
- base.class_eval do
72
- alias_method :clear_orig, :clear
73
- alias_method :clear, :clear_ext
74
- end
75
- end
76
-
77
- def clear_ext
121
+ #
122
+ # Wraps the original method to ensure locks for the job are deleted
123
+ #
124
+ def clear
78
125
  each(&:delete)
79
- clear_orig
126
+ super
80
127
  end
81
128
  end
82
129
 
83
- include UniqueExtension
130
+ prepend UniqueExtension
84
131
  end
85
132
 
133
+ # See Sidekiq::Api
86
134
  class JobSet
135
+ #
136
+ # Provides extensions for unlocking jobs that are removed and deleted
137
+ #
138
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
139
+ #
87
140
  module UniqueExtension
88
- def self.included(base)
89
- base.class_eval do
90
- if base.method_defined?(:clear)
91
- alias_method :clear_orig, :clear
92
- alias_method :clear, :clear_ext
93
- end
94
-
95
- if base.method_defined?(:delete_by_value)
96
- alias_method :delete_by_value_orig, :delete_by_value
97
- alias_method :delete_by_value, :delete_by_value_ext
98
- end
99
- end
100
- end
101
-
102
- def clear_ext
141
+ #
142
+ # Wraps the original method to ensure locks for the job are deleted
143
+ #
144
+ def clear
103
145
  each(&:delete)
104
- clear_orig
146
+ super
105
147
  end
106
148
 
107
- def delete_by_value_ext(name, value)
108
- SidekiqUniqueJobs::Unlockable.unlock(Sidekiq.load_json(value)) if delete_by_value_orig(name, value)
149
+ #
150
+ # Wraps the original method to ensure locks for the job are deleted
151
+ #
152
+ #
153
+ # @param [String] name the name of the key
154
+ # @param [String] value a sidekiq job hash
155
+ #
156
+ def delete_by_value(name, value)
157
+ SidekiqUniqueJobs::Unlockable.delete!(Sidekiq.load_json(value)) if super(name, value)
109
158
  end
110
159
  end
111
160
 
112
- include UniqueExtension
161
+ prepend UniqueExtension
113
162
  end
114
163
  end
@@ -0,0 +1,304 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Contains configuration and utility methods that belongs top level
5
+ #
6
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
7
+ module SidekiqUniqueJobs # rubocop:disable Metrics/ModuleLength
8
+ include SidekiqUniqueJobs::Connection
9
+ extend SidekiqUniqueJobs::JSON
10
+
11
+ module_function
12
+
13
+ #
14
+ # The current configuration (See: {.configure} on how to configure)
15
+ #
16
+ #
17
+ # @return [SidekiqUniqueJobs::Config] the gem configuration
18
+ #
19
+ def config
20
+ @config ||= reset! # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
21
+ end
22
+
23
+ #
24
+ # The current strategies
25
+ #
26
+ #
27
+ # @return [Hash<Symbol, SidekiqUniqueJobs::Strategy>] the configured locks
28
+ #
29
+ def strategies
30
+ config.strategies
31
+ end
32
+
33
+ #
34
+ # The current locks
35
+ #
36
+ #
37
+ # @return [Hash<Symbol, SidekiqUniqueJobs::BaseLock>] the configured locks
38
+ #
39
+ def locks
40
+ config.locks
41
+ end
42
+
43
+ #
44
+ # The current logger
45
+ #
46
+ #
47
+ # @return [Logger] the configured logger
48
+ #
49
+ def logger
50
+ config.logger
51
+ end
52
+
53
+ #
54
+ # The current gem version
55
+ #
56
+ #
57
+ # @return [String] the current gem version
58
+ #
59
+ def version
60
+ VERSION
61
+ end
62
+
63
+ #
64
+ # Set a new logger
65
+ #
66
+ # @param [Logger] other another logger
67
+ #
68
+ # @return [Logger] the new logger
69
+ #
70
+ def logger=(other)
71
+ config.logger = other
72
+ end
73
+
74
+ #
75
+ # Check if logging is enabled
76
+ #
77
+ #
78
+ # @return [true, false]
79
+ #
80
+ def logging?
81
+ config.logger_enabled
82
+ end
83
+
84
+ #
85
+ # Temporarily use another configuration and reset to the old config after yielding
86
+ #
87
+ # @param [Hash] tmp_config the temporary configuration to use
88
+ #
89
+ # @return [void]
90
+ #
91
+ # @yield control to the caller
92
+ def use_config(tmp_config = {})
93
+ raise ::ArgumentError, "#{name}.#{__method__} needs a block" unless block_given?
94
+
95
+ old_config = config.to_h
96
+ reset!
97
+ configure(tmp_config)
98
+ yield
99
+ ensure
100
+ reset!
101
+ configure(old_config.to_h)
102
+ end
103
+
104
+ #
105
+ # Resets configuration to deafult
106
+ #
107
+ #
108
+ # @return [SidekiqUniqueJobs::Config] a default gem configuration
109
+ #
110
+ def reset!
111
+ @config = SidekiqUniqueJobs::Config.default # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
112
+ end
113
+
114
+ #
115
+ # Enable SidekiqUniuqeJobs either temporarily in a block or for good
116
+ #
117
+ #
118
+ # @return [true] when not given a block
119
+ # @return [true, false] the previous value of enable when given a block
120
+ #
121
+ # @yieldreturn [void] temporarily enable sidekiq unique jobs while executing a block of code
122
+ def enable!(&block)
123
+ toggle(true, &block)
124
+ end
125
+
126
+ #
127
+ # Disable SidekiqUniuqeJobs either temporarily in a block or for good
128
+ #
129
+ #
130
+ # @return [false] when not given a block
131
+ # @return [true, false] the previous value of enable when given a block
132
+ #
133
+ # @yieldreturn [void] temporarily disable sidekiq unique jobs while executing a block of code
134
+ def disable!(&block)
135
+ toggle(false, &block)
136
+ end
137
+
138
+ #
139
+ # Checks if the gem has been disabled
140
+ #
141
+ # @return [true] when config.enabled is true
142
+ # @return [false] when config.enabled is false
143
+ #
144
+ def enabled?
145
+ config.enabled
146
+ end
147
+
148
+ #
149
+ # Checks if the gem has been disabled
150
+ #
151
+ # @return [true] when config.enabled is false
152
+ # @return [false] when config.enabled is true
153
+ #
154
+ def disabled?
155
+ !enabled?
156
+ end
157
+
158
+ #
159
+ # Toggles enabled on or off
160
+ #
161
+ # @api private
162
+ # :nodoc:
163
+ def toggle(enabled)
164
+ if block_given?
165
+ enabled_was = config.enabled
166
+ config.enabled = enabled
167
+ yield
168
+ config.enabled = enabled_was
169
+ else
170
+ config.enabled = enabled
171
+ end
172
+ end
173
+
174
+ # Configure the gem
175
+ #
176
+ # This is usually called once at startup of an application
177
+ # @param [Hash] options global gem options
178
+ # @option options [Integer] :lock_timeout (default is 0)
179
+ # @option options [Integer] :lock_ttl (default is 0)
180
+ # @option options [true,false] :enabled (default is true)
181
+ # @option options [String] :lock_prefix (default is 'uniquejobs')
182
+ # @option options [Logger] :logger (default is Sidekiq.logger)
183
+ # @yield control to the caller when given block
184
+ def configure(options = {})
185
+ if block_given?
186
+ yield config
187
+ else
188
+ options.each do |key, val|
189
+ config.send(:"#{key}=", val)
190
+ end
191
+ end
192
+ end
193
+
194
+ #
195
+ # Returns the current redis version
196
+ #
197
+ #
198
+ # @return [String] a string like `5.0.2`
199
+ #
200
+ def fetch_redis_version
201
+ Sidekiq.redis_info["redis_version"]
202
+ end
203
+
204
+ #
205
+ # Current time as float
206
+ #
207
+ #
208
+ # @return [Float]
209
+ #
210
+ def now_f
211
+ now.to_f
212
+ end
213
+
214
+ #
215
+ # Current time
216
+ #
217
+ #
218
+ # @return [Time]
219
+ #
220
+ def now
221
+ Time.now
222
+ end
223
+
224
+ #
225
+ # Checks that the worker is valid with the given options
226
+ #
227
+ # @param [Hash] options the `sidekiq_options` to validate
228
+ #
229
+ # @return [Boolean]
230
+ #
231
+ def validate_worker(options)
232
+ raise NotUniqueWorker, options unless (lock_type = options[LOCK])
233
+
234
+ lock_class = locks[lock_type]
235
+ lock_class.validate_options(options)
236
+ end
237
+
238
+ #
239
+ # Checks that the worker is valid with the given options
240
+ #
241
+ # @param [Hash] options the `sidekiq_options` to validate
242
+ #
243
+ # @raise [InvalidWorker] when {#validate_worker} returns false or nil
244
+ #
245
+ def validate_worker!(options)
246
+ lock_config = validate_worker(options)
247
+ raise InvalidWorker, lock_config unless lock_config.errors.empty?
248
+ end
249
+
250
+ # Attempt to constantize a string worker_class argument, always
251
+ # failing back to the original argument when the constant can't be found
252
+ #
253
+ # @return [Sidekiq::Worker]
254
+ def constantize(str)
255
+ return str.class if str.is_a?(Sidekiq::Worker) # sidekiq v6.x
256
+ return str unless str.is_a?(String)
257
+ return Object.const_get(str) unless str.include?("::")
258
+
259
+ names = str.split("::")
260
+ names.shift if names.empty? || names.first.empty?
261
+
262
+ names.inject(Object) do |constant, name|
263
+ # the false flag limits search for name to under the constant namespace
264
+ # which mimics Rails' behaviour
265
+ constant.const_get(name, false)
266
+ end
267
+ end
268
+
269
+ # Attempt to constantize a string worker_class argument, always
270
+ # failing back to the original argument when the constant can't be found
271
+ #
272
+ # @return [Sidekiq::Worker, String]
273
+ def safe_constantize(str)
274
+ constantize(str)
275
+ rescue NameError => ex
276
+ case ex.message
277
+ when /uninitialized constant/
278
+ str
279
+ else
280
+ raise
281
+ end
282
+ end
283
+
284
+ #
285
+ # Collection with notifications
286
+ #
287
+ #
288
+ # @return [Reflections]
289
+ #
290
+ def reflections
291
+ @reflections ||= Reflections.new # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
292
+ end
293
+
294
+ #
295
+ # Yields notification stack for sidekiq unique jobs to configure notifications
296
+ #
297
+ #
298
+ # @return [void] <description>
299
+ #
300
+ # @yieldparam [Reflections] x used to configure notifications
301
+ def reflect
302
+ yield reflections if block_given?
303
+ end
304
+ end
@@ -3,60 +3,82 @@
3
3
  module SidekiqUniqueJobs
4
4
  # Module with convenience methods for the Sidekiq::Worker class
5
5
  #
6
- # @author Mikael Henriksson <mikael@zoolutions.se>
6
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
7
7
  module SidekiqWorkerMethods
8
+ #
9
+ # @!attribute [r] job_class
10
+ # @return [Sidekiq::Worker] The Sidekiq::Worker implementation
11
+ attr_reader :job_class
12
+
8
13
  # Avoids duplicating worker_class.respond_to? in multiple places
9
14
  # @return [true, false]
10
- def worker_method_defined?(method_sym)
11
- worker_class.respond_to?(method_sym)
15
+ def job_method_defined?(method_sym)
16
+ job_class.respond_to?(method_sym)
12
17
  end
13
18
 
14
19
  # Wraps #get_sidekiq_options to always work with a hash
15
20
  # @return [Hash] of the worker class sidekiq options
16
- def worker_options
17
- return {} unless sidekiq_worker_class?
21
+ def job_options
22
+ return {} unless sidekiq_job_class?
18
23
 
19
- worker_class.get_sidekiq_options.stringify_keys
24
+ job_class.get_sidekiq_options.deep_stringify_keys
20
25
  end
21
26
 
22
27
  # Tests that the
23
- # @return [true] if worker_class responds to get_sidekiq_options
24
- # @return [false] if worker_class does not respond to get_sidekiq_options
25
- def sidekiq_worker_class?
26
- worker_method_defined?(:get_sidekiq_options)
28
+ # @return [true] if job_class responds to get_sidekiq_options
29
+ # @return [false] if job_class does not respond to get_sidekiq_options
30
+ def sidekiq_job_class?
31
+ job_method_defined?(:get_sidekiq_options)
27
32
  end
28
33
 
29
- # The Sidekiq::Worker implementation
30
- # @return [Sidekiq::Worker]
31
- def worker_class
32
- @_worker_class ||= worker_class_constantize # rubocop:disable Naming/MemoizedInstanceVariableName
34
+ def job_class=(obj)
35
+ # this is what was originally passed in, it can be an instance or a class depending on sidekiq version
36
+ @original_job_class = obj
37
+ @job_class = job_class_constantize(obj)
33
38
  end
34
39
 
35
40
  # The hook to call after a successful unlock
36
41
  # @return [Proc]
37
- def after_unlock_hook
38
- -> { worker_class.after_unlock if worker_method_defined?(:after_unlock) }
42
+ def after_unlock_hook # rubocop:disable Metrics/MethodLength
43
+ lambda do
44
+ if @original_job_class.respond_to?(:after_unlock)
45
+ # instance method in sidekiq v6
46
+ if @original_job_class.method(:after_unlock).arity.positive? # arity check to maintain backwards compatibility
47
+ @original_job_class.after_unlock(item)
48
+ else
49
+ @original_job_class.after_unlock
50
+ end
51
+ elsif job_class.respond_to?(:after_unlock)
52
+ # class method regardless of sidekiq version
53
+ if job_class.method(:after_unlock).arity.positive? # arity check to maintain backwards compatibility
54
+ job_class.after_unlock(item)
55
+ else
56
+ job_class.after_unlock
57
+ end
58
+ end
59
+ end
39
60
  end
40
61
 
41
62
  # Attempt to constantize a string worker_class argument, always
42
63
  # failing back to the original argument when the constant can't be found
43
64
  #
44
65
  # @return [Sidekiq::Worker]
45
- def worker_class_constantize(klazz = @worker_class)
46
- return klazz unless klazz.is_a?(String)
47
-
48
- Object.const_get(klazz)
49
- rescue NameError => ex
50
- case ex.message
51
- when /uninitialized constant/
52
- klazz
53
- else
54
- raise
55
- end
66
+ def job_class_constantize(klazz = @job_class)
67
+ SidekiqUniqueJobs.safe_constantize(klazz)
56
68
  end
57
69
 
58
- def default_worker_options
59
- Sidekiq.default_worker_options
70
+ #
71
+ # Returns the default worker options from Sidekiq
72
+ #
73
+ #
74
+ # @return [Hash<Symbol, Object>]
75
+ #
76
+ def default_job_options
77
+ if Sidekiq.respond_to?(:default_job_options)
78
+ Sidekiq.default_job_options
79
+ else
80
+ Sidekiq.default_worker_options
81
+ end
60
82
  end
61
83
  end
62
84
  end