stapfen 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -3,9 +3,9 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in stapfen.gemspec
4
4
  gemspec
5
5
 
6
+
6
7
  group :development do
7
8
  gem 'rake'
8
9
  gem 'rspec', '~> 2.11'
9
10
  gem 'guard-rspec'
10
- gem 'yard'
11
11
  end
@@ -0,0 +1,48 @@
1
+
2
+ module Stapfen
3
+ # Logging module to ensure that {{Stapfen::Worker}} classes can perform
4
+ # logging if they've been configured to
5
+ module Logger
6
+ # Collection of methods to pass arguments through from the class and
7
+ # instance level to a configured logger
8
+ PROXY_METHODS = [:info, :debug, :warn, :error].freeze
9
+
10
+ module ClassMethods
11
+ PROXY_METHODS.each do |method|
12
+ define_method(method) do |*args|
13
+ proxy_log_method(method, args)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def proxy_log_method(method, arguments)
20
+ if self.logger
21
+ self.logger.send(method, *arguments)
22
+ return true
23
+ end
24
+ return false
25
+ end
26
+ end
27
+
28
+ def self.included(klass)
29
+ klass.extend(ClassMethods)
30
+ end
31
+
32
+ PROXY_METHODS.each do |method|
33
+ define_method(method) do |*args|
34
+ proxy_log_method(method, args)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def proxy_log_method(method, arguments)
41
+ if self.class.logger
42
+ self.class.logger.send(method, *arguments)
43
+ return true
44
+ end
45
+ return false
46
+ end
47
+ end
48
+ end
@@ -1,3 +1,3 @@
1
1
  module Stapfen
2
- VERSION = "1.1.#{ENV['BUILD_NUMBER'] || 'dev'}"
2
+ VERSION = '1.2.0'
3
3
  end
@@ -1,16 +1,22 @@
1
- require 'thread'
2
- require 'onstomp'
1
+ require 'stomp'
2
+ require 'stapfen/logger'
3
3
 
4
4
  module Stapfen
5
5
  class Worker
6
+ include Stapfen::Logger
7
+
6
8
  class << self
7
- attr_accessor :configuration, :consumers
9
+ attr_accessor :configuration, :consumers, :logger
8
10
  end
9
11
 
12
+ # Instantiate a new +Worker+ instance and run it
10
13
  def self.run!
11
14
  self.new.run
12
15
  end
13
16
 
17
+ # Expects a block to be passed which will yield the appropriate
18
+ # configuration for the Stomp gem. Whatever the block yields will be passed
19
+ # directly into the {{Stomp::Client#new}} method
14
20
  def self.configure
15
21
  unless block_given?
16
22
  raise Stapfen::ConfigurationError
@@ -18,6 +24,14 @@ module Stapfen
18
24
  @configuration = yield
19
25
  end
20
26
 
27
+ # Optional method, should be passed a block which will yield a {{Logger}}
28
+ # instance for the Stapfen worker to use
29
+ def self.log
30
+ @logger = yield
31
+ end
32
+
33
+
34
+ # Main message consumption block
21
35
  def self.consume(queue_name, headers={}, &block)
22
36
  unless block_given?
23
37
  raise Stapfen::ConsumeError, "Cannot consume #{queue_name} without a block!"
@@ -32,12 +46,11 @@ module Stapfen
32
46
  handle_signals!
33
47
  end
34
48
 
35
-
36
49
  def run
37
- @client = OnStomp::Client.new(generate_uri)
38
- @client.connect
50
+ @client = Stomp::Client.new(self.class.configuration)
39
51
 
40
52
  self.class.consumers.each do |name, headers, block|
53
+
41
54
  # We're taking each block and turning it into a method so that we can
42
55
  # use the instance scope instead of the blocks originally bound scope
43
56
  # which would be at a class level
@@ -45,7 +58,6 @@ module Stapfen
45
58
  self.class.send(:define_method, method_name, &block)
46
59
 
47
60
  client.subscribe(name, headers) do |message|
48
- puts "invoking #{method_name} for #{message.inspect}"
49
61
  self.send(method_name, message)
50
62
  end
51
63
  end
@@ -54,8 +66,8 @@ module Stapfen
54
66
  # Performing this join/open loop to make sure that we don't
55
67
  # experience potential deadlocks between signal handlers who might
56
68
  # close the connection, and an infinite Client#join call
57
- while client.connected? do
58
- sleep(0.2)
69
+ while client.open? do
70
+ client.join(1)
59
71
  end
60
72
  rescue Interrupt
61
73
  exit_cleanly
@@ -73,25 +85,9 @@ module Stapfen
73
85
  end
74
86
 
75
87
  def exit_cleanly
76
- if client.connected?
77
- client.disconnect
88
+ unless client.closed?
89
+ client.close
78
90
  end
79
91
  end
