surety 0.1.9 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -35,10 +35,18 @@ Configuration
35
35
 
36
36
  Both the prefix for the ActiveRecord database connection name (as specified in database.yml) and the class to which Surety delegates messages for processing after pulling them off the queue are configurable.
37
37
 
38
+ Also configurable are three parameters related to failure retries:
39
+ * retry interval: base amount of time to wait before a retry, in minutes. Defaults to 10.
40
+ * backoff factor: factor to divide the failure count by before calculating an exponential backoff. The backoff factor is (2^floor(failure count/backoff interval)). Defaults to 2.
41
+ * max backoff: maximum number of powers of 2 to backoff. Defaults to 7 (which gives a backoff factor of 128; together with the default retry interval of 10, this produces a default max backoff amount of 1280 minutes ~ 1 day).
42
+
38
43
  Example configuration (from a sample config/initializers/surety.rb file)
39
44
 
40
45
  Surety::Configuration.database_prefix = 'surety_'
41
46
  Surety::Configuration.message_processing_delegate = MessageDistributor
47
+ Surety::Configuration.retry_interval = 20
48
+ Surety::Configuration.backoff_factor = 4
49
+ Surety::Configuration.max_backoff = 5
42
50
 
43
51
 
44
52
  Install
@@ -2,7 +2,7 @@ module Surety
2
2
  module Configuration
3
3
  extend self
4
4
 
5
- attr_accessor :database_prefix, :message_processing_delegate
5
+ attr_accessor :database_prefix, :message_processing_delegate, :retry_interval, :backoff_factor, :max_backoff
6
6
 
7
7
  def database
8
8
  "#{database_prefix}#{ENV['RAILS_ENV'] || Rails.env}"
@@ -1,14 +1,9 @@
1
1
  module Surety
2
2
  module Generator
3
- extend ActiveSupport::Concern
4
-
5
- module InstanceMethods
6
-
7
- def send_message(message_content)
8
- Surety::Message.generate_message(message_content)
9
- end
10
-
3
+
4
+ def send_message(message_content)
5
+ Surety::Message.generate_message(message_content)
11
6
  end
12
-
7
+
13
8
  end
14
- end
9
+ end
@@ -9,11 +9,13 @@ module Surety
9
9
  scope :failed, where(:state => 'failed')
10
10
 
11
11
  scope :needs_processing, lambda {
12
- puts ActiveRecord::Base.default_timezone
13
- puts ActiveRecord::Base.default_timezone.to_s=='utc'
14
12
  db_time = Time.now.send(ActiveRecord::Base.default_timezone.to_s=='utc' ? :utc : :localtime)
15
- puts db_time
16
- {:conditions=>"(messages.state='unprocessed') or (messages.state='failed' and messages.failed_at<'#{(db_time-10.minutes).strftime('%Y-%m-%d %H:%M:%S')}')",
13
+ {:conditions=>"(messages.state='unprocessed') or (messages.state='failed' and messages.failed_at<('#{(db_time).strftime('%Y-%m-%d %H:%M:%S')}' -
14
+ INTERVAL (#{Surety::Configuration.retry_interval || 10}*
15
+ POW(2, if(floor(failure_count/#{Surety::Configuration.backoff_factor || 2})>#{Surety::Configuration.max_backoff || 7},
16
+ #{Surety::Configuration.max_backoff || 7},
17
+ floor(failure_count/#{Surety::Configuration.backoff_factor || 2}))))
18
+ MINUTE))",
17
19
  :order => :created_at, :limit=>1}
18
20
  }
19
21
 
data/surety.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'surety'
3
- s.version = '0.1.9'
3
+ s.version = '0.1.10'
4
4
  s.date = Time.now.strftime('%Y-%m-%d')
5
5
  s.summary = 'A lightweight guaranteed-delivery messaging system.'
6
6
  s.homepage = ''
@@ -0,0 +1,50 @@
1
+ require File.expand_path('test_helper.rb', File.dirname(__FILE__))
2
+
3
+ class MessageBackoffTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ Surety::Configuration.database_prefix = 'surety_'
7
+ Surety::Message.new.connection.execute("truncate table messages")
8
+ @message = Surety::Message.generate_message('Testing message')
9
+ @message.begin_processing!
10
+ @message.fail_processing!
11
+ end
12
+
13
+ def test_backoff_with_one_failure
14
+ # Before interval
15
+ message = Surety::Message.needs_processing.first
16
+ assert message.nil?
17
+ # After interval
18
+ @message.failed_at = Time.now-12.minutes
19
+ @message.save
20
+ message = Surety::Message.needs_processing.first
21
+ assert message.present?
22
+ end
23
+
24
+ def test_backoff_with_backoff_factor_failures
25
+ @message.failed_at = Time.now-12.minutes
26
+ @message.failure_count = 2
27
+ # Before interval
28
+ message = Surety::Message.needs_processing.first
29
+ assert message.nil?
30
+ # After interval
31
+ @message.failed_at = Time.now-21.minutes
32
+ @message.save
33
+ message = Surety::Message.needs_processing.first
34
+ assert message.present?
35
+ end
36
+
37
+ def test_max_backoff
38
+ @message.failed_at = Time.now-1277.minutes
39
+ @message.failure_count = 20
40
+ # Before interval
41
+ message = Surety::Message.needs_processing.first
42
+ assert message.nil?
43
+ # After interval
44
+ @message.failed_at = Time.now-1285.minutes
45
+ @message.save
46
+ message = Surety::Message.needs_processing.first
47
+ assert message.present?
48
+ end
49
+
50
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: surety
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 9
10
- version: 0.1.9
9
+ - 10
10
+ version: 0.1.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - Monica McArthur
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-26 00:00:00 Z
18
+ date: 2012-01-30 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: yajl-ruby
@@ -138,6 +138,7 @@ files:
138
138
  - test/fixtures/test_delegate.rb
139
139
  - test/fixtures/test_generator.rb
140
140
  - test/generator_test.rb
141
+ - test/message_backoff_test.rb
141
142
  - test/message_test.rb
142
143
  - test/message_timezone_test.rb
143
144
  - test/processor_test.rb
@@ -183,6 +184,7 @@ test_files:
183
184
  - test/fixtures/test_delegate.rb
184
185
  - test/fixtures/test_generator.rb
185
186
  - test/generator_test.rb
187
+ - test/message_backoff_test.rb
186
188
  - test/message_test.rb
187
189
  - test/message_timezone_test.rb
188
190
  - test/processor_test.rb