ayl-beanstalk 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +10 -12
- data/Gemfile.lock +118 -97
- data/LICENSE.txt +1 -1
- data/README.rdoc +26 -3
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/ayl-beanstalk.gemspec +31 -30
- data/bin/ayl_beanstalk +103 -50
- data/lib/ayl-beanstalk.rb +2 -1
- data/lib/ayl-beanstalk/engine.rb +6 -3
- data/lib/ayl-beanstalk/job.rb +17 -13
- data/lib/ayl-beanstalk/pool.rb +1 -1
- data/lib/ayl-beanstalk/worker.rb +2 -2
- data/spec/ayl_worker_control_spec.rb +2 -2
- data/spec/ayl_worker_spec.rb +33 -33
- data/spec/command_line_spec.rb +5 -5
- data/spec/engine_spec.rb +16 -14
- data/spec/job_spec.rb +59 -65
- data/spec/spec_helper.rb +28 -1
- data/spec/worker_spec.rb +98 -94
- metadata +45 -65
data/lib/ayl-beanstalk.rb
CHANGED
data/lib/ayl-beanstalk/engine.rb
CHANGED
@@ -20,7 +20,7 @@ module Ayl
|
|
20
20
|
connected = true
|
21
21
|
begin
|
22
22
|
pool
|
23
|
-
rescue ::
|
23
|
+
rescue ::Beaneater::NotConnected => ex
|
24
24
|
logger.error "#{self.class.name} not connected error: #{ex}"
|
25
25
|
connected = false
|
26
26
|
end
|
@@ -30,10 +30,13 @@ module Ayl
|
|
30
30
|
def submit(message)
|
31
31
|
log_call(:submit) do
|
32
32
|
begin
|
33
|
-
pool.
|
33
|
+
tube = pool.tubes[message.options.queue_name]
|
34
34
|
code = message.to_rrepr
|
35
35
|
logger.info "#{self.class.name} submitting '#{code}' to tube '#{message.options.queue_name}'"
|
36
|
-
|
36
|
+
tube.put(message.to_hash.to_json,
|
37
|
+
pri: message.options.priority,
|
38
|
+
delay: message.options.delay,
|
39
|
+
ttr: message.options.time_to_run)
|
37
40
|
rescue Exception => ex
|
38
41
|
logger.error "Error submitting message to beanstalk: #{ex}"
|
39
42
|
Ayl::Mailer.instance.deliver_message("Error submitting message to beanstalk", ex)
|
data/lib/ayl-beanstalk/job.rb
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
#
|
6
6
|
# I promise that no methods overridden here; only new methods added.
|
7
7
|
#
|
8
|
-
class
|
8
|
+
class Beaneater::Job
|
9
9
|
include Ayl::Logging
|
10
10
|
|
11
11
|
#
|
@@ -13,7 +13,7 @@ class Beanstalk::Job
|
|
13
13
|
# formatted, then nil is returned.
|
14
14
|
#
|
15
15
|
def ayl_message
|
16
|
-
@msg ||= Ayl::Message.from_hash(
|
16
|
+
@msg ||= Ayl::Message.from_hash(JSON.parse(body))
|
17
17
|
rescue Ayl::UnrecoverableMessageException => ex
|
18
18
|
logger.error "Error extracting message from beanstalk job: #{ex}"
|
19
19
|
Ayl::Mailer.instance.deliver_message "Error extracting message from beanstalk job", ex
|
@@ -36,7 +36,9 @@ class Beanstalk::Job
|
|
36
36
|
# process.
|
37
37
|
#
|
38
38
|
def ayl_decay(delay=nil)
|
39
|
-
|
39
|
+
options = {}
|
40
|
+
options[:delay] = delay unless delay.nil?
|
41
|
+
release(options)
|
40
42
|
rescue Exception => ex
|
41
43
|
logger.error "Error decaying job: #{ex}\n#{ex.backtrace.join("\n")}"
|
42
44
|
Ayl::Mailer.instance.deliver_message("Error decaying job", ex)
|
@@ -53,20 +55,22 @@ class Beanstalk::Job
|
|
53
55
|
Ayl::Mailer.instance.deliver_message("Error decaying job", ex)
|
54
56
|
end
|
55
57
|
|
58
|
+
def reserves
|
59
|
+
stats['reserves']
|
60
|
+
end
|
61
|
+
#
|
62
|
+
# If this job message has been configured for 'decay' handling,
|
63
|
+
# then the rules are as follows:
|
56
64
|
#
|
57
|
-
# Handle the decay process by deleting the job if its age is more than
|
58
|
-
# 60 seconds, but delaying it if it is younger than 60 seconds. Obviously
|
59
|
-
# we want to handle any exceptions that occur here.
|
60
65
|
#
|
61
66
|
def handle_decay(ex)
|
62
|
-
|
63
|
-
|
64
|
-
Ayl::Mailer.instance.deliver_message("Deleting decayed job; it just took too long.", ex)
|
65
|
-
logger.debug "Deleting job"
|
66
|
-
ayl_delete
|
67
|
+
if reserves < ayl_message.options.failed_job_count
|
68
|
+
ayl_decay ayl_message.options.failed_job_delay
|
67
69
|
else
|
68
|
-
|
69
|
-
|
70
|
+
# This job has already been reserved too many times.
|
71
|
+
# Bury it.
|
72
|
+
ayl_bury
|
73
|
+
Ayl::Mailer.instance.burying_job(ayl_message.code)
|
70
74
|
end
|
71
75
|
end
|
72
76
|
|
data/lib/ayl-beanstalk/pool.rb
CHANGED
data/lib/ayl-beanstalk/worker.rb
CHANGED
@@ -16,7 +16,7 @@ module Ayl
|
|
16
16
|
logger.debug "#{self.class.name} entering process_messages loop watching: #{Ayl::MessageOptions.default_queue_name}"
|
17
17
|
|
18
18
|
# Set the queue that we will be watching
|
19
|
-
pool.watch(Ayl::MessageOptions.default_queue_name)
|
19
|
+
pool.tubes.watch!(Ayl::MessageOptions.default_queue_name)
|
20
20
|
|
21
21
|
reserve_job do | job |
|
22
22
|
begin
|
@@ -68,7 +68,7 @@ module Ayl
|
|
68
68
|
begin
|
69
69
|
|
70
70
|
# Sit around and wait for a job to become available
|
71
|
-
job = pool.reserve
|
71
|
+
job = pool.tubes.reserve
|
72
72
|
|
73
73
|
rescue Exception => ex
|
74
74
|
|
@@ -13,14 +13,14 @@ describe 'ayl_worker_control script' do
|
|
13
13
|
|
14
14
|
it "should honor the use of the -a option to name the daemon" do
|
15
15
|
ARGV.concat [ '--', '--pid-path', '/tmp', '--name', 'the_name' ]
|
16
|
-
Daemons.
|
16
|
+
expect(Daemons).to receive(:run).with(anything, { :log_dir => '/tmp', :log_output => true, :dir_mode => :normal, :dir => '/tmp', :multiple => true, :app_name => 'the_name' })
|
17
17
|
|
18
18
|
load(@ayl_control_script, true)
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should assume the use of the script for the daemon name if no name argument is specified" do
|
22
22
|
ARGV.concat [ '--', '--pid-path', '/tmp' ]
|
23
|
-
Daemons.
|
23
|
+
expect(Daemons).to receive(:run).with(anything, { :log_dir => '/tmp', :log_output => true, :dir_mode => :normal, :dir => '/tmp', :multiple => true })
|
24
24
|
|
25
25
|
load(@ayl_control_script, true)
|
26
26
|
end
|
data/spec/ayl_worker_spec.rb
CHANGED
@@ -13,34 +13,34 @@ describe "ayl_worker script" do
|
|
13
13
|
|
14
14
|
it "requires that an application path be specified" do
|
15
15
|
Object.any_instance.stub(:puts)
|
16
|
-
|
16
|
+
expect { load(@ayl_script, true) }.to exit_with_code(64)
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should exit with a status code of 0 if help is invoked" do
|
20
20
|
ARGV << '--help'
|
21
21
|
Ayl::CommandLine.stub(:puts)
|
22
|
-
|
22
|
+
expect { load(@ayl_script, true) }.to exit_with_code(0)
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should exit when an invalid argument is specified" do
|
26
26
|
ARGV << '--not-real'
|
27
27
|
|
28
|
-
|
28
|
+
expect { load(@ayl_script, true) }.to raise_error(OptionParser::InvalidOption)
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should set the correct tube name when specified on the command line" do
|
32
32
|
ARGV.concat [ "-a", "support", "-t", "the-tube" ]
|
33
33
|
|
34
|
-
Ayl::MessageOptions.
|
34
|
+
expect(Ayl::MessageOptions).to receive(:default_queue_name=).with('the-tube')
|
35
35
|
|
36
|
-
mock_worker =
|
37
|
-
mock_worker.
|
38
|
-
mock_worker.
|
36
|
+
mock_worker = double("Worker")
|
37
|
+
expect(mock_worker).to receive(:process_messages)
|
38
|
+
expect(mock_worker).to receive(:eval_binding=)
|
39
39
|
|
40
|
-
mock_active_engine =
|
41
|
-
mock_active_engine.
|
40
|
+
mock_active_engine = double("ActiveEngine")
|
41
|
+
expect(mock_active_engine).to receive(:worker).and_return(mock_worker)
|
42
42
|
|
43
|
-
Ayl::Engine.
|
43
|
+
expect(Ayl::Engine).to receive(:get_active_engine).and_return(mock_active_engine)
|
44
44
|
|
45
45
|
load(@ayl_script, true)
|
46
46
|
end
|
@@ -48,16 +48,16 @@ describe "ayl_worker script" do
|
|
48
48
|
it "should set the default tube name when not specified on the command line" do
|
49
49
|
ARGV.concat [ "-a", "support" ]
|
50
50
|
|
51
|
-
Ayl::MessageOptions.
|
51
|
+
expect(Ayl::MessageOptions).to receive(:default_queue_name=).with('default')
|
52
52
|
|
53
|
-
mock_worker =
|
54
|
-
mock_worker.
|
55
|
-
mock_worker.
|
53
|
+
mock_worker = double("Worker")
|
54
|
+
expect(mock_worker).to receive(:process_messages)
|
55
|
+
expect(mock_worker).to receive(:eval_binding=)
|
56
56
|
|
57
|
-
mock_active_engine =
|
58
|
-
mock_active_engine.
|
57
|
+
mock_active_engine = double("ActiveEngine")
|
58
|
+
expect(mock_active_engine).to receive(:worker).and_return(mock_worker)
|
59
59
|
|
60
|
-
Ayl::Engine.
|
60
|
+
expect(Ayl::Engine).to receive(:get_active_engine).and_return(mock_active_engine)
|
61
61
|
|
62
62
|
load(@ayl_script, true)
|
63
63
|
end
|
@@ -65,17 +65,17 @@ describe "ayl_worker script" do
|
|
65
65
|
it "should default to a rails production environment if not specified" do
|
66
66
|
ARGV.concat [ "-a", "support", "-r" ]
|
67
67
|
|
68
|
-
Ayl::MessageOptions.
|
68
|
+
expect(Ayl::MessageOptions).to receive(:default_queue_name=).with('default')
|
69
69
|
|
70
|
-
mock_worker =
|
71
|
-
mock_worker.
|
72
|
-
mock_worker.
|
70
|
+
mock_worker = double("Worker")
|
71
|
+
expect(mock_worker).to receive(:process_messages)
|
72
|
+
expect(mock_worker).to receive(:eval_binding=)
|
73
73
|
|
74
|
-
mock_active_engine =
|
75
|
-
mock_active_engine.
|
74
|
+
mock_active_engine = double("ActiveEngine")
|
75
|
+
expect(mock_active_engine).to receive(:worker).and_return(mock_worker)
|
76
76
|
|
77
|
-
Ayl::Engine.
|
78
|
-
ENV.
|
77
|
+
expect(Ayl::Engine).to receive(:get_active_engine).and_return(mock_active_engine)
|
78
|
+
expect(ENV).to receive(:[]=).with('RAILS_ENV', 'production')
|
79
79
|
|
80
80
|
load(@ayl_script, true)
|
81
81
|
end
|
@@ -83,17 +83,17 @@ describe "ayl_worker script" do
|
|
83
83
|
it "should use the specified rails environment" do
|
84
84
|
ARGV.concat [ "-a", "support", "-r", "-e", "development" ]
|
85
85
|
|
86
|
-
Ayl::MessageOptions.
|
86
|
+
expect(Ayl::MessageOptions).to receive(:default_queue_name=).with('default')
|
87
87
|
|
88
|
-
mock_worker =
|
89
|
-
mock_worker.
|
90
|
-
mock_worker.
|
88
|
+
mock_worker = double("Worker")
|
89
|
+
expect(mock_worker).to receive(:process_messages)
|
90
|
+
expect(mock_worker).to receive(:eval_binding=)
|
91
91
|
|
92
|
-
mock_active_engine =
|
93
|
-
mock_active_engine.
|
92
|
+
mock_active_engine = double("ActiveEngine")
|
93
|
+
expect(mock_active_engine).to receive(:worker).and_return(mock_worker)
|
94
94
|
|
95
|
-
Ayl::Engine.
|
96
|
-
ENV.
|
95
|
+
expect(Ayl::Engine).to receive(:get_active_engine).and_return(mock_active_engine)
|
96
|
+
expect(ENV).to receive(:[]=).with('RAILS_ENV', 'development')
|
97
97
|
|
98
98
|
load(@ayl_script, true)
|
99
99
|
end
|
data/spec/command_line_spec.rb
CHANGED
@@ -10,12 +10,12 @@ describe Ayl::CommandLine do
|
|
10
10
|
|
11
11
|
app_arguments = %w{ -a path-to-app -r -e development }
|
12
12
|
|
13
|
-
Ayl::CommandLine.grab_app_arguments(raw_argv).
|
13
|
+
expect(Ayl::CommandLine.grab_app_arguments(raw_argv)).to eq(app_arguments)
|
14
14
|
end
|
15
15
|
|
16
16
|
it "raise an exception if no application marker exists in the command line arguments" do
|
17
17
|
raw_argv = %w{ start -a path-to-app -r -e development }
|
18
|
-
|
18
|
+
expect { Ayl::CommandLine.grab_app_arguments(raw_argv) }.to raise_exception
|
19
19
|
end
|
20
20
|
|
21
21
|
end
|
@@ -25,18 +25,18 @@ describe Ayl::CommandLine do
|
|
25
25
|
it "should extract the correct options from the command line when the short options are used" do
|
26
26
|
argv = %w{ -t tube_name -e development -a app_path -r -c config/environment -p pid_path -n the_name }
|
27
27
|
parsed_options = Ayl::CommandLine.parse!(argv)
|
28
|
-
parsed_options.
|
28
|
+
expect(parsed_options).to eq({ :tube => 'tube_name', :env => 'development', :app_path => 'app_path', :rails_app => true, :app_require => 'config/environment', :pid_path => 'pid_path', :app_name => 'the_name' })
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should extract the correct options from the command line when the long options are used" do
|
32
32
|
argv = %w{ --tube tube_name --environment development --app-path app_path --rails --require config/environment --pid-path pid_path --name the_name }
|
33
33
|
parsed_options = Ayl::CommandLine.parse!(argv)
|
34
|
-
parsed_options.
|
34
|
+
expect(parsed_options).to eq({ :tube => 'tube_name', :env => 'development', :app_path => 'app_path', :rails_app => true, :app_require => 'config/environment', :pid_path => 'pid_path', :app_name => 'the_name' })
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should raise an exception if invalid arguments are provided" do
|
38
38
|
argv = %w{ --tuber tube_name --environmen development --apppath app_path --rail --require config/environment --pidpath pid_path }
|
39
|
-
|
39
|
+
expect { Ayl::CommandLine.parse!(argv) }.to raise_exception
|
40
40
|
end
|
41
41
|
|
42
42
|
end
|
data/spec/engine_spec.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'beanstalk-client'
|
3
2
|
require 'active_record'
|
4
3
|
require 'active_record/errors'
|
5
4
|
|
@@ -16,26 +15,26 @@ describe Ayl::Beanstalk::Engine do
|
|
16
15
|
end
|
17
16
|
|
18
17
|
it "should default to localhost and 11300 as the host port for the beanstalkd server" do
|
19
|
-
@engine.host.
|
20
|
-
@engine.port.
|
18
|
+
expect(@engine.host).to eq('localhost')
|
19
|
+
expect(@engine.port).to eq(11300)
|
21
20
|
end
|
22
21
|
|
23
22
|
it "should respond true to the asynchronous? message" do
|
24
|
-
@engine.asynchronous
|
23
|
+
expect(@engine.asynchronous?).to be true
|
25
24
|
end
|
26
25
|
|
27
26
|
it "should return true if it has a valid connection to beanstalk" do
|
28
|
-
mock_pool =
|
27
|
+
mock_pool = double("Beanstalk::Pool")
|
29
28
|
|
30
|
-
|
29
|
+
expect(Beaneater).to receive(:new).with("localhost:11300").and_return(mock_pool)
|
31
30
|
|
32
|
-
@engine.is_connected
|
31
|
+
expect(@engine.is_connected?).to be true
|
33
32
|
end
|
34
33
|
|
35
34
|
it "should return false if it does not have a valid connection to beanstalk" do
|
36
|
-
|
35
|
+
expect(Beaneater).to receive(:new).with("localhost:11300").and_raise(Beaneater::NotConnected)
|
37
36
|
|
38
|
-
@engine.is_connected
|
37
|
+
expect(@engine.is_connected?).to be false
|
39
38
|
end
|
40
39
|
|
41
40
|
context "Message Submission" do
|
@@ -45,11 +44,14 @@ describe Ayl::Beanstalk::Engine do
|
|
45
44
|
end
|
46
45
|
|
47
46
|
it "should submit the specified message to beanstalk" do
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
47
|
+
mock_tube = double("Tube")
|
48
|
+
expect(mock_tube).to receive(:put).
|
49
|
+
with({ :type => :ayl, :failed_job_handler => 'delete', :code => "23.to_s(2)" }.to_json, {
|
50
|
+
pri: 512, delay: 0, ttr: 120})
|
51
|
+
mock_pool = double("Beanstalk::Pool")
|
52
|
+
expect(mock_pool).to receive(:tubes).and_return({'default' => mock_tube})
|
53
|
+
|
54
|
+
expect(Beaneater).to receive(:new).with("localhost:11300").and_return(mock_pool)
|
53
55
|
|
54
56
|
@engine.submit(@msg)
|
55
57
|
end
|
data/spec/job_spec.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'beanstalk-client'
|
3
2
|
|
4
|
-
describe
|
3
|
+
describe Beaneater::Job do
|
5
4
|
|
6
5
|
before(:each) do
|
7
|
-
@job =
|
6
|
+
@job = Beaneater::Job.new(nil,{id: '1', body: '{"ayl":"thing"}', status: 'RESERVED'})
|
8
7
|
@job.stub_chain(:logger, :error)
|
9
8
|
@job.stub_chain(:logger, :debug)
|
10
9
|
end
|
@@ -12,36 +11,32 @@ describe Beanstalk::Job do
|
|
12
11
|
context '#ayl_message' do
|
13
12
|
|
14
13
|
it "should return the message constructed from the body of the job" do
|
15
|
-
@job.stub(:ybody).and_return('the body')
|
16
14
|
msg = Ayl::Message.new(nil,nil,nil)
|
17
|
-
Ayl::Message.
|
15
|
+
expect(Ayl::Message).to receive(:from_hash).with({"ayl"=>"thing"}).and_return(msg)
|
18
16
|
|
19
|
-
@job.ayl_message.
|
17
|
+
expect(@job.ayl_message).to eq(msg)
|
20
18
|
end
|
21
19
|
|
22
20
|
it "should return the same message constructed from the body of the job on subsequent calls" do
|
23
|
-
@job.stub(:ybody).and_return('the body')
|
24
21
|
msg = Ayl::Message.new(nil,nil,nil)
|
25
|
-
Ayl::Message.
|
22
|
+
expect(Ayl::Message).to receive(:from_hash).with({"ayl"=>"thing"}).and_return(msg)
|
26
23
|
|
27
|
-
@job.ayl_message.
|
24
|
+
expect(@job.ayl_message).to eq(msg)
|
28
25
|
|
29
|
-
@job.ayl_message.
|
26
|
+
expect(@job.ayl_message).to eq(msg)
|
30
27
|
end
|
31
28
|
|
32
29
|
it "should return nil and send an email if the message body was bad" do
|
33
|
-
@job.stub(:ybody).and_return('the body')
|
34
30
|
msg = Ayl::Message.new(nil,nil,nil)
|
35
31
|
ex = Ayl::UnrecoverableMessageException.new
|
36
|
-
Ayl::Message.
|
32
|
+
expect(Ayl::Message).to receive(:from_hash).with({"ayl"=>"thing"}).and_raise(ex)
|
37
33
|
|
38
|
-
|
39
|
-
|
40
|
-
mock_mailer.should_receive(:deliver_message).with(anything(), ex)
|
41
|
-
end
|
42
|
-
end
|
34
|
+
mock_mailer = double("Mailer")
|
35
|
+
expect(mock_mailer).to receive(:deliver_message).with(anything(), ex)
|
43
36
|
|
44
|
-
|
37
|
+
expect(Ayl::Mailer).to receive(:instance).and_return(mock_mailer)
|
38
|
+
|
39
|
+
expect(@job.ayl_message).to be_nil
|
45
40
|
end
|
46
41
|
|
47
42
|
end
|
@@ -49,20 +44,19 @@ describe Beanstalk::Job do
|
|
49
44
|
context '#ayl_delete' do
|
50
45
|
|
51
46
|
it "should call the delete method on the job" do
|
52
|
-
@job.
|
47
|
+
expect(@job).to receive(:delete)
|
53
48
|
|
54
49
|
@job.ayl_delete
|
55
50
|
end
|
56
51
|
|
57
52
|
it "should send an email if the delete method raises an exception" do
|
58
53
|
ex = Exception.new
|
59
|
-
@job.
|
54
|
+
expect(@job).to receive(:delete).and_raise(ex)
|
55
|
+
|
56
|
+
mock_mailer = double("Mailer")
|
57
|
+
expect(mock_mailer).to receive(:deliver_message).with(anything(), ex)
|
60
58
|
|
61
|
-
Ayl::Mailer.
|
62
|
-
mock("Mailer").tap do | mock_mailer |
|
63
|
-
mock_mailer.should_receive(:deliver_message).with(anything(), ex)
|
64
|
-
end
|
65
|
-
end
|
59
|
+
expect(Ayl::Mailer).to receive(:instance).and_return(mock_mailer)
|
66
60
|
|
67
61
|
@job.ayl_delete
|
68
62
|
end
|
@@ -72,32 +66,31 @@ describe Beanstalk::Job do
|
|
72
66
|
context '#ayl_decay' do
|
73
67
|
|
74
68
|
it "should call the decay with no arguments when nil delay is specified" do
|
75
|
-
@job.
|
69
|
+
expect(@job).to receive(:release).with({})
|
76
70
|
|
77
71
|
@job.ayl_decay(nil)
|
78
72
|
end
|
79
73
|
|
80
74
|
it "should call the decay with no arguments when no delay is specified" do
|
81
|
-
@job.
|
75
|
+
expect(@job).to receive(:release).with({})
|
82
76
|
|
83
77
|
@job.ayl_decay
|
84
78
|
end
|
85
79
|
|
86
80
|
it "should call the decay with no arguments when no delay is specified" do
|
87
|
-
@job.
|
81
|
+
expect(@job).to receive(:release).with({delay: 10})
|
88
82
|
|
89
83
|
@job.ayl_decay(10)
|
90
84
|
end
|
91
85
|
|
92
86
|
it "should send an email if the decay method raises an exception" do
|
93
87
|
ex = Exception.new
|
94
|
-
@job.
|
88
|
+
expect(@job).to receive(:release).and_raise(ex)
|
89
|
+
|
90
|
+
mock_mailer = double("Mailer")
|
91
|
+
expect(mock_mailer).to receive(:deliver_message).with(anything(), ex)
|
95
92
|
|
96
|
-
Ayl::Mailer.
|
97
|
-
mock("Mailer").tap do | mock_mailer |
|
98
|
-
mock_mailer.should_receive(:deliver_message).with(anything(), ex)
|
99
|
-
end
|
100
|
-
end
|
93
|
+
expect(Ayl::Mailer).to receive(:instance).and_return(mock_mailer)
|
101
94
|
|
102
95
|
@job.ayl_decay
|
103
96
|
end
|
@@ -107,20 +100,19 @@ describe Beanstalk::Job do
|
|
107
100
|
context '#ayl_bury' do
|
108
101
|
|
109
102
|
it "should call the bury method on the job" do
|
110
|
-
@job.
|
103
|
+
expect(@job).to receive(:bury)
|
111
104
|
|
112
105
|
@job.ayl_bury
|
113
106
|
end
|
114
107
|
|
115
108
|
it "should send an email if the bury method raises an exception" do
|
116
109
|
ex = Exception.new
|
117
|
-
@job.
|
110
|
+
expect(@job).to receive(:bury).and_raise(ex)
|
118
111
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
end
|
112
|
+
mock_mailer = double("Mailer")
|
113
|
+
expect(mock_mailer).to receive(:deliver_message).with(anything(), ex)
|
114
|
+
|
115
|
+
expect(Ayl::Mailer).to receive(:instance).and_return(mock_mailer)
|
124
116
|
|
125
117
|
@job.ayl_bury
|
126
118
|
end
|
@@ -129,37 +121,39 @@ describe Beanstalk::Job do
|
|
129
121
|
|
130
122
|
context '#handle_decay' do
|
131
123
|
|
132
|
-
it "should decay the job if the
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
ex = Exception.new
|
124
|
+
it "should decay the job if the number of times the job has been reserved is less than the configured failed job count" do
|
125
|
+
mock_options = double("options")
|
126
|
+
expect(mock_options).to receive(:failed_job_count).and_return(2)
|
127
|
+
expect(mock_options).to receive(:failed_job_delay).and_return(3)
|
137
128
|
|
138
|
-
|
139
|
-
|
129
|
+
mock_message = double("message")
|
130
|
+
expect(mock_message).to receive(:options).exactly(2).times.and_return(mock_options)
|
140
131
|
|
141
|
-
|
142
|
-
@job.
|
143
|
-
@job.
|
144
|
-
|
145
|
-
ex = Exception.new
|
132
|
+
expect(@job).to receive(:reserves).and_return(1)
|
133
|
+
expect(@job).to receive(:ayl_message).exactly(2).times.and_return(mock_message)
|
134
|
+
expect(@job).to receive(:ayl_decay).with(3)
|
146
135
|
|
147
|
-
@job.handle_decay(
|
136
|
+
@job.handle_decay(Exception.new)
|
148
137
|
end
|
149
138
|
|
150
|
-
it "should
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
139
|
+
it "should bury the job if the number of times the job has been reserved is the same or more than the configured failed job count" do
|
140
|
+
mock_options = double("options")
|
141
|
+
expect(mock_options).to receive(:failed_job_count).and_return(2)
|
142
|
+
|
143
|
+
mock_message = double("message")
|
144
|
+
expect(mock_message).to receive(:options).and_return(mock_options)
|
145
|
+
expect(mock_message).to receive(:code).and_return('the code')
|
146
|
+
|
147
|
+
expect(@job).to receive(:reserves).and_return(2)
|
148
|
+
expect(@job).to receive(:ayl_message).exactly(2).times.and_return(mock_message)
|
149
|
+
expect(@job).to receive(:ayl_bury)
|
150
|
+
|
151
|
+
mock_mailer = double('Ayl::Mailer')
|
152
|
+
expect(mock_mailer).to receive(:burying_job).with('the code')
|
155
153
|
|
156
|
-
Ayl::Mailer.
|
157
|
-
mock("Mailer").tap do | mock_mailer |
|
158
|
-
mock_mailer.should_receive(:deliver_message).with(anything(), ex)
|
159
|
-
end
|
160
|
-
end
|
154
|
+
expect(Ayl::Mailer).to receive(:instance).and_return(mock_mailer)
|
161
155
|
|
162
|
-
@job.handle_decay(
|
156
|
+
@job.handle_decay(Exception.new)
|
163
157
|
end
|
164
158
|
|
165
159
|
end
|