activemessaging-kestrel-adapter 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ pkg
2
+ *.swp
3
+ doc
@@ -1,3 +1,11 @@
1
+ == 0.0.5 / 2011-02-27
2
+ * Clean up retry_policy initialization. Make sure the logger is set correctly.
3
+ * Avoid reinstantiating a new retry policy object each time through the
4
+ receive method. Create and reuse one object when the adapter is created.
5
+ * Make the empty_queues_delay default to nil, or no sleep at all, for cases
6
+ where the adapter is used outside of the ActiveMessaging::Gateway receive
7
+ loop
8
+
1
9
  == 0.0.4 / 2011-02-15
2
10
  * Add a queue_stats method to get stats on server queues from the adapter.
3
11
 
data/README.md CHANGED
@@ -51,11 +51,18 @@ be the config: key in the configuration file. For 99% of use cases, you
51
51
  should not even bother defining the retry_policy as the default is fine.
52
52
  The ability to override the policy is provided for edge cases.
53
53
 
54
+ empty_queues_delay is provided as a hack for when this adapter is used by the
55
+ ActiveMessaging::Gateway messaging loop. Without setting this to some
56
+ small non zero value, the messaging loop will consume 100% of CPU if there
57
+ are no messages is subscribed queues. If you are using this adapter outside
58
+ of the ActiveMessaging::Gateway messaging loop, do not set this key
59
+
54
60
  Requirements
55
61
  ------------
56
62
 
57
63
  * activemessaging >= 0.7.1
58
64
  * memcached-client
65
+ * activesupport
59
66
 
60
67
  Probably activesupport as well since activemessaging barfs if you do not
61
68
  include it at the right time for certain use cases.
