diff --git a/lib/active_scheduler/resque_wrapper.rb b/lib/active_scheduler/resque_wrapper.rb index 7e39e22..60ec63f 100644 --- a/lib/active_scheduler/resque_wrapper.rb +++ b/lib/active_scheduler/resque_wrapper.rb @@ -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 @@ -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 ' \ @@ -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) diff --git a/spec/active_scheduler/resque_wrapper_spec.rb b/spec/active_scheduler/resque_wrapper_spec.rb index b416319..754eab8 100644 --- a/spec/active_scheduler/resque_wrapper_spec.rb +++ b/spec/active_scheduler/resque_wrapper_spec.rb @@ -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 @@ -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 diff --git a/spec/fixtures/named_args_job.yaml b/spec/fixtures/named_args_job.yaml new file mode 100644 index 0000000..b4d4546 --- /dev/null +++ b/spec/fixtures/named_args_job.yaml @@ -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