Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
26 changes: 20 additions & 6 deletions lib/easy_stalk/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def self.tube_prefix(prefix=nil)
if prefix
@tube_prefix = prefix
else
@tube_prefix || EasyStalk.configuration.default_tube_prefix
fetch_attribute(:tube_prefix)
end
end

Expand All @@ -30,7 +30,7 @@ def self.priority(pri=nil)
if pri
@priority = pri
else
@priority || EasyStalk.configuration.default_priority
fetch_attribute(:priority)
end
end

Expand All @@ -39,7 +39,7 @@ def self.time_to_run(seconds=nil)
if seconds
@time_to_run = seconds
else
@time_to_run || EasyStalk.configuration.default_time_to_run
fetch_attribute(:time_to_run)
end
end

Expand All @@ -48,7 +48,7 @@ def self.delay(seconds=nil)
if seconds
@delay = seconds
else
@delay || EasyStalk.configuration.default_delay
fetch_attribute(:delay)
end
end

Expand All @@ -57,15 +57,15 @@ def self.retry_times(attempts=nil)
if attempts
@retry_times = attempts
else
@retry_times || EasyStalk.configuration.default_retry_times
fetch_attribute(:retry_times)
end
end

def self.serializable_context_keys(*keys)
if keys.size > 0
@serializable_context_keys = keys
else
@serializable_context_keys || DEFAULT_SERIALIZABLE_CONTEXT_KEYS
fetch_attribute(:serializable_context_keys, DEFAULT_SERIALIZABLE_CONTEXT_KEYS)
end
end

Expand All @@ -91,6 +91,20 @@ def job_data
def call
raise NotImplementedError
end

private

def self.fetch_attribute(attribute, default=nil)
if self.instance_variable_defined?("@#{attribute}".to_sym)
self.instance_variable_get("@#{attribute}".to_sym)
elsif superclass.respond_to?(attribute.to_sym)
superclass.send(attribute.to_sym)
elsif EasyStalk.configuration.respond_to?("default_#{attribute}".to_sym)
EasyStalk.configuration.send("default_#{attribute}".to_sym)
else
default
end
end
end
end

117 changes: 112 additions & 5 deletions spec/lib/easy_stalk/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@

describe EasyStalk::Job do

class EasyStalk::MockJob < EasyStalk::Job
before do
class EasyStalk::MockJob < EasyStalk::Job
end
end

after do
EasyStalk.send(:remove_const, :MockJob)
end
describe EasyStalk::MockJob do

subject { EasyStalk::MockJob.new }

context "with a MockJob" do
after do
EasyStalk.configure
end

describe 'self << class' do
subject { described_class }
subject { EasyStalk::MockJob }

describe '.tube_name' do
it 'defaults to class name' do
Expand All @@ -24,13 +33,36 @@ class MockJobWithName < subject
end
EasyStalk.configure { |config| config.default_tube_prefix = "rating.test." }
expect(MockJobWithName.new.class.tube_name).to eq "rating.test.bar"
Object.send(:remove_const, :MockJobWithName)
end
it 'properly uses a prefix' do
class MockJobWithNameAndPrefix < subject
tube_name "bar"
tube_prefix "foo."
end
expect(MockJobWithNameAndPrefix.new.class.tube_name).to eq "foo.bar"
Object.send(:remove_const, :MockJobWithNameAndPrefix)
end
it 'properly uses an inherited prefix' do
class MockJobWithName < subject
tube_prefix "foo."
end
class MockJobWithNameAndPrefix < MockJobWithName
tube_name "bar"
end
expect(MockJobWithNameAndPrefix.new.class.tube_name).to eq "foo.bar"
Object.send(:remove_const, :MockJobWithName)
Object.send(:remove_const, :MockJobWithNameAndPrefix)
end
it 'does not inherit tube_name' do
class MockJobWithName < subject
tube_name "bar"
end
class MockChildJobWithoutName < MockJobWithName
end
expect(MockChildJobWithoutName.new.class.tube_name).to eq "MockChildJobWithoutName"
Object.send(:remove_const, :MockJobWithName)
Object.send(:remove_const, :MockChildJobWithoutName)
end
end

