1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Speed up find_job, closes #4259

This API has been in Sidekiq Pro for a number of years; I think it is fair to promote certain changes with such a huge positive impact to OSS.
This commit is contained in:
Mike Perham 2019-09-11 19:54:52 -07:00
parent f181279271
commit 2d8cc23969
3 changed files with 28 additions and 4 deletions

View file

@ -5,6 +5,12 @@
HEAD
---------
- Dramatically speed up SortedSet#find\_job(jid) by using Redis's ZSCAN
support, approx 10x faster. [#4259]
```
zscan 0.179366 0.047727 0.227093 ( 1.161376)
enum 8.522311 0.419826 8.942137 ( 9.785079)
```
- Respect rails' generators `test_framework` option and gracefully handle extra `worker` suffix on generator [#4256]
- Add ability to sort 'Enqueued' page on Web UI by position in the queue [#4248]
- Support `Client.push_bulk` with different delays [#4243]

View file

@ -593,11 +593,16 @@ module Sidekiq
##
# Find the job with the given JID within this sorted set.
#
# This is a slow, inefficient operation. Do not use under
# normal conditions. Sidekiq Pro contains a faster version.
# This is a slower O(n) operation. Do not use for app logic.
def find_job(jid)
detect { |j| j.jid == jid }
Sidekiq.redis do |conn|
conn.zscan_each(name, match: "*#{jid}*", count: 100) do |entry, score|
job = JSON.parse(entry)
matched = job["jid"] == jid
return SortedEntry.new(self, score, entry) if matched
end
end
nil
end
def delete_by_value(name, value)

View file

@ -341,6 +341,19 @@ describe 'API' do
assert_equal 1, ds.size
end
it 'can find a scheduled job by jid' do
10.times do |idx|
ApiWorker.perform_in(idx, 1)
end
job_id = ApiWorker.perform_in(5, 1)
job = Sidekiq::ScheduledSet.new.find_job(job_id)
assert_equal job_id, job.jid
ApiWorker.perform_in(100, 1, 'jid' => 'jid_in_args')
assert_nil Sidekiq::ScheduledSet.new.find_job('jid_in_args')
end
it 'can remove jobs when iterating over a sorted set' do
# scheduled jobs must be greater than SortedSet#each underlying page size
51.times do