Skip to content

Commit

Permalink
Merge pull request #3 from ximus/only-wrap-active-jobs
Browse files Browse the repository at this point in the history
Only wrap jobs that inherit ActiveJob::Base
  • Loading branch information
JustinAiken committed Mar 29, 2016
2 parents 0d6993a + e56ae90 commit 1159bf0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/active_scheduler/resque_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ def self.wrap(schedule)
schedule = HashWithIndifferentAccess.new(schedule)

schedule.each do |job, opts|
next if opts[:class] =~ /ActiveScheduler::ResqueWrapper/
class_name = opts[:class] || job
next if class_name =~ /ActiveScheduler::ResqueWrapper/
next unless class_name.constantize <= ActiveJob::Base

queue = opts[:queue] || 'default'

schedule[job] = {
class: 'ActiveScheduler::ResqueWrapper',
queue: queue,
args: [{
job_class: opts[:class] || job,
job_class: class_name,
queue_name: queue,
arguments: opts[:arguments]
}]
Expand Down
20 changes: 19 additions & 1 deletion spec/active_scheduler/resque_wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
let(:schedule) { YAML.load_file 'spec/fixtures/simple_job.yaml' }

it "queues up a simple job" do
stub_jobs("SimpleJob")
expect(wrapped['simple_job']).to eq(
"class" => "ActiveScheduler::ResqueWrapper",
"queue" => "simple",
Expand All @@ -21,12 +22,26 @@
}]
)
end

context "job is not an active job descendant" do
it "doesn't wrap" do
stub_const("SimpleJob", Class.new)
expect(wrapped['simple_job']).to eq(
"class" => "SimpleJob",
"queue" => "simple",
"description" => "It's a simple job.",
"every" => "30s",
"args" => [nil],
)
end
end
end

context "with a simple job json" do
let(:schedule) { YAML.load_file 'spec/fixtures/simple_job.json' }

it "queues up a simple job" do
stub_jobs("SimpleJob")
expect(wrapped['simple_job']).to eq(
"class" => "ActiveScheduler::ResqueWrapper",
"queue" => "simple",
Expand All @@ -45,6 +60,7 @@
let(:schedule) { YAML.load_file 'spec/fixtures/two_jobs.yaml' }

it "queues them all up simple job" do
stub_jobs("JobOne", "JobTwo")
expect(wrapped['job_1']['args'][0]['job_class']).to eq 'JobOne'
expect(wrapped['job_2']['args'][0]['job_class']).to eq 'JobTwo'
end
Expand All @@ -54,6 +70,7 @@
let(:schedule) { YAML.load_file 'spec/fixtures/cron_job.yaml' }

it "uses that instead" do
stub_jobs("CronJob")
expect(wrapped['cron_job']['cron']).to eq '* * * *'
expect(wrapped['cron_job']['every']).to be_nil
end
Expand All @@ -63,6 +80,7 @@
let(:schedule) { YAML.load_file 'spec/fixtures/no_queue.yaml' }

it "uses 'default'" do
stub_jobs("SimpleJob")
expect(wrapped['no_queue_job']['queue']).to eq 'default'
end
end
Expand All @@ -71,6 +89,7 @@
let(:schedule) { YAML.load_file 'spec/fixtures/schedule_name_is_class_name.yaml' }

it "queues up a job, using the schedule name for the class name" do
stub_jobs("MyScheduleNameIsClassNameJob")
expect(wrapped['MyScheduleNameIsClassNameJob']).to eq(
"class" => "ActiveScheduler::ResqueWrapper",
"queue" => "myscheduledjobqueue",
Expand All @@ -83,7 +102,6 @@
}]
)
end

end
end

Expand Down
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@
Coveralls.wear!
end

module Helpers
def stub_jobs(*names)
names.each do |name|
stub_const(name, Class.new(ActiveJob::Base))
end
end
end

RSpec.configure do |config|
config.include Helpers
config.filter_run :focus
config.run_all_when_everything_filtered = true
end

0 comments on commit 1159bf0

Please sign in to comment.