ayl-beanstalk 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ayl-beanstalk.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'ayl'
2
- require 'beanstalk-client'
2
+ require 'beaneater'
3
+ require 'json'
3
4
  require 'ayl-beanstalk/job'
4
5
  require 'ayl-beanstalk/exceptions'
5
6
  require 'ayl-beanstalk/pool'
@@ -20,7 +20,7 @@ module Ayl
20
20
  connected = true
21
21
  begin
22
22
  pool
23
- rescue ::Beanstalk::NotConnected => ex
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.use(message.options.queue_name)
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
- pool.yput(message.to_hash, message.options.priority, message.options.delay, message.options.time_to_run)
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)
@@ -5,7 +5,7 @@
5
5
  #
6
6
  # I promise that no methods overridden here; only new methods added.
7
7
  #
8
- class Beanstalk::Job
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(ybody)
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
- decay(*[ delay ].compact)
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
- logger.debug "Age of job: #{age}"
63
- if age > 60
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
- logger.debug "Decaying job"
69
- ayl_decay
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
 
@@ -5,7 +5,7 @@ module Ayl
5
5
  module Pool
6
6
 
7
7
  def pool
8
- @pool ||= ::Beanstalk::Pool.new([ "#{@host}:#{@port}" ])
8
+ @pool ||= ::Beaneater.new("#{@host}:#{@port}")
9
9
  end
10
10
 
11
11
  end
@@ -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.should_receive(:run).with(anything, { :log_dir => '/tmp', :log_output => true, :dir_mode => :normal, :dir => '/tmp', :multiple => true, :app_name => 'the_name' })
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.should_receive(:run).with(anything, { :log_dir => '/tmp', :log_output => true, :dir_mode => :normal, :dir => '/tmp', :multiple => true })
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
@@ -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
- lambda { load(@ayl_script, true) }.should exit_with_code(64)
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
- lambda { load(@ayl_script, true) }.should exit_with_code(0)
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
- lambda { load(@ayl_script, true) }.should raise_error(OptionParser::InvalidOption)
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.should_receive(:default_queue_name=).with('the-tube')
34
+ expect(Ayl::MessageOptions).to receive(:default_queue_name=).with('the-tube')
35
35
 
36
- mock_worker = mock("Worker")
37
- mock_worker.should_receive(:process_messages)
38
- mock_worker.should_receive(:eval_binding=)
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 = mock("ActiveEngine")
41
- mock_active_engine.should_receive(:worker).and_return(mock_worker)
40
+ mock_active_engine = double("ActiveEngine")
41
+ expect(mock_active_engine).to receive(:worker).and_return(mock_worker)
42
42
 
43
- Ayl::Engine.should_receive(:get_active_engine).and_return(mock_active_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.should_receive(:default_queue_name=).with('default')
51
+ expect(Ayl::MessageOptions).to receive(:default_queue_name=).with('default')
52
52
 
53
- mock_worker = mock("Worker")
54
- mock_worker.should_receive(:process_messages)
55
- mock_worker.should_receive(:eval_binding=)
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 = mock("ActiveEngine")
58
- mock_active_engine.should_receive(:worker).and_return(mock_worker)
57
+ mock_active_engine = double("ActiveEngine")
58
+ expect(mock_active_engine).to receive(:worker).and_return(mock_worker)
59
59
 
60
- Ayl::Engine.should_receive(:get_active_engine).and_return(mock_active_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.should_receive(:default_queue_name=).with('default')
68
+ expect(Ayl::MessageOptions).to receive(:default_queue_name=).with('default')
69
69
 
70
- mock_worker = mock("Worker")
71
- mock_worker.should_receive(:process_messages)
72
- mock_worker.should_receive(:eval_binding=)
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 = mock("ActiveEngine")
75
- mock_active_engine.should_receive(:worker).and_return(mock_worker)
74
+ mock_active_engine = double("ActiveEngine")
75
+ expect(mock_active_engine).to receive(:worker).and_return(mock_worker)
76
76
 
77
- Ayl::Engine.should_receive(:get_active_engine).and_return(mock_active_engine)
78
- ENV.should_receive(:[]=).with('RAILS_ENV', 'production')
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.should_receive(:default_queue_name=).with('default')
86
+ expect(Ayl::MessageOptions).to receive(:default_queue_name=).with('default')
87
87
 
88
- mock_worker = mock("Worker")
89
- mock_worker.should_receive(:process_messages)
90
- mock_worker.should_receive(:eval_binding=)
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 = mock("ActiveEngine")
93
- mock_active_engine.should_receive(:worker).and_return(mock_worker)
92
+ mock_active_engine = double("ActiveEngine")
93
+ expect(mock_active_engine).to receive(:worker).and_return(mock_worker)
94
94
 
95
- Ayl::Engine.should_receive(:get_active_engine).and_return(mock_active_engine)
96
- ENV.should_receive(:[]=).with('RAILS_ENV', 'development')
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
@@ -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).should == app_arguments
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
- lambda { Ayl::CommandLine.grab_app_arguments(raw_argv) }.should raise_exception
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.should == { :tube => 'tube_name', :env => 'development', :app_path => 'app_path', :rails_app => true, :app_require => 'config/environment', :pid_path => 'pid_path', :app_name => 'the_name' }
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.should == { :tube => 'tube_name', :env => 'development', :app_path => 'app_path', :rails_app => true, :app_require => 'config/environment', :pid_path => 'pid_path', :app_name => 'the_name' }
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
- lambda { Ayl::CommandLine.parse!(argv) }.should raise_exception
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.should == 'localhost'
20
- @engine.port.should == 11300
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?.should be_true
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 = mock("Beanstalk::Pool")
27
+ mock_pool = double("Beanstalk::Pool")
29
28
 
30
- ::Beanstalk::Pool.should_receive(:new).with([ "localhost:11300" ]).and_return(mock_pool)
29
+ expect(Beaneater).to receive(:new).with("localhost:11300").and_return(mock_pool)
31
30
 
32
- @engine.is_connected?.should be_true
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
- ::Beanstalk::Pool.should_receive(:new).with([ "localhost:11300" ]).and_raise(::Beanstalk::NotConnected)
35
+ expect(Beaneater).to receive(:new).with("localhost:11300").and_raise(Beaneater::NotConnected)
37
36
 
38
- @engine.is_connected?.should be_false
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
- mock_pool = mock("Beanstalk::Pool")
49
- mock_pool.should_receive(:use).with("default")
50
- mock_pool.should_receive(:yput).with( { :type => :ayl, :failed_job_handler => 'delete', :code => "23.to_s(2)" }, 512, 0, 120)
51
-
52
- ::Beanstalk::Pool.should_receive(:new).with([ "localhost:11300" ]).and_return(mock_pool)
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 Beanstalk::Job do
3
+ describe Beaneater::Job do
5
4
 
6
5
  before(:each) do
7
- @job = Beanstalk::Job.new(nil,nil,nil)
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.should_receive(:from_hash).with('the body').and_return(msg)
15
+ expect(Ayl::Message).to receive(:from_hash).with({"ayl"=>"thing"}).and_return(msg)
18
16
 
19
- @job.ayl_message.should == msg
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.should_receive(:from_hash).with('the body').and_return(msg)
22
+ expect(Ayl::Message).to receive(:from_hash).with({"ayl"=>"thing"}).and_return(msg)
26
23
 
27
- @job.ayl_message.should == msg
24
+ expect(@job.ayl_message).to eq(msg)
28
25
 
29
- @job.ayl_message.should == msg
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.should_receive(:from_hash).with('the body').and_raise(ex)
32
+ expect(Ayl::Message).to receive(:from_hash).with({"ayl"=>"thing"}).and_raise(ex)
37
33
 
38
- Ayl::Mailer.should_receive(:instance).and_return do
39
- mock("Mailer").tap do | mock_mailer |
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
- @job.ayl_message.should == nil
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.should_receive(:delete)
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.should_receive(:delete).and_raise(ex)
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.should_receive(:instance).and_return do
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.should_receive(:decay).with()
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.should_receive(:decay).with()
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.should_receive(:decay).with(10)
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.should_receive(:decay).and_raise(ex)
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.should_receive(:instance).and_return do
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.should_receive(:bury)
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.should_receive(:bury).and_raise(ex)
110
+ expect(@job).to receive(:bury).and_raise(ex)
118
111
 
119
- Ayl::Mailer.should_receive(:instance).and_return do
120
- mock("Mailer").tap do | mock_mailer |
121
- mock_mailer.should_receive(:deliver_message).with(anything(), ex)
122
- end
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 age of the job is less than 60 seconds" do
133
- @job.should_receive(:age).at_least(1).times.and_return(2)
134
- @job.should_receive(:ayl_decay)
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
- @job.handle_decay(ex)
139
- end
129
+ mock_message = double("message")
130
+ expect(mock_message).to receive(:options).exactly(2).times.and_return(mock_options)
140
131
 
141
- it "should decay the job if the age of the job is exactly 60 seconds" do
142
- @job.should_receive(:age).at_least(1).times.and_return(60)
143
- @job.should_receive(:ayl_decay)
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(ex)
136
+ @job.handle_decay(Exception.new)
148
137
  end
149
138
 
150
- it "should delete the job and send an email if the job was older than 60 seconds" do
151
- @job.should_receive(:age).at_least(1).times.and_return(61)
152
- @job.should_receive(:ayl_delete)
153
-
154
- ex = Exception.new
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.should_receive(:instance).and_return do
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(ex)
156
+ @job.handle_decay(Exception.new)
163
157
  end
164
158
 
165
159
  end