data/Rakefile CHANGED
@@ -17,6 +17,7 @@ Bones {
17
17
 
18
18
  depend_on 'memcache-client'
19
19
  depend_on 'activemessaging', '>= 0.7.1'
20
+ depend_on 'activesupport', '~>2.3'
20
21
 
21
22
  rdoc.include << 'README.md'
22
23
 
@@ -27,11 +27,10 @@ module ActiveMessaging
27
27
  # If you want error logging, pass in :logger. If not provided, ActiveMessaging.logger
28
28
  # is used if defined.
29
29
  def do_work(options = {})
30
- logger = defined?(::Rails) ? ::Rails.logger : nil
31
- use_options = {:tries => 3, :delay => 5, :logger => logger}.merge(options || {})
30
+ use_options = {:tries => 3, :delay => 5}.merge(options || {})
32
31
  exception = nil
33
32
  return_value = nil
34
- logger = use_options[:logger]
33
+ logger = use_options[:logger] || (defined?(::ActiveMessaging) ? ::ActiveMessaging.logger : nil)
35
34
  use_options[:tries].times do |try|
36
35
  begin
37
36
  exception = nil
@@ -62,17 +61,26 @@ module ActiveMessaging
62
61
 
63
62
  # Create a new Kestrel adapter using the provided config
64
63
  def initialize(cfg = {})
64
+ # Like symbol keys
65
65
  cfg = symbolize_keys(cfg)
66
- # TODO: Decide on fallback logging ...
66
+
67
+ # Create a logger. Use framework loggers when available.
67
68
  @logger = cfg.delete(:logger) || ActiveMessaging.logger || (defined?(::Rails) && ::Rails.logger ? ::Rails.logger : nil) || default_logger
69
+
70
+ # Get the retry policy
68
71
  @retry_policy = cfg.delete(:retry_policy) || {:strategy => SimpleRetry, :config => {:tries => 1, :delay => 5}}
69
- @empty_queues_delay = cfg.delete(:empty_queues_delay) || 0.1
72
+ # If the retry policy came from the cfg, make sure we set the :logger
73
+ @retry_policy[:config][:logger] ||= @logger
74
+ # Turn the strategy into a Class if it is a String
70
75
  if @retry_policy[:strategy].is_a?(String)
71
76
  # Convert strategy from string to class
72
- @retry_policy[:strategy] = Kestrel.const_get(retry_policy[:strategy]) rescue Kestrel.to_class(@retry_policy[:strategy])
77
+ @retry_policy[:strategy] = Kestrel.const_get(@retry_policy[:strategy]) rescue Kestrel.to_class(@retry_policy[:strategy])
73
78
  end
79
+
80
+ @empty_queues_delay = cfg.delete(:empty_queues_delay)
74
81
  @config = cfg
75
82
  @subscriptions = {}
83
+ retrier
76
84
  connect
77
85
  nil
78
86
  end
@@ -107,10 +115,17 @@ module ActiveMessaging
107
115
 
108
116
  # Connect to the kestrel server using a Memcached client
109
117
  def connect
110
- logger.debug("Creating connection to Kestrel using config #{@config.inspect}") if logger.level <= Logger::DEBUG
118
+ logger.debug("Creating connection to Kestrel using config #{@config.inspect}") if logger && logger.level <= Logger::DEBUG
111
119
  @kestrel = MemCache.new(@config)
112
120
  @kestrel.servers = @config[:servers]
113
121
  end
122
+
123
+ # Creates a retrier object according to the @retry_policy
124
+ def retrier
125
+ @retrier ||= begin
126
+ @retry_policy[:strategy].new
127
+ end
128
+ end
114
129
 
115
130
  # Subscribe to the named destination and begin receiving
116
131
  # messages from it
@@ -146,10 +161,7 @@ module ActiveMessaging
146
161
  def receive
147
162
 
148
163
  if @subscriptions.size > 0
149
- # instantiate a class for doing the retries
150
- retrier = @retry_policy[:strategy].new
151
-
152
- retrier.do_work(@retry_policy[:config]) do
164
+ @retrier.do_work(@retry_policy[:config]) do
153
165
  queues_to_check = @subscriptions.size > 1 ? @subscriptions.keys.sort_by{rand} : @subscriptions.keys
154
166
  queues_to_check.each do |queue|
155
167
  if item = @kestrel.get(normalize(queue))
@@ -162,7 +174,7 @@ module ActiveMessaging
162
174
  end
163
175
  end
164
176
  # Sleep a little to avoid a spin loop (ActiveMessaging Gateway ought to do this)
165
- sleep(@empty_queues_delay)
177
+ sleep(@empty_queues_delay) if @empty_queues_delay && @empty_queues_delay > 0
166
178
  return nil
167
179
  end
168
180
 
@@ -7,8 +7,9 @@ class TestActiveMessagingKestrelAdapter < Test::Unit::TestCase
7
7
  def test_default_config
8
8
  cfg = {:servers => 'localhost:22133'}
9
9
  adapter = ActiveMessaging::Adapters::Kestrel::Connection.new(cfg)
10
- assert_equal adapter.instance_variable_get("@retry_policy")[:strategy], ActiveMessaging::Adapters::Kestrel::SimpleRetry
11
- assert_equal adapter.instance_variable_get("@retry_policy")[:config], {:tries => 1, :delay => 5}
10
+ assert_equal ActiveMessaging::Adapters::Kestrel::SimpleRetry, adapter.instance_variable_get("@retry_policy")[:strategy]
11
+ assert_equal 1, adapter.instance_variable_get("@retry_policy")[:config][:tries]
12
+ assert_equal 5, adapter.instance_variable_get("@retry_policy")[:config][:delay]
12
13
  end
13
14
 
14
15
  def test_config_from_yaml
@@ -24,7 +25,8 @@ development:
24
25
  YAML
25
26
  cfg = YAML.load(yaml)['development']
26
27
  adapter = ActiveMessaging::Adapters::Kestrel::Connection.new(cfg)
27
- assert_equal adapter.instance_variable_get("@retry_policy")[:strategy], ActiveMessaging::Adapters::Kestrel::SimpleRetry
28
- assert_equal adapter.instance_variable_get("@retry_policy")[:config], {:tries => 1, :delay => 5}
28
+ assert_equal ActiveMessaging::Adapters::Kestrel::SimpleRetry, adapter.instance_variable_get("@retry_policy")[:strategy]
29
+ assert_equal 1, adapter.instance_variable_get("@retry_policy")[:config][:tries]
30
+ assert_equal 5, adapter.instance_variable_get("@retry_policy")[:config][:delay]
29
31
  end
30
32
  end
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activemessaging-kestrel-adapter
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Douglas A. Seifert
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-15 00:00:00 -08:00
18
+ date: 2011-02-27 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -24,14 +24,12 @@ dependencies:
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - "="
27
+ - - ">="
28
28
  - !ruby/object:Gem::Version
29
- hash: 49
29
+ hash: 3
30
30
  segments:
31
- - 1
32
- - 8
33
- - 3
34
- version: 1.8.3
31
+ - 0
32
+ version: "0"
35
33
  type: :runtime
36
34
  version_requirements: *id001
37
35
  - !ruby/object:Gem::Dependency
@@ -51,21 +49,36 @@ dependencies:
51
49
  type: :runtime
52
50
  version_requirements: *id002
53
51
  - !ruby/object:Gem::Dependency
54
- name: bones
52
+ name: activesupport
55
53
  prerelease: false
56
54
  requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 5
60
+ segments:
61
+ - 2
62
+ - 3
63
+ version: "2.3"
64
+ type: :runtime
65
+ version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
67
+ name: bones
68
+ prerelease: false
69
+ requirement: &id004 !ruby/object:Gem::Requirement
57
70
  none: false
58
71
  requirements:
59
72
  - - ">="
60
73
  - !ruby/object:Gem::Version
61
- hash: 27
74
+ hash: 21
62
75
  segments:
63
76
  - 3
64
77
  - 6
65
- - 2
66
- version: 3.6.2
78
+ - 5
79
+ version: 3.6.5
67
80
  type: :development
68
- version_requirements: *id003
81
+ version_requirements: *id004
69
82
  description: |-
70
83
  This is an adapter to the kestrel messaging server for the ActiveMessaging framework. It is
71
84
  in the early stages of development.
@@ -75,13 +88,12 @@ executables:
75
88
  extensions: []
76
89
 
77
90
  extra_rdoc_files:
78
- - .README.md.swp
79
91
  - History.txt
80
92
  - README.md
81
93
  - bin/activemessaging-kestrel-adapter
82
94
  files:
83
- - .README.md.swp
84
95
  - .bnsignore
96
+ - .gitignore
85
97
  - History.txt
86
98
  - README.md
87
99
  - Rakefile
Binary file