80
-
81
- # Convert Stapfen configuration into an OnStomp URL
82
- #
83
- # @return [String]
84
- def generate_uri
85
- config = self.class.configuration
86
- raise Stapfen::ConfigurationError if config.nil? || config.empty?
87
-
88
- user_info = nil
89
-
90
- if config[:login] && config[:passcode]
91
- user_info = "#{config[:login]}:#{config[:passcode]}@"
92
- end
93
-
94
- "stomp://#{user_info}#{config[:host]}:#{config[:port]}"
95
- end
96
92
  end
97
93
  end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Stapfen::Logger do
4
+ let(:mixin) do
5
+ Class.new do
6
+ include Stapfen::Logger
7
+ end
8
+ end
9
+
10
+ subject(:logger) { mixin.new }
11
+
12
+ context 'instance methods' do
13
+ it { should respond_to :info }
14
+ it { should respond_to :debug }
15
+ it { should respond_to :warn }
16
+ it { should respond_to :error }
17
+
18
+ context 'without an initialized logger' do
19
+ before :each do
20
+ logger.class.stub(:logger)
21
+ end
22
+
23
+ it 'should discard info messages' do
24
+ expect(logger.info('rspec')).to be_false
25
+ end
26
+ end
27
+
28
+ context 'with an initialized logger' do
29
+ let(:plogger) { double('RSpec Logger') }
30
+
31
+ before :each do
32
+ logger.class.stub(:logger).and_return(plogger)
33
+ end
34
+
35
+ it 'should pass info messages along' do
36
+ plogger.should_receive(:info)
37
+
38
+ expect(logger.info('rspec')).to be_true
39
+ end
40
+ end
41
+ end
42
+ end
data/spec/worker_spec.rb CHANGED
@@ -10,6 +10,7 @@ describe Stapfen::Worker do
10
10
  it { should respond_to :run! }
11
11
  it { should respond_to :configure }
12
12
  it { should respond_to :consume }
13
+ it { should respond_to :logger }
13
14
 
14
15
  describe '#configure' do
15
16
  it 'should error when not passed a block' do
@@ -53,38 +54,4 @@ describe Stapfen::Worker do
53
54
  end
54
55
  end
55
56
 
56
- context 'instance methods' do
57
- describe '#generate_uri' do
58
- subject(:uri) { worker.generate_uri }
59
- let(:conf) { {} }
60
-
61
- before :each do
62
- worker.class.stub(:configuration).and_return(conf)
63
- end
64
-
65
- context 'with a blank configuration' do
66
- it 'should raise an error' do
67
- expect { worker.generate_uri }.to raise_error(Stapfen::ConfigurationError)
68
- end
69
- end
70
-
71
- context 'with an unauthenticated non-ssl host' do
72
- let(:conf) { {:host => 'localhost', :port => 61613} }
73
-
74
- it { should eql('stomp://localhost:61613') }
75
- end
76
-
77
- context 'with an authentication ssl host' do
78
- let(:conf) do
79
- {:host => 'localhost',
80
- :port => 61613,
81
- :login => 'admin',
82
- :passcode => 'admin'}
83
- end
84
-
85
- it { should eql('stomp://admin:admin@localhost:61613') }
86
- end
87
- end
88
-
89
- end
90
57
  end
data/stapfen.gemspec CHANGED
@@ -18,5 +18,5 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
 
21
- gem.add_dependency('onstomp', '>= 1.0.7')
21
+ gem.add_dependency('stomp', '>= 1.2.8')
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stapfen
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,16 +9,16 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-27 00:00:00.000000000 Z
12
+ date: 2013-07-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: onstomp
15
+ name: stomp
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 1.0.7
21
+ version: 1.2.8
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 1.0.7
29
+ version: 1.2.8
30
30
  description: A simple gem for writing good basic STOMP workers
31
31
  email:
32
32
  - rtyler.croy@lookout.com
@@ -42,8 +42,10 @@ files:
42
42
  - Rakefile
43
43
  - examples/simple.rb
44
44
  - lib/stapfen.rb
45
+ - lib/stapfen/logger.rb
45
46
  - lib/stapfen/version.rb
46
47
  - lib/stapfen/worker.rb
48
+ - spec/logger_spec.rb
47
49
  - spec/spec_helper.rb
48
50
  - spec/worker_spec.rb
49
51
  - stapfen.gemspec
@@ -61,7 +63,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
63
  version: '0'
62
64
  segments:
63
65
  - 0
64
- hash: 4532487508502136780
66
+ hash: -644536034951123366
65
67
  required_rubygems_version: !ruby/object:Gem::Requirement
66
68
  none: false
67
69
  requirements:
@@ -70,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
72
  version: '0'
71
73
  segments:
72
74
  - 0
73
- hash: 4532487508502136780
75
+ hash: -644536034951123366
74
76
  requirements: []
75
77
  rubyforge_project:
76
78
  rubygems_version: 1.8.25
@@ -78,6 +80,6 @@ signing_key:
78
80
  specification_version: 3
79
81
  summary: A simple gem for writing good basic STOMP workers
80
82
  test_files:
83
+ - spec/logger_spec.rb
81
84
  - spec/spec_helper.rb
82
85
  - spec/worker_spec.rb
83
- has_rdoc: