Skip to content

Commit

Permalink
Merge 4370e87 into 5917609
Browse files Browse the repository at this point in the history
  • Loading branch information
n3bulous committed Jan 10, 2015
2 parents 5917609 + 4370e87 commit 6abfb2e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ You can also specify the number of times a job should be retried, if it fails :

$ sidekiq-client -r 5 push MyWorker OtherWorker

If your job requires arguments, you can specify them a la Rake (it uses Rake's task parser):

$ sidekiq-client push MyWorker[my-arg,my-other-arg]

For help :

$ sidekiq-client --help
Expand Down
28 changes: 26 additions & 2 deletions lib/sidekiq_client_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ def push
private

def push_argument(arg)
jid = Sidekiq::Client.push({ 'class' => arg,
klass, klass_args = parse_task_string(arg)
jid = Sidekiq::Client.push({ 'class' => klass,
'queue' => settings.queue,
'args' => [],
'args' => klass_args,
'retry' => settings.retry })
p "Posted #{arg} to queue '#{settings.queue}', Job ID : #{jid}, Retry : #{settings.retry}"
true
Expand All @@ -63,4 +64,27 @@ def push_argument(arg)
false
end


# pilfered from rake
# commas within args are not supported
def parse_task_string(string)
/^([^\[]+)(?:\[(.*)\])$/ =~ string.to_s

name = $1
remaining_args = $2

return string, [] unless name
return name, [] if remaining_args.empty?

args = []

begin
/((?:[^\\,]|\\.)*?)\s*(?:,\s*(.*))?$/ =~ remaining_args

remaining_args = $2
args << $1.gsub(/\\(.)/, '\1')
end while remaining_args

return name, args
end
end
46 changes: 46 additions & 0 deletions spec/sidekiq_client_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@
describe '#push_argument' do
let(:settings) { double("settings", :queue => default_queue, :retry => default_retry_option) }
let(:klass1) { "FirstWorker" }
let(:klass1_arg1) { "2015-01-01" }
let(:klass1_arg2) { "some string variable" }
let(:client) { SidekiqClientCLI.new }

before(:each) do
Expand Down Expand Up @@ -242,6 +244,21 @@
out.should include("Failed to push")
end

it "pushes a worker with one arg" do
Sidekiq::Client.should_receive(:push).with('class' => klass1,
'args' => [klass1_arg1],
'queue' => default_queue,
'retry' => default_retry_option)
client.__send__(:push_argument, "#{klass1}[#{klass1_arg1}]").should eq true
end

it "pushes a worker with two args" do
Sidekiq::Client.should_receive(:push).with('class' => klass1,
'args' => [klass1_arg1,klass1_arg2],
'queue' => default_queue,
'retry' => default_retry_option)
client.__send__(:push_argument, "#{klass1}[#{klass1_arg1},#{klass1_arg2}]").should eq true
end
end

describe 'cast_retry_option' do
Expand Down Expand Up @@ -269,4 +286,33 @@

end

describe 'parse_task_string' do
let(:client) { SidekiqClientCLI.new }
it 'parses a task without args' do
expected = "SomeTaskName"
name, args = client.send(:parse_task_string, expected)
name.should eq expected
args.should eq []
end

it 'parses a task with one arg' do
expected = "SomeTaskName"
expected_arg = "arg1"
name, args = client.send(:parse_task_string, "#{expected}[#{expected_arg}]")

name.should eq expected
args.should eq [expected_arg]
end

it 'parses a task with two args' do
expected = "SomeTaskName"
expected_args = ["arg1", "arg2"]
name, args = client.send(:parse_task_string,
"#{expected}[#{expected_args[0]},#{expected_args[1]}]")

name.should eq expected
args.should eq expected_args
end
end

end

0 comments on commit 6abfb2e

Please sign in to comment.