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,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ #
5
+ # Class Changelogs provides access to the changelog entries
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ #
9
+ class Digests < Redis::SortedSet
10
+ #
11
+ # @return [Integer] the number of matches to return by default
12
+ DEFAULT_COUNT = 1_000
13
+ #
14
+ # @return [String] the default pattern to use for matching
15
+ SCAN_PATTERN = "*"
16
+
17
+ def initialize(digests_key = DIGESTS)
18
+ super(digests_key)
19
+ end
20
+
21
+ #
22
+ # Adds a digest
23
+ #
24
+ # @param [String] digest the digest to add
25
+ #
26
+ def add(digest)
27
+ redis { |conn| conn.zadd(key, now_f, digest) }
28
+ end
29
+
30
+ # Deletes unique digests by pattern
31
+ #
32
+ # @param [String] pattern a key pattern to match with
33
+ # @param [Integer] count the maximum number
34
+ # @return [Hash<String,Float>] Hash mapping of digest matching the given pattern and score
35
+
36
+ def delete_by_pattern(pattern, count: DEFAULT_COUNT)
37
+ result, elapsed = timed do
38
+ digests = entries(pattern: pattern, count: count).keys
39
+ redis { |conn| BatchDelete.call(digests, conn) }
40
+ end
41
+
42
+ log_info("#{__method__}(#{pattern}, count: #{count}) completed in #{elapsed}ms")
43
+
44
+ result
45
+ end
46
+
47
+ # Delete unique digests by digest
48
+ # Also deletes the :AVAILABLE, :EXPIRED etc keys
49
+ #
50
+ # @param [String] digest a unique digest to delete
51
+ def delete_by_digest(digest) # rubocop:disable Metrics/MethodLength
52
+ result, elapsed = timed do
53
+ call_script(:delete_by_digest, [
54
+ digest,
55
+ "#{digest}:QUEUED",
56
+ "#{digest}:PRIMED",
57
+ "#{digest}:LOCKED",
58
+ "#{digest}:RUN",
59
+ "#{digest}:RUN:QUEUED",
60
+ "#{digest}:RUN:PRIMED",
61
+ "#{digest}:RUN:LOCKED",
62
+ key,
63
+ ])
64
+ end
65
+
66
+ log_info("#{__method__}(#{digest}) completed in #{elapsed}ms")
67
+
68
+ result
69
+ end
70
+
71
+ #
72
+ # The entries in this sorted set
73
+ #
74
+ # @param [String] pattern SCAN_PATTERN the match pattern to search for
75
+ # @param [Integer] count DEFAULT_COUNT the number of entries to return
76
+ #
77
+ # @return [Array<String>] an array of digests matching the given pattern
78
+ #
79
+ def entries(pattern: SCAN_PATTERN, count: DEFAULT_COUNT)
80
+ options = {}
81
+ options[:match] = pattern
82
+ options[:count] = count
83
+
84
+ redis { |conn| conn.zscan_each(key, **options).to_a }.to_h
85
+ end
86
+
87
+ #
88
+ # Returns a paginated
89
+ #
90
+ # @param [Integer] cursor the cursor for this iteration
91
+ # @param [String] pattern SCAN_PATTERN the match pattern to search for
92
+ # @param [Integer] page_size 100 the size per page
93
+ #
94
+ # @return [Array<Integer, Integer, Array<Lock>>] total_size, next_cursor, locks
95
+ #
96
+ def page(cursor: 0, pattern: SCAN_PATTERN, page_size: 100)
97
+ redis do |conn|
98
+ total_size, digests = conn.multi do |pipeline|
99
+ pipeline.zcard(key)
100
+ pipeline.zscan(key, cursor, match: pattern, count: page_size)
101
+ end
102
+
103
+ [
104
+ total_size.to_i,
105
+ digests[0].to_i, # next_cursor
106
+ digests[1].map { |digest, score| Lock.new(digest, time: score) }, # entries
107
+ ]
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ #
5
+ # Base class for all exceptions raised from the gem
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ #
9
+ class UniqueJobsError < ::RuntimeError
10
+ end
11
+
12
+ # Error raised when a Lua script fails to execute
13
+ #
14
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
15
+ class Conflict < UniqueJobsError
16
+ def initialize(item)
17
+ super("Item with the key: #{item[LOCK_DIGEST]} is already scheduled or processing")
18
+ end
19
+ end
20
+
21
+ #
22
+ # Raised when no block was given
23
+ #
24
+ class NoBlockGiven < SidekiqUniqueJobs::UniqueJobsError; end
25
+
26
+ #
27
+ # Raised when a notification has been mistyped
28
+ #
29
+ class NoSuchNotificationError < UniqueJobsError; end
30
+
31
+ #
32
+ # Error raised when trying to add a duplicate lock
33
+ #
34
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
35
+ #
36
+ class DuplicateLock < UniqueJobsError
37
+ end
38
+
39
+ #
40
+ # Error raised when trying to add a duplicate stragegy
41
+ #
42
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
43
+ #
44
+ class DuplicateStrategy < UniqueJobsError
45
+ end
46
+
47
+ #
48
+ # Error raised when an invalid argument is given
49
+ #
50
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
51
+ #
52
+ class InvalidArgument < UniqueJobsError
53
+ end
54
+
55
+ #
56
+ # Raised when a workers configuration is invalid
57
+ #
58
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
59
+ #
60
+ class InvalidWorker < UniqueJobsError
61
+ def initialize(lock_config)
62
+ super(<<~FAILURE_MESSAGE)
63
+ Expected #{lock_config.worker} to have valid sidekiq options but found the following problems:
64
+ #{lock_config.errors_as_string}
65
+ FAILURE_MESSAGE
66
+ end
67
+ end
68
+
69
+ # Error raised when a Lua script fails to execute
70
+ #
71
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
72
+ class InvalidUniqueArguments < UniqueJobsError
73
+ def initialize(options)
74
+ given = options[:given]
75
+ job_class = options[:job_class]
76
+ lock_args_method = options[:lock_args_method]
77
+ lock_args_meth = job_class.method(lock_args_method)
78
+ num_args = lock_args_meth.arity
79
+ source_location = lock_args_meth.source_location
80
+
81
+ super(
82
+ "#{job_class}##{lock_args_method} takes #{num_args} arguments, received #{given.inspect}" \
83
+ "\n\n" \
84
+ " #{source_location.join(':')}"
85
+ )
86
+ end
87
+ end
88
+
89
+ #
90
+ # Raised when a workers configuration is invalid
91
+ #
92
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
93
+ #
94
+ class NotUniqueWorker < UniqueJobsError
95
+ def initialize(options)
96
+ super("#{options[:class]} is not configured for uniqueness. Missing the key `:lock` in #{options.inspect}")
97
+ end
98
+ end
99
+
100
+ # Error raised from {OptionsWithFallback#lock_class}
101
+ #
102
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
103
+ class UnknownLock < UniqueJobsError
104
+ end
105
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ #
5
+ # Class ExpiringDigests provides access to the expiring digests used by until_expired locks
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ #
9
+ class ExpiringDigests < Digests
10
+ def initialize
11
+ super(EXPIRING_DIGESTS)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ # Utility class to append uniqueness to the sidekiq job hash
5
+ #
6
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
7
+ module Job
8
+ extend self
9
+
10
+ # Adds timeout, expiration, lock_args, lock_prefix and lock_digest to the sidekiq job hash
11
+ # @return [Hash] the job hash
12
+ def prepare(item)
13
+ stringify_on_conflict_hash(item)
14
+ add_lock_timeout(item)
15
+ add_lock_ttl(item)
16
+ add_digest(item)
17
+ end
18
+
19
+ # Adds lock_args, lock_prefix and lock_digest to the sidekiq job hash
20
+ # @return [Hash] the job hash
21
+ def add_digest(item)
22
+ add_lock_prefix(item)
23
+ add_lock_args(item)
24
+ add_lock_digest(item)
25
+
26
+ item
27
+ end
28
+
29
+ private
30
+
31
+ def stringify_on_conflict_hash(item)
32
+ on_conflict = item[ON_CONFLICT]
33
+ return unless on_conflict.is_a?(Hash)
34
+
35
+ item[ON_CONFLICT] = on_conflict.deep_stringify_keys
36
+ end
37
+
38
+ def add_lock_ttl(item)
39
+ item[LOCK_TTL] = SidekiqUniqueJobs::LockTTL.calculate(item)
40
+ end
41
+
42
+ def add_lock_timeout(item)
43
+ item[LOCK_TIMEOUT] ||= SidekiqUniqueJobs::LockTimeout.calculate(item)
44
+ end
45
+
46
+ def add_lock_args(item)
47
+ item[LOCK_ARGS] ||= SidekiqUniqueJobs::LockArgs.call(item)
48
+ end
49
+
50
+ def add_lock_digest(item)
51
+ item[LOCK_DIGEST] ||= SidekiqUniqueJobs::LockDigest.call(item)
52
+ end
53
+
54
+ def add_lock_prefix(item)
55
+ item[LOCK_PREFIX] ||= SidekiqUniqueJobs.config.lock_prefix
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ # Handles loading and dumping of json
5
+ #
6
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
7
+ module JSON
8
+ module_function
9
+
10
+ #
11
+ # Parses a JSON string into an object
12
+ #
13
+ # @param [String] string the object to parse
14
+ #
15
+ # @return [Object]
16
+ #
17
+ def load_json(string)
18
+ return if string.nil? || string.empty?
19
+
20
+ ::JSON.parse(string)
21
+ end
22
+
23
+ #
24
+ # Prevents trying JSON.load from raising errors given argument is a hash
25
+ #
26
+ # @param [String, Hash] string the JSON string to parse
27
+ #
28
+ # @return [Hash,Array]
29
+ #
30
+ def safe_load_json(string)
31
+ return string if string.is_a?(Hash)
32
+
33
+ load_json(string)
34
+ end
35
+
36
+ #
37
+ # Dumps an object into a JSON string
38
+ #
39
+ # @param [Object] object a JSON convertible object
40
+ #
41
+ # @return [String] a JSON string
42
+ #
43
+ def dump_json(object)
44
+ ::JSON.generate(object)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ # Key class wraps logic dealing with various lock keys
5
+ #
6
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
7
+ class Key
8
+ #
9
+ # @!attribute [r] digest
10
+ # @return [String] the digest key for which keys are created
11
+ attr_reader :digest
12
+ #
13
+ # @!attribute [r] queued
14
+ # @return [String] the list key with queued job_id's
15
+ attr_reader :queued
16
+ #
17
+ # @!attribute [r] primed
18
+ # @return [String] the list key with primed job_id's
19
+ attr_reader :primed
20
+ #
21
+ # @!attribute [r] locked
22
+ # @return [String] the hash key with locked job_id's
23
+ attr_reader :locked
24
+ #
25
+ # @!attribute [r] info
26
+ # @return [String] information about the lock
27
+ attr_reader :info
28
+ #
29
+ # @!attribute [r] changelog
30
+ # @return [String] the zset with changelog entries
31
+ attr_reader :changelog
32
+ #
33
+ # @!attribute [r] digests
34
+ # @return [String] the zset with locked digests
35
+ attr_reader :digests
36
+ #
37
+ # @!attribute [r] expiring_digests
38
+ # @return [String] the zset with locked expiring_digests
39
+ attr_reader :expiring_digests
40
+
41
+ #
42
+ # Initialize a new Key
43
+ #
44
+ # @param [String] digest the digest to use as key
45
+ #
46
+ def initialize(digest)
47
+ @digest = digest
48
+ @queued = suffixed_key("QUEUED")
49
+ @primed = suffixed_key("PRIMED")
50
+ @locked = suffixed_key("LOCKED")
51
+ @info = suffixed_key("INFO")
52
+ @changelog = CHANGELOGS
53
+ @digests = DIGESTS
54
+ @expiring_digests = EXPIRING_DIGESTS
55
+ end
56
+
57
+ #
58
+ # Provides the only important information about this keys
59
+ #
60
+ #
61
+ # @return [String]
62
+ #
63
+ def to_s
64
+ digest
65
+ end
66
+
67
+ # @see to_s
68
+ def inspect
69
+ digest
70
+ end
71
+
72
+ #
73
+ # Compares keys by digest
74
+ #
75
+ # @param [Key] other the key to compare with
76
+ #
77
+ # @return [true, false]
78
+ #
79
+ def ==(other)
80
+ digest == other.digest
81
+ end
82
+
83
+ #
84
+ # Returns all keys as an ordered array
85
+ #
86
+ # @return [Array] an ordered array with all keys
87
+ #
88
+ def to_a
89
+ [digest, queued, primed, locked, info, changelog, digests, expiring_digests]
90
+ end
91
+
92
+ private
93
+
94
+ def suffixed_key(variable)
95
+ "#{digest}:#{variable}"
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,165 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ class Lock
5
+ # Abstract base class for locks
6
+ #
7
+ # @abstract
8
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
9
+ class BaseLock
10
+ extend Forwardable
11
+
12
+ # includes "SidekiqUniqueJobs::Logging"
13
+ # @!parse include SidekiqUniqueJobs::Logging
14
+ include SidekiqUniqueJobs::Logging
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
+
35
+ # @param [Hash] item the Sidekiq job hash
36
+ # @param [Proc] callback the callback to use after unlock
37
+ # @param [Sidekiq::RedisConnection, ConnectionPool] redis_pool the redis connection
38
+ def initialize(item, callback, redis_pool = nil)
39
+ @item = item
40
+ @callback = callback
41
+ @redis_pool = redis_pool
42
+ @attempt = 0
43
+ prepare_item # Used to ease testing
44
+ @lock_config = LockConfig.new(item)
45
+ end
46
+
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
+ #
56
+ def lock
57
+ raise NotImplementedError, "##{__method__} needs to be implemented in #{self.class}"
58
+ end
59
+
60
+ # Execute the job in the Sidekiq server processor
61
+ # @raise [NotImplementedError] needs to be implemented in child class
62
+ def execute
63
+ raise NotImplementedError, "##{__method__} needs to be implemented in #{self.class}"
64
+ end
65
+
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)
74
+ end
75
+
76
+ private
77
+
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
+ #
95
+ # Eases testing by allowing the lock implementation to add the missing
96
+ # keys to the job hash.
97
+ #
98
+ #
99
+ # @return [void] the return value should be irrelevant
100
+ #
101
+ def prepare_item
102
+ return if item.key?(LOCK_DIGEST)
103
+
104
+ # The below should only be done to ease testing
105
+ # in production this will be done by the middleware
106
+ SidekiqUniqueJobs::Job.prepare(item)
107
+ end
108
+
109
+ #
110
+ # Call whatever strategry that has been configured
111
+ #
112
+ # @param [Symbol] origin: the origin `:client` or `:server`
113
+ #
114
+ # @return [void] the return value is irrelevant
115
+ #
116
+ # @yieldparam [void] if a new job id was set and a block is given
117
+ # @yieldreturn [void] the yield is irrelevant, it only provides a mechanism in
118
+ # one specific situation to yield back to the middleware.
119
+ def call_strategy(origin:)
120
+ new_job_id = nil
121
+ strategy = strategy_for(origin)
122
+ @attempt += 1
123
+
124
+ strategy.call { new_job_id = lock if strategy.replace? && @attempt < 2 }
125
+ yield if new_job_id && block_given?
126
+ end
127
+
128
+ def unlock_and_callback
129
+ return callback_safely if locksmith.unlock
130
+
131
+ reflect(:unlock_failed, item)
132
+ end
133
+
134
+ def callback_safely
135
+ callback&.call
136
+ item[JID]
137
+ rescue StandardError
138
+ reflect(:after_unlock_callback_failed, item)
139
+ raise
140
+ end
141
+
142
+ def strategy_for(origin)
143
+ case origin
144
+ when :client
145
+ client_strategy
146
+ when :server
147
+ server_strategy
148
+ else
149
+ raise SidekiqUniqueJobs::InvalidArgument,
150
+ "#origin needs to be either `:server` or `:client`"
151
+ end
152
+ end
153
+
154
+ def client_strategy
155
+ @client_strategy ||=
156
+ OnConflict.find_strategy(lock_config.on_client_conflict).new(item, redis_pool)
157
+ end
158
+
159
+ def server_strategy
160
+ @server_strategy ||=
161
+ OnConflict.find_strategy(lock_config.on_server_conflict).new(item, redis_pool)
162
+ end
163
+ end
164
+ end
165
+ 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