sidekiq-unique-jobs 4.0.18 → 7.1.28

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 (296) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1889 -94
  3. data/LICENSE.txt +21 -0
  4. data/README.md +927 -119
  5. data/bin/uniquejobs +7 -0
  6. data/lib/sidekiq-unique-jobs.rb +2 -78
  7. data/lib/sidekiq_unique_jobs/batch_delete.rb +124 -0
  8. data/lib/sidekiq_unique_jobs/changelog.rb +78 -0
  9. data/lib/sidekiq_unique_jobs/cli.rb +79 -21
  10. data/lib/sidekiq_unique_jobs/config.rb +310 -14
  11. data/lib/sidekiq_unique_jobs/connection.rb +23 -0
  12. data/lib/sidekiq_unique_jobs/constants.rb +52 -16
  13. data/lib/sidekiq_unique_jobs/core_ext.rb +114 -33
  14. data/lib/sidekiq_unique_jobs/deprecation.rb +65 -0
  15. data/lib/sidekiq_unique_jobs/digests.rb +111 -0
  16. data/lib/sidekiq_unique_jobs/exceptions.rb +105 -0
  17. data/lib/sidekiq_unique_jobs/expiring_digests.rb +14 -0
  18. data/lib/sidekiq_unique_jobs/job.rb +58 -0
  19. data/lib/sidekiq_unique_jobs/json.rb +47 -0
  20. data/lib/sidekiq_unique_jobs/key.rb +98 -0
  21. data/lib/sidekiq_unique_jobs/lock/base_lock.rb +165 -0
  22. data/lib/sidekiq_unique_jobs/lock/client_validator.rb +28 -0
  23. data/lib/sidekiq_unique_jobs/lock/server_validator.rb +27 -0
  24. data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +65 -7
  25. data/lib/sidekiq_unique_jobs/lock/until_executed.rb +34 -64
  26. data/lib/sidekiq_unique_jobs/lock/until_executing.rb +37 -4
  27. data/lib/sidekiq_unique_jobs/lock/until_expired.rb +42 -0
  28. data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
  29. data/lib/sidekiq_unique_jobs/lock/while_executing.rb +53 -35
  30. data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +21 -0
  31. data/lib/sidekiq_unique_jobs/lock.rb +338 -6
  32. data/lib/sidekiq_unique_jobs/lock_args.rb +127 -0
  33. data/lib/sidekiq_unique_jobs/lock_config.rb +126 -0
  34. data/lib/sidekiq_unique_jobs/lock_digest.rb +79 -0
  35. data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
  36. data/lib/sidekiq_unique_jobs/lock_timeout.rb +62 -0
  37. data/lib/sidekiq_unique_jobs/lock_ttl.rb +77 -0
  38. data/lib/sidekiq_unique_jobs/locksmith.rb +383 -0
  39. data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
  40. data/lib/sidekiq_unique_jobs/logging.rb +236 -0
  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 +34 -29
  65. data/lib/sidekiq_unique_jobs/normalizer.rb +11 -1
  66. data/lib/sidekiq_unique_jobs/on_conflict/log.rb +24 -0
  67. data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +16 -0
  68. data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +17 -0
  69. data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +118 -0
  70. data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +82 -0
  71. data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +35 -0
  72. data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +51 -0
  73. data/lib/sidekiq_unique_jobs/on_conflict.rb +44 -0
  74. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +54 -31
  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 +116 -104
  97. data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +304 -0
  98. data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +84 -0
  99. data/lib/sidekiq_unique_jobs/testing.rb +115 -50
  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 +29 -33
  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 +5 -1
  106. data/lib/sidekiq_unique_jobs/version_check.rb +114 -0
  107. data/lib/sidekiq_unique_jobs/web/helpers.rb +173 -0
  108. data/lib/sidekiq_unique_jobs/web/views/_paging.erb +10 -0
  109. data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +54 -0
  110. data/lib/sidekiq_unique_jobs/web/views/lock.erb +108 -0
  111. data/lib/sidekiq_unique_jobs/web/views/locks.erb +54 -0
  112. data/lib/sidekiq_unique_jobs/web.rb +109 -0
  113. data/lib/sidekiq_unique_jobs.rb +81 -0
  114. data/lib/tasks/changelog.rake +23 -0
  115. metadata +173 -251
  116. data/.codeclimate.yml +0 -22
  117. data/.dockerignore +0 -4
  118. data/.editorconfig +0 -14
  119. data/.fasterer.yml +0 -23
  120. data/.gitignore +0 -14
  121. data/.rspec +0 -3
  122. data/.rubocop.yml +0 -45
  123. data/.simplecov +0 -12
  124. data/.travis.yml +0 -43
  125. data/Appraisals +0 -31
  126. data/Gemfile +0 -16
  127. data/LICENSE +0 -13
  128. data/Rakefile +0 -12
  129. data/bin/bench +0 -19
  130. data/bin/jobs +0 -6
  131. data/circle.yml +0 -38
  132. data/gemfiles/sidekiq_2.15.gemfile +0 -9
  133. data/gemfiles/sidekiq_2.16.gemfile +0 -9
  134. data/gemfiles/sidekiq_2.17.gemfile +0 -19
  135. data/gemfiles/sidekiq_3.0.gemfile +0 -19
  136. data/gemfiles/sidekiq_3.1.gemfile +0 -19
  137. data/gemfiles/sidekiq_3.2.gemfile +0 -19
  138. data/gemfiles/sidekiq_3.3.gemfile +0 -19
  139. data/gemfiles/sidekiq_4.0.gemfile +0 -19
  140. data/gemfiles/sidekiq_4.1.gemfile +0 -19
  141. data/gemfiles/sidekiq_develop.gemfile +0 -19
  142. data/lib/sidekiq/simulator.rb +0 -75
  143. data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -41
  144. data/lib/sidekiq_unique_jobs/lock/until_timeout.rb +0 -15
  145. data/lib/sidekiq_unique_jobs/run_lock_failed.rb +0 -1
  146. data/lib/sidekiq_unique_jobs/script_mock.rb +0 -68
  147. data/lib/sidekiq_unique_jobs/scripts.rb +0 -49
  148. data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -34
  149. data/lib/sidekiq_unique_jobs/testing/sidekiq_overrides.rb +0 -70
  150. data/lib/sidekiq_unique_jobs/timeout_calculator.rb +0 -65
  151. data/lib/sidekiq_unique_jobs/unique_args.rb +0 -134
  152. data/lib/sidekiq_unique_jobs/util.rb +0 -91
  153. data/rails_example/.env +0 -12
  154. data/rails_example/.gitignore +0 -13
  155. data/rails_example/.rspec +0 -2
  156. data/rails_example/Gemfile +0 -34
  157. data/rails_example/Procfile +0 -2
  158. data/rails_example/README.rdoc +0 -28
  159. data/rails_example/Rakefile +0 -6
  160. data/rails_example/app/assets/images/.keep +0 -0
  161. data/rails_example/app/assets/javascripts/application.js +0 -16
  162. data/rails_example/app/assets/stylesheets/application.css +0 -15
  163. data/rails_example/app/channels/appearance_channel.rb +0 -17
  164. data/rails_example/app/channels/application_cable/channel.rb +0 -4
  165. data/rails_example/app/channels/application_cable/connection.rb +0 -9
  166. data/rails_example/app/channels/post_channel.rb +0 -5
  167. data/rails_example/app/controllers/application_controller.rb +0 -5
  168. data/rails_example/app/controllers/concerns/.keep +0 -0
  169. data/rails_example/app/controllers/work_controller.rb +0 -13
  170. data/rails_example/app/helpers/application_helper.rb +0 -2
  171. data/rails_example/app/mailers/.keep +0 -0
  172. data/rails_example/app/models/.keep +0 -0
  173. data/rails_example/app/models/application_record.rb +0 -3
  174. data/rails_example/app/models/concerns/.keep +0 -0
  175. data/rails_example/app/models/guest.rb +0 -21
  176. data/rails_example/app/models/post.rb +0 -2
  177. data/rails_example/app/views/layouts/application.html.erb +0 -15
  178. data/rails_example/app/workers/simple_worker.rb +0 -12
  179. data/rails_example/app/workers/spawn_simple_worker.rb +0 -7
  180. data/rails_example/bin/bundle +0 -3
  181. data/rails_example/bin/check_or_setup_db +0 -57
  182. data/rails_example/bin/docker-setup +0 -20
  183. data/rails_example/bin/rails +0 -4
  184. data/rails_example/bin/rake +0 -4
  185. data/rails_example/bin/setup +0 -34
  186. data/rails_example/bin/update +0 -29
  187. data/rails_example/cable.ru +0 -6
  188. data/rails_example/common-services.yml +0 -50
  189. data/rails_example/config/application.rb +0 -27
  190. data/rails_example/config/boot.rb +0 -3
  191. data/rails_example/config/cable.yml +0 -9
  192. data/rails_example/config/database.docker.yml +0 -12
  193. data/rails_example/config/database.yml +0 -19
  194. data/rails_example/config/environment.rb +0 -5
  195. data/rails_example/config/environments/development.rb +0 -54
  196. data/rails_example/config/environments/production.rb +0 -86
  197. data/rails_example/config/environments/test.rb +0 -42
  198. data/rails_example/config/initializers/application_controller_renderer.rb +0 -6
  199. data/rails_example/config/initializers/assets.rb +0 -11
  200. data/rails_example/config/initializers/backtrace_silencers.rb +0 -9
  201. data/rails_example/config/initializers/cookies_serializer.rb +0 -5
  202. data/rails_example/config/initializers/filter_parameter_logging.rb +0 -4
  203. data/rails_example/config/initializers/inflections.rb +0 -16
  204. data/rails_example/config/initializers/mime_types.rb +0 -4
  205. data/rails_example/config/initializers/new_framework_defaults.rb +0 -23
  206. data/rails_example/config/initializers/session_store.rb +0 -3
  207. data/rails_example/config/initializers/sidekiq.rb +0 -13
  208. data/rails_example/config/initializers/wrap_parameters.rb +0 -14
  209. data/rails_example/config/locales/en.yml +0 -23
  210. data/rails_example/config/puma.rb +0 -38
  211. data/rails_example/config/routes.rb +0 -6
  212. data/rails_example/config/secrets.yml +0 -22
  213. data/rails_example/config/sidekiq.yml +0 -6
  214. data/rails_example/config/spring.rb +0 -6
  215. data/rails_example/config.ru +0 -4
  216. data/rails_example/db/migrate/20160724111322_create_posts.rb +0 -12
  217. data/rails_example/db/schema.rb +0 -25
  218. data/rails_example/db/seeds.rb +0 -7
  219. data/rails_example/dev-entrypoint.sh +0 -55
  220. data/rails_example/dev.env +0 -12
  221. data/rails_example/docker/rails.Dockerfile +0 -27
  222. data/rails_example/docker-compose.yml +0 -90
  223. data/rails_example/lib/assets/.keep +0 -0
  224. data/rails_example/lib/tasks/.keep +0 -0
  225. data/rails_example/log/.keep +0 -0
  226. data/rails_example/public/404.html +0 -67
  227. data/rails_example/public/422.html +0 -67
  228. data/rails_example/public/500.html +0 -66
  229. data/rails_example/public/favicon.ico +0 -0
  230. data/rails_example/public/robots.txt +0 -5
  231. data/rails_example/simple.ru +0 -12
  232. data/rails_example/spec/controllers/work_controller_spec.rb +0 -172
  233. data/rails_example/spec/factories/posts.rb +0 -8
  234. data/rails_example/spec/models/post_spec.rb +0 -4
  235. data/rails_example/spec/rails_helper.rb +0 -21
  236. data/rails_example/spec/spec_helper.rb +0 -20
  237. data/rails_example/spec/support/sidekiq_meta.rb +0 -12
  238. data/rails_example/spec/workers/simple_worker_spec.rb +0 -4
  239. data/rails_example/vendor/assets/javascripts/.keep +0 -0
  240. data/rails_example/vendor/assets/stylesheets/.keep +0 -0
  241. data/redis/acquire_lock.lua +0 -19
  242. data/redis/release_lock.lua +0 -16
  243. data/redis/synchronize.lua +0 -16
  244. data/sidekiq-unique-jobs.gemspec +0 -25
  245. data/spec/celluloid_with_fallback.rb +0 -18
  246. data/spec/jobs/another_unique_job.rb +0 -13
  247. data/spec/jobs/custom_queue_job.rb +0 -6
  248. data/spec/jobs/custom_queue_job_with_filter_method.rb +0 -7
  249. data/spec/jobs/custom_queue_job_with_filter_proc.rb +0 -10
  250. data/spec/jobs/expiring_job.rb +0 -4
  251. data/spec/jobs/inline_worker.rb +0 -8
  252. data/spec/jobs/just_a_worker.rb +0 -8
  253. data/spec/jobs/long_running_job.rb +0 -7
  254. data/spec/jobs/main_job.rb +0 -8
  255. data/spec/jobs/my_job.rb +0 -12
  256. data/spec/jobs/my_unique_job.rb +0 -12
  257. data/spec/jobs/my_unique_job_with_filter_method.rb +0 -17
  258. data/spec/jobs/my_unique_job_with_filter_proc.rb +0 -15
  259. data/spec/jobs/notify_worker.rb +0 -10
  260. data/spec/jobs/plain_class.rb +0 -4
  261. data/spec/jobs/simple_worker.rb +0 -11
  262. data/spec/jobs/spawn_simple_worker.rb +0 -8
  263. data/spec/jobs/test_class.rb +0 -4
  264. data/spec/jobs/unique_job_with_filter_method.rb +0 -18
  265. data/spec/jobs/unique_on_all_queues_job.rb +0 -13
  266. data/spec/jobs/until_and_while_executing_job.rb +0 -8
  267. data/spec/jobs/until_executed_job.rb +0 -17
  268. data/spec/jobs/until_executing_job.rb +0 -8
  269. data/spec/jobs/until_global_timeout_job.rb +0 -8
  270. data/spec/jobs/until_timeout_job.rb +0 -8
  271. data/spec/jobs/while_executing_job.rb +0 -12
  272. data/spec/lib/sidekiq_unique_jobs/client/middleware_spec.rb +0 -321
  273. data/spec/lib/sidekiq_unique_jobs/core_ext_spec.rb +0 -25
  274. data/spec/lib/sidekiq_unique_jobs/lock/until_and_while_executing_spec.rb +0 -43
  275. data/spec/lib/sidekiq_unique_jobs/lock/until_executed_spec.rb +0 -37
  276. data/spec/lib/sidekiq_unique_jobs/lock/until_timeout_spec.rb +0 -26
  277. data/spec/lib/sidekiq_unique_jobs/lock/while_executing_spec.rb +0 -86
  278. data/spec/lib/sidekiq_unique_jobs/normalizer_spec.rb +0 -21
  279. data/spec/lib/sidekiq_unique_jobs/options_with_fallback_spec.rb +0 -113
  280. data/spec/lib/sidekiq_unique_jobs/queue_lock_timeout_calculator_spec.rb +0 -47
  281. data/spec/lib/sidekiq_unique_jobs/run_lock_timeout_calculator_spec.rb +0 -36
  282. data/spec/lib/sidekiq_unique_jobs/script_mock_spec.rb +0 -84
  283. data/spec/lib/sidekiq_unique_jobs/scripts_spec.rb +0 -77
  284. data/spec/lib/sidekiq_unique_jobs/server/middleware_spec.rb +0 -83
  285. data/spec/lib/sidekiq_unique_jobs/sidekiq_testing_enabled_spec.rb +0 -181
  286. data/spec/lib/sidekiq_unique_jobs/sidekiq_unique_ext_spec.rb +0 -80
  287. data/spec/lib/sidekiq_unique_jobs/sidekiq_unique_jobs_spec.rb +0 -41
  288. data/spec/lib/sidekiq_unique_jobs/timeout_calculator_spec.rb +0 -70
  289. data/spec/lib/sidekiq_unique_jobs/unique_args_spec.rb +0 -131
  290. data/spec/lib/sidekiq_unique_jobs/unlockable_spec.rb +0 -4
  291. data/spec/lib/sidekiq_unique_jobs/util_spec.rb +0 -53
  292. data/spec/spec_helper.rb +0 -65
  293. data/spec/support/matchers/redis_matchers.rb +0 -19
  294. data/spec/support/ruby_meta.rb +0 -10
  295. data/spec/support/sidekiq_meta.rb +0 -31
  296. data/spec/support/unique_macros.rb +0 -71
@@ -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!
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
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
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
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ # Module with convenience methods for the Sidekiq::Worker class
5
+ #
6
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
7
+ module SidekiqWorkerMethods
8
+ #
9
+ # @!attribute [r] job_class
10
+ # @return [Sidekiq::Worker] The Sidekiq::Worker implementation
11
+ attr_reader :job_class
12
+
13
+ # Avoids duplicating worker_class.respond_to? in multiple places
14
+ # @return [true, false]
15
+ def job_method_defined?(method_sym)
16
+ job_class.respond_to?(method_sym)
17
+ end
18
+
19
+ # Wraps #get_sidekiq_options to always work with a hash
20
+ # @return [Hash] of the worker class sidekiq options
21
+ def job_options
22
+ return {} unless sidekiq_job_class?
23
+
24
+ job_class.get_sidekiq_options.deep_stringify_keys
25
+ end
26
+
27
+ # Tests that the
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)
32
+ end
33
+
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)
38
+ end
39
+
40
+ # The hook to call after a successful unlock
41
+ # @return [Proc]
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
60
+ end
61
+
62
+ # Attempt to constantize a string worker_class argument, always
63
+ # failing back to the original argument when the constant can't be found
64
+ #
65
+ # @return [Sidekiq::Worker]
66
+ def job_class_constantize(klazz = @job_class)
67
+ SidekiqUniqueJobs.safe_constantize(klazz)
68
+ end
69
+
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
82
+ end
83
+ end
84
+ end
@@ -1,70 +1,135 @@
1
- require 'sidekiq_unique_jobs/testing/sidekiq_overrides'
2
- require 'sidekiq_unique_jobs/script_mock'
3
-
4
- module SidekiqUniqueJobs
5
- alias redis_version_real redis_version
6
- def redis_version
7
- if mocked?
8
- '0.0'
1
+ # frozen_string_literal: true
2
+
3
+ # :nocov:
4
+ # :nodoc:
5
+
6
+ require "sidekiq"
7
+ require "sidekiq/testing"
8
+ require "sidekiq_unique_jobs/rspec/matchers"
9
+ require "sidekiq_unique_jobs/lock/validator"
10
+ require "sidekiq_unique_jobs/lock/client_validator"
11
+ require "sidekiq_unique_jobs/lock/server_validator"
12
+
13
+ #
14
+ # See Sidekiq gem for more details
15
+ #
16
+ module Sidekiq
17
+ #
18
+ # Temporarily turn Sidekiq's options into something different
19
+ #
20
+ # @note this method will restore the original options after yielding
21
+ #
22
+ # @param [Hash<Symbol, Object>] tmp_config the temporary config to use
23
+ #
24
+ def self.use_options(tmp_config = {}) # rubocop:disable Metrics/MethodLength
25
+ if respond_to?(:default_job_options)
26
+ default_job_options.clear
27
+ self.default_job_options = tmp_config
9
28
  else
10
- redis_version_real
29
+ default_worker_options.clear
30
+ self.default_worker_options = tmp_config
31
+ end
32
+
33
+ yield
34
+ ensure
35
+ if respond_to?(:default_job_options)
36
+ default_job_options.clear
37
+ self.default_job_options = default_job_options
38
+ else
39
+ default_worker_options.clear
40
+ self.default_worker_options = DEFAULT_WORKER_OPTIONS
11
41
  end
12
42
  end
13
43
 
14
- module Scripts
15
- module Overrides
16
- def self.included(base)
17
- base.extend Testing
18
- base.class_eval do
19
- class << self
20
- alias_method :call_orig, :call
21
- alias_method :call, :call_ext
22
- end
23
- end
24
- end
44
+ #
45
+ # See Sidekiq::Worker in Sidekiq gem for more details
46
+ #
47
+ module Worker
48
+ #
49
+ # Adds class methods to Sidekiq::Worker
50
+ #
51
+ module ClassMethods
52
+ #
53
+ # Temporarily turn a workers sidekiq_options into something different
54
+ #
55
+ # @note this method will restore the original configuration after yielding
56
+ #
57
+ # @param [Hash<Symbol, Object>] tmp_config the temporary config to use
58
+ #
59
+ def use_options(tmp_config = {})
60
+ old_options = sidekiq_options_hash.dup
61
+ sidekiq_options(old_options.merge(tmp_config))
25
62
 
26
- module Testing
27
- def call_ext(file_name, redis_pool, options = {})
28
- if SidekiqUniqueJobs.config.redis_test_mode == :mock
29
- SidekiqUniqueJobs::ScriptMock.call(file_name, redis_pool, options)
63
+ yield
64
+ ensure
65
+ self.sidekiq_options_hash =
66
+ if Sidekiq.respond_to?(:default_job_options)
67
+ Sidekiq.default_job_options
30
68
  else
31
- call_orig(file_name, redis_pool, options)
69
+ DEFAULT_WORKER_OPTIONS
32
70
  end
33
- end
71
+
72
+ sidekiq_options(old_options)
34
73
  end
35
74
  end
36
75
 
37
- include Overrides
38
- end
76
+ #
77
+ # Prepends deletion of locks to clear_all
78
+ #
79
+ module Overrides
80
+ #
81
+ # Overrides sidekiq_options on the worker class to prepend validation
82
+ #
83
+ # @param [Hash] options worker options
84
+ #
85
+ # @return [void]
86
+ #
87
+ def sidekiq_options(options = {})
88
+ SidekiqUniqueJobs.validate_worker!(options) if SidekiqUniqueJobs.config.raise_on_config_error
39
89
 
40
- module Client
41
- class Middleware
42
- alias call_real call
43
- def call(worker_class, item, queue, redis_pool = nil)
44
- worker_class = SidekiqUniqueJobs.worker_class_constantize(worker_class)
45
-
46
- if Sidekiq::Testing.inline?
47
- _server.call(worker_class.new, item, queue, redis_pool) do
48
- call_real(worker_class, item, queue, redis_pool) do
49
- yield
50
- end
51
- end
52
- else
53
- call_real(worker_class, item, queue, redis_pool) do
54
- yield
90
+ super(options)
91
+ end
92
+
93
+ #
94
+ # Prepends deletion of locks to clear
95
+ #
96
+ module ClassMethods
97
+ #
98
+ # Clears the jobs for this worker and removes all locks
99
+ #
100
+ def clear
101
+ jobs.each do |job|
102
+ SidekiqUniqueJobs::Unlockable.unlock(job)
55
103
  end
104
+
105
+ super
56
106
  end
57
107
  end
108
+ end
58
109
 
59
- def _server
60
- SidekiqUniqueJobs::Server::Middleware.new
61
- end
110
+ prepend Overrides
111
+
112
+ #
113
+ # Prepends methods to Sidekiq::Worker
114
+ #
115
+ module ClassMethods
116
+ prepend Overrides::ClassMethods
62
117
  end
63
- end
64
118
 
65
- class Testing
66
- def mocking!
67
- require 'sidekiq_unique_jobs/testing/mocking'
119
+ #
120
+ # Prepends singleton methods to Sidekiq::Worker
121
+ #
122
+ module SignletonOverrides
123
+ #
124
+ # Clears all jobs for this worker and removes all locks
125
+ #
126
+ def clear_all
127
+ super
128
+
129
+ SidekiqUniqueJobs::Digests.new.delete_by_pattern("*", count: 10_000)
130
+ end
68
131
  end
132
+
133
+ singleton_class.prepend SignletonOverrides
69
134
  end
70
135
  end