lib/sidekiq/api.rb in sidekiq-6.0.2 vs lib/sidekiq/api.rb in sidekiq-6.0.3
- old
+ new
@@ -138,17 +138,12 @@
queues.each do |queue|
conn.llen("queue:#{queue}")
end
}
- i = 0
- array_of_arrays = queues.each_with_object({}) { |queue, memo|
- memo[queue] = lengths[i]
- i += 1
- }.sort_by { |_, size| size }
-
- Hash[array_of_arrays.reverse]
+ array_of_arrays = queues.zip(lengths).sort_by { |_, size| -size }
+ Hash[array_of_arrays]
end
end
end
class History
@@ -166,22 +161,16 @@
end
private
def date_stat_hash(stat)
- i = 0
stat_hash = {}
- keys = []
- dates = []
+ dates = @start_date.downto(@start_date - @days_previous + 1).map { |date|
+ date.strftime("%Y-%m-%d")
+ }
- while i < @days_previous
- date = @start_date - i
- datestr = date.strftime("%Y-%m-%d")
- keys << "stat:#{stat}:#{datestr}"
- dates << datestr
- i += 1
- end
+ keys = dates.map { |datestr| "stat:#{stat}:#{datestr}" }
begin
Sidekiq.redis do |conn|
conn.mget(keys).each_with_index do |value, idx|
stat_hash[dates[idx]] = value ? value.to_i : 0
@@ -476,11 +465,11 @@
end
end
def reschedule(at)
Sidekiq.redis do |conn|
- conn.zincrby(@parent.name, at - @score, Sidekiq.dump_json(@item))
+ conn.zincrby(@parent.name, at.to_f - @score, Sidekiq.dump_json(@item))
end
end
def add_to_queue
remove_job do |message|
@@ -521,25 +510,25 @@
if results.size == 1
yield results.first
else
# multiple jobs with the same score
# find the one with the right JID and push it
- hash = results.group_by { |message|
+ matched, nonmatched = results.partition { |message|
if message.index(jid)
msg = Sidekiq.load_json(message)
msg["jid"] == jid
else
false
end
}
- msg = hash.fetch(true, []).first
+ msg = matched.first
yield msg if msg
# push the rest back onto the sorted set
conn.multi do
- hash.fetch(false, []).each do |message|
+ nonmatched.each do |message|
conn.zadd(parent.name, score.to_f.to_s, message)
end
end
end
end
@@ -783,13 +772,12 @@
}
# the hash named key has an expiry of 60 seconds.
# if it's not found, that means the process has not reported
# in to Redis and probably died.
- to_prune = []
- heartbeats.each_with_index do |beat, i|
- to_prune << procs[i] if beat.nil?
- end
+ to_prune = procs.select.with_index { |proc, i|
+ heartbeats[i].nil?
+ }
count = conn.srem("processes", to_prune) unless to_prune.empty?
end
count
end