Skip to content

Commit

Permalink
Add ability to find a currently running work by jid
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Mar 4, 2024
1 parent 7b650ad commit 9355d9f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

[Sidekiq Changes](https://github.com/sidekiq/sidekiq/blob/main/Changes.md) | [Sidekiq Pro Changes](https://github.com/sidekiq/sidekiq/blob/main/Pro-Changes.md) | [Sidekiq Enterprise Changes](https://github.com/sidekiq/sidekiq/blob/main/Ent-Changes.md)

HEAD
----------

- Add ability to find a currently running work by jid [#6212, fatkodima]

7.2.2
----------

Expand Down
14 changes: 14 additions & 0 deletions lib/sidekiq/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,20 @@ def size
end
end
end

##
# Find the work which represents a job with the given JID.
# *This is a slow O(n) operation*. Do not use for app logic.
#
# @param jid [String] the job identifier
# @return [Sidekiq::Work] the work or nil
def find_work_by_jid(jid)
each do |_process_id, _thread_id, work|
job = work.job
return work if job.jid == jid
end
nil
end
end

# Sidekiq::Work represents a job which is currently executing.
Expand Down
21 changes: 21 additions & 0 deletions test/api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,27 @@ class JobWithTags
assert_equal ["5678", "1234"], w.map { |_, tid, _| tid }
end

it "can find a work by jid" do
w = Sidekiq::Workers.new
hn = Socket.gethostname
key = "#{hn}:#{$$}"
@cfg.redis do |conn|
conn.sadd("processes", [key])
end

s = "#{key}:work"
jid = "abcdef"
data = Sidekiq.dump_json({"payload" => {"args" => ["foo"], "jid" => jid}, "queue" => "default", "run_at" => Time.now.to_i})
@cfg.redis do |c|
c.hset(s, "1234", data)
end

assert_nil w.find_work_by_jid("nonexistent")

work = w.find_work_by_jid(jid)
assert_equal ["foo"], work.job.args
end

it "can reschedule jobs" do
add_retry("foo1")
add_retry("foo2")
Expand Down

0 comments on commit 9355d9f

Please sign in to comment.