Skip to content

Commit 3722c99

Browse files
authored
Merge pull request #6 from jdguzman/feature/support-for-named-args
Feature: add support for named args
2 parents 02878b9 + 2874f26 commit 3722c99

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

README.markdown

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ ThisIsTheClass:
5757
5858
Only classes that are descended from `ActiveJob::Base` will be wrapped
5959

60+
### Job Classes With Named Args
61+
62+
If you have a job class that uses named arguments you can specify that. `args`
63+
should be a hash nested in the array and you need to add the named_args key.
64+
65+
```yaml
66+
simple_job:
67+
every: "30s"
68+
queue: "simple"
69+
class: "SimpleJob"
70+
args:
71+
- foo: 1
72+
bar: 2
73+
description: "It's a simple job."
74+
named_args: true
75+
```
76+
6077
## Credits
6178

6279
- Written by [@JustinAiken](https://www.github.com/JustinAiken)

lib/active_scheduler/resque_wrapper.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ class ResqueWrapper
33

44
def self.perform(job_data)
55
klass = Object.const_get job_data['job_class']
6+
named_args = job_data.delete('named_args') || false
67

78
if job_data.has_key? 'arguments'
8-
klass.perform_later *job_data['arguments']
9+
if named_args
10+
args = job_data['arguments'].first.symbolize_keys
11+
else
12+
args = job_data['arguments']
13+
end
14+
15+
named_args ? klass.perform_later(**args) : klass.perform_later(*args)
916
else
1017
klass.perform_later
1118
end
@@ -21,6 +28,7 @@ def self.wrap(schedule)
2128

2229
queue = opts[:queue] || 'default'
2330
args = opts[:args]
31+
named_args = opts[:named_args] || false
2432

2533
if !args && opts.has_key?(:arguments)
2634
warn 'active_scheduler: [DEPRECATION] using the `arguments` key in ' \
@@ -35,10 +43,12 @@ def self.wrap(schedule)
3543
args: [{
3644
job_class: class_name,
3745
queue_name: queue,
38-
arguments: args
46+
arguments: args,
3947
}]
4048
}
4149

50+
schedule[job][:args].first.merge!({ named_args: named_args }) if named_args
51+
4252
schedule[job][:description] = opts.fetch(:description, nil) if opts.fetch(:description, nil)
4353
schedule[job][:every] = opts.fetch(:every, nil) if opts.fetch(:every, nil)
4454
schedule[job][:cron] = opts.fetch(:cron, nil) if opts.fetch(:cron, nil)

spec/active_scheduler/resque_wrapper_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@
103103
)
104104
end
105105
end
106+
107+
context "when the schedule is for a job with named arguments" do
108+
let(:schedule) { YAML.load_file 'spec/fixtures/named_args_job.yaml' }
109+
110+
it "queues up a job, specifing that there are named args in the job" do
111+
stub_jobs("NamedArgsJob")
112+
expect(wrapped['named_args_job']).to eq(
113+
"class" => "ActiveScheduler::ResqueWrapper",
114+
"queue" => "simple",
115+
"description" => "It's a named args job.",
116+
"every" => "30s",
117+
"args" => [{
118+
"job_class" => "NamedArgsJob",
119+
"queue_name" => "simple",
120+
"arguments" => [{'foo' => 1, 'bar' => 2}],
121+
"named_args" => true
122+
}]
123+
)
124+
end
125+
end
106126
end
107127

108128
describe ".perform" do
@@ -124,5 +144,19 @@ class TestKlass
124144
expect(TestKlass).to receive(:perform_later).with 1, 2
125145
end
126146
end
147+
148+
context "with named_arguments specified" do
149+
let(:job_data) do
150+
{
151+
'job_class' => 'TestKlass',
152+
'arguments' => [{ 'foo' => 1, 'bar' => 2 }],
153+
'named_args' => true
154+
}
155+
end
156+
157+
it "passed the arguments as Named args" do
158+
expect(TestKlass).to receive(:perform_later).with(foo: 1, bar: 2)
159+
end
160+
end
127161
end
128162
end

spec/fixtures/named_args_job.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
named_args_job:
2+
every: "30s"
3+
queue: "simple"
4+
class: "NamedArgsJob"
5+
args:
6+
- foo: 1
7+
bar: 2
8+
description: "It's a named args job."
9+
named_args: true

0 commit comments

Comments
 (0)