Expand All @@ -40,6 +72,17 @@ class MockJobWithPrefix < subject
tube_prefix "bar."
end
expect(MockJobWithPrefix.new().class.tube_prefix).to eq "bar."
Object.send(:remove_const, :MockJobWithPrefix)
end
it 'uses inheritance if present' do
class MockJobWithPrefix < subject
tube_prefix "bar."
end
class MockChildJobWithPrefix < MockJobWithPrefix
end
expect(MockChildJobWithPrefix.new().class.tube_prefix).to eq "bar."
Object.send(:remove_const, :MockJobWithPrefix)
Object.send(:remove_const, :MockChildJobWithPrefix)
end
it 'uses the env if present' do
EasyStalk.configure { |config| config.default_tube_prefix = "foo." }
Expand All @@ -56,6 +99,17 @@ class MockJobWithPri < subject
priority 25
end
expect(MockJobWithPri.new().class.priority).to eq 25
Object.send(:remove_const, :MockJobWithPri)
end
it 'uses inheritance if present' do
class MockJobWithPri < subject
priority 25
end
class MockChildJobWithPri < MockJobWithPri
end
expect(MockChildJobWithPri.new().class.priority).to eq 25
Object.send(:remove_const, :MockJobWithPri)
Object.send(:remove_const, :MockChildJobWithPri)
end
it 'uses default if not set' do
expect(subject.priority).to eq EasyStalk::Configuration::DEFAULT_PRI
Expand All @@ -68,6 +122,17 @@ class MockJobWithTtr < subject
time_to_run 90
end
expect(MockJobWithTtr.new().class.time_to_run).to eq 90
Object.send(:remove_const, :MockJobWithTtr)
end
it 'uses inheritance if present' do
class MockJobWithTtr < subject
time_to_run 90
end
class MockChildJobWithTtr < MockJobWithTtr
end
expect(MockChildJobWithTtr.new().class.time_to_run).to eq 90
Object.send(:remove_const, :MockJobWithTtr)
Object.send(:remove_const, :MockChildJobWithTtr)
end
it 'uses default if not set' do
expect(subject.time_to_run).to eq EasyStalk::Configuration::DEFAULT_TTR
Expand All @@ -80,6 +145,17 @@ class MockJobWithDelay < subject
delay 5
end
expect(MockJobWithDelay.new().class.delay).to eq 5
Object.send(:remove_const, :MockJobWithDelay)
end
it 'uses inheritance if present' do
class MockJobWithDelay < subject
delay 5
end
class MockChildJobWithDelay < MockJobWithDelay
end
expect(MockChildJobWithDelay.new().class.delay).to eq 5
Object.send(:remove_const, :MockJobWithDelay)
Object.send(:remove_const, :MockChildJobWithDelay)
end
it 'uses default if not set' do
expect(subject.delay).to eq EasyStalk::Configuration::DEFAULT_DELAY
Expand All @@ -92,6 +168,17 @@ class MockJobWithRetryTimes < subject
retry_times 5
end
expect(MockJobWithRetryTimes.new().class.retry_times).to eq 5
Object.send(:remove_const, :MockJobWithRetryTimes)
end
it 'uses inheritance if present' do
class MockJobWithRetryTimes < subject
retry_times 5
end
class MockChildJobWithRetryTimes < MockJobWithRetryTimes
end
expect(MockChildJobWithRetryTimes.new().class.retry_times).to eq 5
Object.send(:remove_const, :MockJobWithRetryTimes)
Object.send(:remove_const, :MockChildJobWithRetryTimes)
end
it 'uses default if not set' do
expect(subject.retry_times).to eq EasyStalk::Configuration::DEFAULT_RETRY_TIMES
Expand All @@ -104,6 +191,17 @@ class MockJobWithKeys < subject
serializable_context_keys :cat, :dog
end
expect(MockJobWithKeys.new().class.serializable_context_keys).to eq [:cat, :dog]
Object.send(:remove_const, :MockJobWithKeys)
end
it 'uses inheritance if present' do
class MockJobWithKeys < subject
serializable_context_keys :cat, :dog
end
class MockChildJobWithKeys < MockJobWithKeys
end
expect(MockChildJobWithKeys.new().class.serializable_context_keys).to eq [:cat, :dog]
Object.send(:remove_const, :MockJobWithKeys)
Object.send(:remove_const, :MockChildJobWithKeys)
end
it 'uses default if not set' do
expect(subject.serializable_context_keys).to eq described_class::DEFAULT_SERIALIZABLE_CONTEXT_KEYS
Expand Down Expand Up @@ -169,6 +267,7 @@ class MockJobWithKeys < described_class
}
MockJobWithKeys.new(:cat => "mew", "dog" => "wuf", :fish => "blu").
enqueue(conn, priority: pri, time_to_run: ttr, delay: delay)
Object.send(:remove_const, :MockJobWithKeys)
end
end

Expand All @@ -195,13 +294,21 @@ class MockJobWithKeys < described_class
end
context = { :cat => "mew", "dog" => "wuf", :fish => "blu" }
expect(MockJobWithKeys.new(context).job_data).to eq "{\"cat\":\"mew\",\"dog\":\"wuf\"}"
Object.send(:remove_const, :MockJobWithKeys)
end
end

describe '.call' do
class ImplementedJob < described_class
def call; end
before do
class ImplementedJob < described_class
def call; end
end
end

after do
Object.send(:remove_const, :ImplementedJob)
end

it { expect { described_class.call }.to raise_error(NotImplementedError) }
it { expect { ImplementedJob.call }.to_not raise_error }
end
Expand Down