Skip to content

Commit

Permalink
Sort busy workers by run_at, fixes #4641
Browse files Browse the repository at this point in the history
We sort oldest first so long-running jobs will appear at the top; those jobs are typically the ones that developers want to see and diagnose.
  • Loading branch information
mperham committed Jul 13, 2020
1 parent 374703e commit 2139254
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/sidekiq/api.rb
Expand Up @@ -916,7 +916,8 @@ def signal(sig)
class Workers
include Enumerable

def each
def each(&block)
results = []
Sidekiq.redis do |conn|
procs = conn.sscan_each("processes").to_a
procs.sort.each do |key|
Expand All @@ -930,10 +931,12 @@ def each
p = hsh["payload"]
# avoid breaking API, this is a side effect of the JSON optimization in #4316
hsh["payload"] = Sidekiq.load_json(p) if p.is_a?(String)
yield key, tid, hsh
results << [key, tid, hsh]
end
end
end

results.sort_by { |(_, _, hsh)| hsh["run_at"] }.each(&block)
end

# Note that #size is only as accurate as Sidekiq's heartbeat,
Expand Down
2 changes: 1 addition & 1 deletion test/test_api.rb
Expand Up @@ -602,7 +602,7 @@ class WorkerWithTags
end
end

assert_equal ['1234', '5678'], w.map { |_, tid, _| tid }
assert_equal ['5678', '1234'], w.map { |_, tid, _| tid }
end

it 'can reschedule jobs' do
Expand Down

0 comments on commit 2139254

Please sign in to comment.