Skip to content

Commit

Permalink
Merge pull request #6 from jdguzman/feature/support-for-named-args
Browse files Browse the repository at this point in the history
Feature: add support for named args
  • Loading branch information
JustinAiken authored Aug 2, 2016
2 parents 02878b9 + 2874f26 commit 3722c99
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
17 changes: 17 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ ThisIsTheClass:
Only classes that are descended from `ActiveJob::Base` will be wrapped

### Job Classes With Named Args

If you have a job class that uses named arguments you can specify that. `args`
should be a hash nested in the array and you need to add the named_args key.

```yaml
simple_job:
every: "30s"
queue: "simple"
class: "SimpleJob"
args:
- foo: 1
bar: 2
description: "It's a simple job."
named_args: true
```

## Credits

- Written by [@JustinAiken](https://www.github.com/JustinAiken)
Expand Down
14 changes: 12 additions & 2 deletions lib/active_scheduler/resque_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ class ResqueWrapper

def self.perform(job_data)
klass = Object.const_get job_data['job_class']
named_args = job_data.delete('named_args') || false

if job_data.has_key? 'arguments'
klass.perform_later *job_data['arguments']
if named_args
args = job_data['arguments'].first.symbolize_keys
else
args = job_data['arguments']
end

named_args ? klass.perform_later(**args) : klass.perform_later(*args)
else
klass.perform_later
end
Expand All @@ -21,6 +28,7 @@ def self.wrap(schedule)

queue = opts[:queue] || 'default'
args = opts[:args]
named_args = opts[:named_args] || false

if !args && opts.has_key?(:arguments)
warn 'active_scheduler: [DEPRECATION] using the `arguments` key in ' \
Expand All @@ -35,10 +43,12 @@ def self.wrap(schedule)
args: [{
job_class: class_name,
queue_name: queue,
arguments: args
arguments: args,
}]
}

schedule[job][:args].first.merge!({ named_args: named_args }) if named_args

schedule[job][:description] = opts.fetch(:description, nil) if opts.fetch(:description, nil)
schedule[job][:every] = opts.fetch(:every, nil) if opts.fetch(:every, nil)
schedule[job][:cron] = opts.fetch(:cron, nil) if opts.fetch(:cron, nil)
Expand Down
34 changes: 34 additions & 0 deletions spec/active_scheduler/resque_wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@
)
end
end

context "when the schedule is for a job with named arguments" do
let(:schedule) { YAML.load_file 'spec/fixtures/named_args_job.yaml' }

it "queues up a job, specifing that there are named args in the job" do
stub_jobs("NamedArgsJob")
expect(wrapped['named_args_job']).to eq(
"class" => "ActiveScheduler::ResqueWrapper",
"queue" => "simple",
"description" => "It's a named args job.",
"every" => "30s",
"args" => [{
"job_class" => "NamedArgsJob",
"queue_name" => "simple",
"arguments" => [{'foo' => 1, 'bar' => 2}],
"named_args" => true
}]
)
end
end
end

describe ".perform" do
Expand All @@ -124,5 +144,19 @@ class TestKlass
expect(TestKlass).to receive(:perform_later).with 1, 2
end
end

context "with named_arguments specified" do
let(:job_data) do
{
'job_class' => 'TestKlass',
'arguments' => [{ 'foo' => 1, 'bar' => 2 }],
'named_args' => true
}
end

it "passed the arguments as Named args" do
expect(TestKlass).to receive(:perform_later).with(foo: 1, bar: 2)
end
end
end
end
9 changes: 9 additions & 0 deletions spec/fixtures/named_args_job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
named_args_job:
every: "30s"
queue: "simple"
class: "NamedArgsJob"
args:
- foo: 1
bar: 2
description: "It's a named args job."
named_args: true

0 comments on commit 3722c99

Please sign in to comment.