Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ActiveScheduler.config.{guard_with,queue_name_resolver} #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/active_scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,21 @@
require 'active_scheduler/resque_wrapper'

module ActiveScheduler
Config = Struct.new(:guard_with, :queue_name_resolver)

class << self
def config
@config ||= Config.new
end

def configure(&block)
yield(config)
end
end

# Default config
configure do |config|
config.guard_with = ->(class_name) { class_name.constantize <= ActiveJob::Base }
config.queue_name_resolver = ->(class_name) { class_name.constantize.queue_name }
end
end
6 changes: 2 additions & 4 deletions lib/active_scheduler/resque_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ def self.wrap(schedule)
schedule.each do |job, opts|
class_name = opts[:class] || job
next if class_name =~ /#{self.to_s}/
next unless ActiveScheduler.config.guard_with.(class_name)

klass = class_name.constantize
next unless klass <= ActiveJob::Base

queue = opts[:queue] || klass.queue_name
queue = opts[:queue] || ActiveScheduler.config.queue_name_resolver.(class_name)
args = opts[:args]
named_args = opts[:named_args] || false

Expand Down
88 changes: 73 additions & 15 deletions spec/active_scheduler/resque_wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,48 @@
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",
"rails_env" => "test",
"args" => ['foo-arg-1', 'foo-arg-2'],
)
context "with default configuration" do
it "doesn't wrap" do
stub_jobs("SimpleJob", base_class: Class.new)
expect(wrapped['simple_job']).to eq(
"class" => "SimpleJob",
"queue" => "simple",
"description" => "It's a simple job.",
"every" => "30s",
"rails_env" => "test",
"args" => ['foo-arg-1', 'foo-arg-2'],
)
end
end

context "with a custom guard_with configuration" do
let!(:guard_with) do
ActiveScheduler.config.guard_with
end

before do
ActiveScheduler.config.guard_with = ->(*) { true }
end

after do
ActiveScheduler.config.guard_with = guard_with
end

it "wraps the job" do
stub_jobs("SimpleJob", base_class: Class.new)
expect(wrapped['simple_job']).to eq(
"class" => "ActiveScheduler::ResqueWrapper",
"queue" => "simple",
"description" => "It's a simple job.",
"every" => "30s",
"rails_env" => "test",
"args" => [{
"job_class" => "SimpleJob",
"queue_name" => "simple",
"arguments" => ['foo-arg-1', 'foo-arg-2'],
}]
)
end
end
end

Expand Down Expand Up @@ -125,14 +157,40 @@ class CustomWrapper < ActiveScheduler::ResqueWrapper
context "when the queue is blank" do
let(:schedule) { YAML.load_file 'spec/fixtures/no_queue.yaml' }

it "uses the job's queue" do
simple_job = Class.new(ActiveJob::Base) do
queue_as :myscheduledjobqueue
context "with default configuration" do
it "uses the job's queue" do
simple_job = Class.new(ActiveJob::Base) do
queue_as :myscheduledjobqueue
end

stub_jobs("SimpleJob", base_class: simple_job)

expect(wrapped['no_queue_job']['queue']).to eq 'myscheduledjobqueue'
end
end

stub_const("SimpleJob", simple_job)
context "with a custom queue_name_resolver configuration" do
let!(:queue_name_resolver) do
ActiveScheduler.config.queue_name_resolver
end

expect(wrapped['no_queue_job']['queue']).to eq 'myscheduledjobqueue'
before do
ActiveScheduler.config.queue_name_resolver = ->(*) { 'test' }
end

after do
ActiveScheduler.config.queue_name_resolver = queue_name_resolver
end

it "uses the queue name resolver" do
simple_job = Class.new(ActiveJob::Base) do
queue_as :myscheduledjobqueue
end

stub_jobs("SimpleJob", base_class: simple_job)

expect(wrapped['no_queue_job']['queue']).to eq 'test'
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
end

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