Skip to content

Commit

Permalink
Merge pull request #31 from cryo28/utilize-sidekiq-jid
Browse files Browse the repository at this point in the history
Use Sidekiq::Worker#jid instead of replacing job arguments with jid  generated by SidekiqStatus
  • Loading branch information
cryo28 committed Apr 12, 2016
2 parents 5743232 + 3d0c029 commit b5a3022
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ and clean status containers.
### 1.1.0

* Support for sidekiq 4.1, 4.0, 3.5, 3.4
* No more replacement of original job arguments with generated unique jid.
This is not needed anymore as Sidekiq started to do it.
This change should make integration with other middlewares easier.
* Dropped support for sidekiq versions older than 3.3
* Dropped support for ruby 1.9.x, 2.0.x
* Experimental support for Rubinius
Expand Down
1 change: 0 additions & 1 deletion lib/sidekiq_status/client_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def call(worker, item, queue, redis_pool = nil)

jid = item['jid']
args = item['args']
item['args'] = [jid]

SidekiqStatus::Container.create(
'jid' => jid,
Expand Down
2 changes: 1 addition & 1 deletion lib/sidekiq_status/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def self.load_data(jid)
# @param [Array<#to_s>] jids a list of job identifiers to load data for
# @return [Hash{String => Hash}] A hash of job-id to deserialized data pairs
def self.load_data_multi(jids)
keys = jids.map { |jid| status_key(jid) }
keys = jids.map(&method(:status_key))

return {} if keys.empty?

Expand Down
4 changes: 2 additions & 2 deletions lib/sidekiq_status/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def self.included(base)
end

module Prepending
def perform(jid)
def perform(*args)
@status_container = SidekiqStatus::Container.load(jid)

begin
catch(:killed) do
set_status('working')
super(*@status_container.args)
super(*args)
set_status('complete')
end
rescue Exception => exc
Expand Down
22 changes: 16 additions & 6 deletions spec/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ def some_method(*args); end
describe "#perform (Worker context)" do
let(:worker) { SomeWorker.new }

it "receives jid as parameters, loads container and runs original perform with enqueued args" do
it "loads container using @jid and runs original perform" do
expect(worker).to receive(:some_method).with(*args)
jid = SomeWorker.perform_async(*args)
worker.perform(jid)
worker.jid = jid.freeze
worker.perform(*args)
end

it "changes status to working" do
Expand All @@ -42,7 +43,8 @@ def some_method(*args); end
end)

jid = SomeWorker.perform_async(*args)
worker.perform(jid)
worker.jid = jid.freeze
worker.perform(*args)

has_been_run.should be true
worker.status_container.reload.status.should == 'complete'
Expand All @@ -53,33 +55,39 @@ def some_method(*args); end
allow(worker).to receive(:some_method).and_raise(exc)

jid = SomeWorker.perform_async(*args)
expect{ worker.perform(jid) }.to raise_exception{ |error| error.object_id.should == exc.object_id }
worker.jid = jid.freeze
expect{ worker.perform(*args) }.to raise_exception{ |error| error.object_id.should == exc.object_id }

container = SidekiqStatus::Container.load(jid)
container.status.should == 'failed'
end

it "sets status to 'complete' if finishes without errors" do
jid = SomeWorker.perform_async(*args)
worker.perform(jid)
worker.jid = jid.freeze
worker.perform(*args)

container = SidekiqStatus::Container.load(jid)
container.status.should == 'complete'
end

it "handles kill requests if kill requested before job execution" do
jid = SomeWorker.perform_async(*args)
worker.jid = jid.freeze

container = SidekiqStatus::Container.load(jid)
container.request_kill

worker.perform(jid)
worker.perform(*args)

container.reload
container.status.should == 'killed'
end

it "handles kill requests if kill requested amid job execution" do
jid = SomeWorker.perform_async(*args)
worker.jid = jid.freeze

container = SidekiqStatus::Container.load(jid)
container.status.should == 'waiting'

Expand Down Expand Up @@ -117,6 +125,8 @@ def some_method(*args); end

it "allows to set at, total and customer payload from the worker" do
jid = SomeWorker.perform_async(*args)
worker.jid = jid.freeze

container = SidekiqStatus::Container.load(jid)

lets_stop = false
Expand Down

0 comments on commit b5a3022

Please sign in to comment.