File tree Expand file tree Collapse file tree 4 files changed +72
-2
lines changed Expand file tree Collapse file tree 4 files changed +72
-2
lines changed Original file line number Diff line number Diff line change @@ -57,6 +57,23 @@ ThisIsTheClass:
57
57
58
58
Only classes that are descended from ` ActiveJob::Base` will be wrapped
59
59
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
+
60
77
# # Credits
61
78
62
79
- Written by [@JustinAiken](https://www.github.com/JustinAiken)
Original file line number Diff line number Diff line change @@ -3,9 +3,16 @@ class ResqueWrapper
3
3
4
4
def self . perform ( job_data )
5
5
klass = Object . const_get job_data [ 'job_class' ]
6
+ named_args = job_data . delete ( 'named_args' ) || false
6
7
7
8
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 )
9
16
else
10
17
klass . perform_later
11
18
end
@@ -21,6 +28,7 @@ def self.wrap(schedule)
21
28
22
29
queue = opts [ :queue ] || 'default'
23
30
args = opts [ :args ]
31
+ named_args = opts [ :named_args ] || false
24
32
25
33
if !args && opts . has_key? ( :arguments )
26
34
warn 'active_scheduler: [DEPRECATION] using the `arguments` key in ' \
@@ -35,10 +43,12 @@ def self.wrap(schedule)
35
43
args : [ {
36
44
job_class : class_name ,
37
45
queue_name : queue ,
38
- arguments : args
46
+ arguments : args ,
39
47
} ]
40
48
}
41
49
50
+ schedule [ job ] [ :args ] . first . merge! ( { named_args : named_args } ) if named_args
51
+
42
52
schedule [ job ] [ :description ] = opts . fetch ( :description , nil ) if opts . fetch ( :description , nil )
43
53
schedule [ job ] [ :every ] = opts . fetch ( :every , nil ) if opts . fetch ( :every , nil )
44
54
schedule [ job ] [ :cron ] = opts . fetch ( :cron , nil ) if opts . fetch ( :cron , nil )
Original file line number Diff line number Diff line change 103
103
)
104
104
end
105
105
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
106
126
end
107
127
108
128
describe ".perform" do
@@ -124,5 +144,19 @@ class TestKlass
124
144
expect ( TestKlass ) . to receive ( :perform_later ) . with 1 , 2
125
145
end
126
146
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
127
161
end
128
162
end
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments