ayl 0.1.1 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "ayl"
8
- s.version = "0.1.1"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dave Sieh"]
12
- s.date = "2013-02-25"
12
+ s.date = "2013-07-22"
13
13
  s.description = "Invoke code At Your Leisure. ayl is a small framework that simplifies the process of implementing asynchronous method calls in Ruby."
14
14
  s.email = "j0hnds@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
31
31
  "lib/ayl/extensions.rb",
32
32
  "lib/ayl/logger.rb",
33
33
  "lib/ayl/logging.rb",
34
+ "lib/ayl/mailer.rb",
34
35
  "lib/ayl/message.rb",
35
36
  "lib/ayl/message_options.rb",
36
37
  "lib/ayl/unrecoverable_message_exception.rb",
@@ -38,6 +39,7 @@ Gem::Specification.new do |s|
38
39
  "spec/engine_spec.rb",
39
40
  "spec/extensions_spec.rb",
40
41
  "spec/logger_spec.rb",
42
+ "spec/mailer_spec.rb",
41
43
  "spec/message_options_spec.rb",
42
44
  "spec/message_spec.rb",
43
45
  "spec/spec_helper.rb",
data/lib/ayl.rb CHANGED
@@ -6,3 +6,4 @@ require 'ayl/message_options'
6
6
  require 'ayl/unrecoverable_message_exception'
7
7
  require 'ayl/logger'
8
8
  require 'ayl/worker'
9
+ require 'ayl/mailer'
@@ -0,0 +1,21 @@
1
+ module Ayl
2
+
3
+ class Mailer
4
+ include Singleton
5
+
6
+ attr_reader :mailer
7
+
8
+ def mailer=(mailer)
9
+ raise "Mailer implement the 'ayl_message(message, exception)' method" if mailer && !mailer.respond_to?(:ayl_message)
10
+ @mailer = mailer
11
+ end
12
+
13
+ def deliver_message(message, exception=nil)
14
+ mailer.ayl_message(message, exception).deliver if mailer && mailer.respond_to?(:ayl_message)
15
+ rescue Exception => ex
16
+ Ayl::Logger.instance.error("Error sending ayl email message: #{ex.backtrace.join("\n")}")
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -21,6 +21,7 @@ module Ayl
21
21
  Message.new(nil, nil, MessageOptions.new).tap do | m |
22
22
  m.send(:message_hash=, message_hash)
23
23
  m.send(:code=, code)
24
+ m.options.decay_failed_job = message_hash[:decay_failed_job] if message_hash[:decay_failed_job]
24
25
  end
25
26
 
26
27
  end
@@ -32,6 +33,7 @@ module Ayl
32
33
  def to_hash
33
34
  @message_hash ||= {
34
35
  :type => :ayl,
36
+ :decay_failed_job => options.decay_failed_job,
35
37
  :code => to_rrepr
36
38
  }
37
39
  end
@@ -2,12 +2,12 @@ module Ayl
2
2
 
3
3
  class MessageOptions
4
4
 
5
- OPTIONS = [ :priority, :fuzz, :delay, :time_to_run, :queue_name ]
5
+ OPTIONS = [ :priority, :fuzz, :delay, :time_to_run, :queue_name, :decay_failed_job ]
6
6
 
7
7
  attr_accessor *OPTIONS
8
8
 
9
9
  class << self
10
- attr_accessor :default_priority, :default_fuzz, :default_delay, :default_time_to_run, :default_queue_name
10
+ attr_accessor :default_priority, :default_fuzz, :default_delay, :default_time_to_run, :default_queue_name, :default_decay_failed_job
11
11
  end
12
12
 
13
13
  # Set the default options
@@ -16,6 +16,7 @@ module Ayl
16
16
  self.default_delay = 0
17
17
  self.default_time_to_run = 120
18
18
  self.default_queue_name = 'default'
19
+ self.default_decay_failed_job = false
19
20
 
20
21
  def initialize(opts=nil)
21
22
  opts ||= {}
@@ -68,7 +68,7 @@ describe Ayl::Engine do
68
68
 
69
69
  it "should simply execute the code provided in the message submission" do
70
70
  mock_logger = mock("Ayl::Logger")
71
- mock_logger.stub(:info)
71
+ mock_logger.stub(:debug)
72
72
 
73
73
  Ayl::Logger.instance.logger = mock_logger
74
74
 
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ayl::Mailer do
4
+
5
+ before(:each) do
6
+ Ayl::Mailer.instance.mailer = nil
7
+ end
8
+
9
+ context 'Mailer configuration' do
10
+
11
+ context "#logger" do
12
+
13
+ it "should respond with nil if a mailer isn't configured" do
14
+ Ayl::Mailer.instance.mailer.should be_nil
15
+ end
16
+
17
+ it "should raise an error if an attempt is made to configure the mailer with an invalid object" do
18
+ lambda { Ayl::Mailer.instance.mailer = Object.new }.should raise_error
19
+ end
20
+
21
+ it "should allow a valid mailer to be configured" do
22
+ mock_mailer = mock("MyMailer")
23
+ mock_mailer.stub(:ayl_message)
24
+
25
+ lambda { Ayl::Mailer.instance.mailer = mock_mailer }.should_not raise_error
26
+ Ayl::Mailer.instance.mailer.should == mock_mailer
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
33
+ context "Sending messages" do
34
+
35
+ context '#deliver_message' do
36
+
37
+ it "should do nothing if no mailer is configured" do
38
+ Ayl::Mailer.instance.deliver_message("The message")
39
+ end
40
+
41
+ it "should deliver a message and no stack trace if a valid mailer is configured" do
42
+ mock_mailer = mock("MyMailer")
43
+ mock_mailer.should_receive(:ayl_message).with('The Message', nil).and_return do
44
+ mock("MailMessage").tap do | mail_message |
45
+ mail_message.should_receive(:deliver)
46
+ end
47
+ end
48
+
49
+ Ayl::Mailer.instance.mailer = mock_mailer
50
+
51
+ Ayl::Mailer.instance.deliver_message('The Message')
52
+ end
53
+
54
+ it "should deliver a message and a stack trace if a valid mailer is configured" do
55
+ mock_mailer = mock("MyMailer")
56
+ mock_mailer.should_receive(:ayl_message).with('The Message', 'StackTrace').and_return do
57
+ mock("MailMessage").tap do | mail_message |
58
+ mail_message.should_receive(:deliver)
59
+ end
60
+ end
61
+
62
+ Ayl::Mailer.instance.mailer = mock_mailer
63
+
64
+ Ayl::Mailer.instance.deliver_message('The Message', 'StackTrace')
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+
71
+ end
@@ -10,6 +10,7 @@ describe Ayl::MessageOptions do
10
10
  Ayl::MessageOptions.default_delay.should == 0
11
11
  Ayl::MessageOptions.default_time_to_run.should == 120
12
12
  Ayl::MessageOptions.default_queue_name.should == 'default'
13
+ Ayl::MessageOptions.default_decay_failed_job.should == false
13
14
  end
14
15
 
15
16
  it "should reflect the changes if changes have been made" do
@@ -18,12 +19,14 @@ describe Ayl::MessageOptions do
18
19
  Ayl::MessageOptions.default_delay = 2300
19
20
  Ayl::MessageOptions.default_time_to_run = 1
20
21
  Ayl::MessageOptions.default_queue_name = 'different'
22
+ Ayl::MessageOptions.default_decay_failed_job = true
21
23
 
22
24
  Ayl::MessageOptions.default_priority.should == 256
23
25
  Ayl::MessageOptions.default_fuzz.should == 18
24
26
  Ayl::MessageOptions.default_delay.should == 2300
25
27
  Ayl::MessageOptions.default_time_to_run.should == 1
26
28
  Ayl::MessageOptions.default_queue_name.should == 'different'
29
+ Ayl::MessageOptions.default_decay_failed_job.should == true
27
30
  end
28
31
 
29
32
  end
@@ -38,6 +41,7 @@ describe Ayl::MessageOptions do
38
41
  mo.delay.should == Ayl::MessageOptions.default_delay
39
42
  mo.time_to_run.should == Ayl::MessageOptions.default_time_to_run
40
43
  mo.queue_name.should == Ayl::MessageOptions.default_queue_name
44
+ mo.decay_failed_job.should == Ayl::MessageOptions.default_decay_failed_job
41
45
  end
42
46
 
43
47
  it "should respond with the correct defaults when initialized with nil" do
@@ -48,6 +52,7 @@ describe Ayl::MessageOptions do
48
52
  mo.delay.should == Ayl::MessageOptions.default_delay
49
53
  mo.time_to_run.should == Ayl::MessageOptions.default_time_to_run
50
54
  mo.queue_name.should == Ayl::MessageOptions.default_queue_name
55
+ mo.decay_failed_job.should == Ayl::MessageOptions.default_decay_failed_job
51
56
  end
52
57
 
53
58
  it "should respond with the correct defaults when initialized with no parameter" do
@@ -58,10 +63,11 @@ describe Ayl::MessageOptions do
58
63
  mo.delay.should == Ayl::MessageOptions.default_delay
59
64
  mo.time_to_run.should == Ayl::MessageOptions.default_time_to_run
60
65
  mo.queue_name.should == Ayl::MessageOptions.default_queue_name
66
+ mo.decay_failed_job.should == Ayl::MessageOptions.default_decay_failed_job
61
67
  end
62
68
 
63
69
  it "should respond with the correct values when all are specified in the options" do
64
- opts = { :priority => 22, :fuzz => 88, :delay => 99, :time_to_run => 11, :queue_name => 'stub' }
70
+ opts = { :priority => 22, :fuzz => 88, :delay => 99, :time_to_run => 11, :queue_name => 'stub', :decay_failed_job => true }
65
71
  mo = Ayl::MessageOptions.new(opts)
66
72
 
67
73
  mo.priority.should == opts[:priority]
@@ -69,6 +75,7 @@ describe Ayl::MessageOptions do
69
75
  mo.delay.should == opts[:delay]
70
76
  mo.time_to_run.should == opts[:time_to_run]
71
77
  mo.queue_name.should == opts[:queue_name]
78
+ mo.decay_failed_job.should == opts[:decay_failed_job]
72
79
  end
73
80
 
74
81
  it "should respond with the correct values when some values are specified in the options" do
@@ -80,6 +87,7 @@ describe Ayl::MessageOptions do
80
87
  mo.delay.should == opts[:delay]
81
88
  mo.time_to_run.should == Ayl::MessageOptions.default_time_to_run
82
89
  mo.queue_name.should == Ayl::MessageOptions.default_queue_name
90
+ mo.decay_failed_job.should == Ayl::MessageOptions.default_decay_failed_job
83
91
  end
84
92
 
85
93
  it "should raise an exception when an invalid option is provided in the hash" do
@@ -2,6 +2,16 @@ require 'spec_helper'
2
2
 
3
3
  describe Ayl::Message do
4
4
 
5
+ before(:each) do
6
+ # Need to reset the default message options each time
7
+ Ayl::MessageOptions.default_priority = 512
8
+ Ayl::MessageOptions.default_fuzz = 0
9
+ Ayl::MessageOptions.default_delay = 0
10
+ Ayl::MessageOptions.default_time_to_run = 120
11
+ Ayl::MessageOptions.default_queue_name = 'default'
12
+ Ayl::MessageOptions.default_decay_failed_job = false
13
+ end
14
+
5
15
  context "Initialization" do
6
16
 
7
17
  it "should accept an object, selector options and a set of arguments" do
@@ -47,7 +57,14 @@ describe Ayl::Message do
47
57
  it "should package up the message into a hash" do
48
58
  options = Ayl::MessageOptions.new
49
59
  m = Ayl::Message.new("object", :method_name, options, "arg1", "arg2")
50
- m.to_hash.should == { :type => :ayl, :code => "\"object\".method_name(\"arg1\", \"arg2\")" }
60
+ m.to_hash.should == { :type => :ayl, :decay_failed_job => false, :code => "\"object\".method_name(\"arg1\", \"arg2\")" }
61
+ end
62
+
63
+ it "should package up the message into a hash when the decay failed job has been set" do
64
+ options = Ayl::MessageOptions.new
65
+ options.decay_failed_job = true
66
+ m = Ayl::Message.new("object", :method_name, options, "arg1", "arg2")
67
+ m.to_hash.should == { :type => :ayl, :decay_failed_job => true, :code => "\"object\".method_name(\"arg1\", \"arg2\")" }
51
68
  end
52
69
 
53
70
  it "should be able to create a message from a hash with code that has arguments" do
@@ -86,6 +103,30 @@ describe Ayl::Message do
86
103
  m.to_hash.should === m_hash
87
104
  end
88
105
 
106
+ it "should create a message with decay_failed job set to false if not in the original hash" do
107
+ m_hash = { :type => :ayl, :code => "String._ayl_after_create(2.to_s(2))" }
108
+ m = Ayl::Message.from_hash(m_hash)
109
+ m.options.is_a?(Ayl::MessageOptions).should be_true
110
+ m.to_hash.should === m_hash
111
+ m.options.decay_failed_job.should be_false
112
+ end
113
+
114
+ it "should create a message with decay_failed job set to false if in the original hash as false" do
115
+ m_hash = { :type => :ayl, :decay_failed_job => false, :code => "String._ayl_after_create(2.to_s(2))" }
116
+ m = Ayl::Message.from_hash(m_hash)
117
+ m.options.is_a?(Ayl::MessageOptions).should be_true
118
+ m.to_hash.should === m_hash
119
+ m.options.decay_failed_job.should be_false
120
+ end
121
+
122
+ it "should create a message with decay_failed job set to true if in the original hash as true" do
123
+ m_hash = { :type => :ayl, :decay_failed_job => true, :code => "String._ayl_after_create(2.to_s(2))" }
124
+ m = Ayl::Message.from_hash(m_hash)
125
+ m.options.is_a?(Ayl::MessageOptions).should be_true
126
+ m.to_hash.should === m_hash
127
+ m.options.decay_failed_job.should be_true
128
+ end
129
+
89
130
 
90
131
  end
91
132
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ayl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-25 00:00:00.000000000 Z
12
+ date: 2013-07-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -114,6 +114,7 @@ files:
114
114
  - lib/ayl/extensions.rb
115
115
  - lib/ayl/logger.rb
116
116
  - lib/ayl/logging.rb
117
+ - lib/ayl/mailer.rb
117
118
  - lib/ayl/message.rb
118
119
  - lib/ayl/message_options.rb
119
120
  - lib/ayl/unrecoverable_message_exception.rb
@@ -121,6 +122,7 @@ files:
121
122
  - spec/engine_spec.rb
122
123
  - spec/extensions_spec.rb
123
124
  - spec/logger_spec.rb
125
+ - spec/mailer_spec.rb
124
126
  - spec/message_options_spec.rb
125
127
  - spec/message_spec.rb
126
128
  - spec/spec_helper.rb
@@ -140,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
140
142
  version: '0'
141
143
  segments:
142
144
  - 0
143
- hash: -1981444232111291644
145
+ hash: 3676915364557125950
144
146
  required_rubygems_version: !ruby/object:Gem::Requirement
145
147
  none: false
146
148
  requirements: