From 68872b2b9bd8873355c58ef9b6f51f0bcf830418 Mon Sep 17 00:00:00 2001 From: Dominik Sander Date: Tue, 31 May 2022 13:08:21 +0200 Subject: [PATCH] Use sidekiq/testing `Worker.clear` API in sidekiq_unique_jobs/testing Instead of reimplementing the logic, we use the upstream `Worker.clear` API in the `sidekiq_unique_jobs/testing` `Worker.clear` overwrite. This fixes an issue that causes an infinite loop in `drain_all` after calling `clear` on a worker class that has it's queue name defined as a symbol. Because `sidekiq/testing` uses strings as the keys for the internal state tracking calling `Sidekiq::Queues[queue].clear` cleared a queue that never contained a job, while leaving jobs in the "string-indexed" queue. This only actually works after [this][1] is merged into `sidekiq` until then the old behaviour remains the same (clearing workers with their queue name defined as strings is fine). [1]: https://github.com/mperham/sidekiq/pull/5352 --- lib/sidekiq_unique_jobs/testing.rb | 35 ++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/sidekiq_unique_jobs/testing.rb b/lib/sidekiq_unique_jobs/testing.rb index d8654be3..64dc47e3 100644 --- a/lib/sidekiq_unique_jobs/testing.rb +++ b/lib/sidekiq_unique_jobs/testing.rb @@ -71,18 +71,6 @@ def use_options(tmp_config = {}) sidekiq_options(old_options) end - - # - # Clears the jobs for this worker and removes all locks - # - def clear - jobs.each do |job| - SidekiqUniqueJobs::Unlockable.unlock(job) - end - - Sidekiq::Queues[queue].clear - jobs.clear - end end # @@ -110,8 +98,31 @@ def clear_all SidekiqUniqueJobs::Digests.new.delete_by_pattern("*", count: 10_000) end + + # + # Prepends deletion of locks to clear + # + module ClassMethods + # + # Clears the jobs for this worker and removes all locks + # + def clear + jobs.each do |job| + SidekiqUniqueJobs::Unlockable.unlock(job) + end + + super + end + end end prepend Overrides + + # + # Prepends methods to Sidekiq::Worker + # + module ClassMethods + prepend Overrides::ClassMethods + end end end