Skip to content

Commit

Permalink
Use sidekiq/testing Worker.clear API in sidekiq_unique_jobs/testing
Browse files Browse the repository at this point in the history
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]: sidekiq/sidekiq#5352
  • Loading branch information
dsander committed May 31, 2022
1 parent 5fa840c commit 119c643
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions lib/sidekiq_unique_jobs/testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,25 @@ 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

#
# Prepends deletion of locks to clear_all
# Prepends deletion of locks to clear_all and clear
#
module Overrides
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

#
# Overrides sidekiq_options on the worker class to prepend validation
#
Expand All @@ -113,5 +114,9 @@ def clear_all
end

prepend Overrides

module ClassMethods
prepend Overrides::ClassMethods
end
end
end

0 comments on commit 119c643

Please sign in to comment.