Skip to content

Instantly share code, notes, and snippets.

@BuonOmo
Created August 26, 2021 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BuonOmo/089bf3e978262b66094496992f428430 to your computer and use it in GitHub Desktop.
Save BuonOmo/089bf3e978262b66094496992f428430 to your computer and use it in GitHub Desktop.
# @param job_name [nil | #call | Regexp | String] name or proc to filter on
# @param tally_by [Symbol | #call] either a Sidekiq::SortedEntry method or a
# thing that responds to #call and takes the entry as argument
# @return the filtered jobs
# @example
# sidekiq_stat("ApplyReferrals.call", ->(j) { j.item["error_message"].gsub(/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/, "UUID") })
def sidekiq_stat(job_name = nil, tally_by = :display_class)
jobs = Sidekiq::DeadSet.new.select do |sorted_entry|
case job_name
when nil then true
when Regexp then sorted_entry.display_class.match?(job_name)
when String then sorted_entry.display_class == job_name
else
job_name.call(sorted_entry)
end
end
first, last = jobs.map { _1.item["enqueued_at"] }.minmax.map { Time.at _1.to_i }
puts(
jobs.map do |sorted_entry|
case tally_by
when Symbol
if sorted_entry.respond_to?(tally_by)
sorted_entry.public_send(tally_by)
else
sorted_entry.item[tally_by.to_s]
end
else
tally_by.call(sorted_entry)
end
end.tally.sort_by(&:last).map { [_2, _1] * "\t" }
)
puts
puts "jobs: #{jobs.size}"
puts "first occurrence: #{first}"
puts "last occurrence: #{last}"
return jobs
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment