amqp_helpers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ Gemfile.lock
30
+ .ruby-version
31
+ #.ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ amqp-helpers
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 nine.ch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # amqp_helpers [![Build Status](https://travis-ci.org/ninech/amqp_helpers.svg)](https://travis-ci.org/ninech/amqp_helpers)
2
+
3
+ Simple utilities to achieve various AMQP tasks.
4
+
5
+ ## Daemon
6
+
7
+ `AMQPHelpers::Daemon` allows you to build a simple message consumer.
8
+
9
+ ### Example
10
+
11
+ ``` ruby
12
+ AMQPHelpers::Daemon.new({
13
+ name: 'nba-scores-daemon',
14
+ environment: 'production',
15
+ logger: PXEConfGen.logger,
16
+ connection_params: {
17
+ host: 'localhost',
18
+ port: 5672
19
+ },
20
+ queue_params: { durable: false },
21
+ exchanges: {
22
+ 'nba.scores': {
23
+ params: {
24
+ type: :topic,
25
+ durable: false
26
+ },
27
+ bindings: [
28
+ { routing_key: 'north.#' },
29
+ { routing_key: 'south.#' }
30
+ ]
31
+ }
32
+ }
33
+ }).start do |delivery_info, payload|
34
+ puts "AMQP Incoming message #{payload}"
35
+ end
36
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler'
3
+ require 'rake'
4
+ require 'bundler/gem_tasks'
5
+ require 'rspec/core/rake_task'
6
+
7
+ task :default => :spec
8
+
9
+ desc 'Run all specs'
10
+ RSpec::Core::RakeTask.new(:spec) do |spec|
11
+ spec.pattern = FileList['spec/**/*_spec.rb']
12
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'amqp_helpers'
3
+ s.version = File.read(File.expand_path('../VERSION', __FILE__)).strip
4
+ s.authors = ['Nils Caspar', 'Raffael Schmid', 'Samuel Sieg']
5
+ s.email = 'development@nine.ch'
6
+ s.homepage = 'http://nine.ch/'
7
+ s.license = 'MIT'
8
+ s.summary = 'Simple helpers to achieve various AMQP tasks.'
9
+ s.description = s.summary
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
+ s.require_paths = ['lib']
15
+
16
+ s.add_development_dependency 'rspec', '~> 2.14'
17
+ s.add_development_dependency 'rake', '~> 10.3'
18
+
19
+ s.add_runtime_dependency 'amqp', '~> 1.3'
20
+ s.add_runtime_dependency 'syslogger', '~> 1.5'
21
+ end
@@ -0,0 +1,108 @@
1
+ require 'amqp'
2
+ require 'logger'
3
+ require 'socket'
4
+ require 'syslogger'
5
+
6
+ module AMQPHelpers
7
+ class Daemon
8
+
9
+ class Error < StandardError; end
10
+ class ConnectionError < Error; end
11
+ class ChannelError < Error; end
12
+
13
+ DEFAULT_RECONNECT_WAIT_TIME = 30
14
+
15
+ attr_accessor :name, :exchanges, :connection_params
16
+ attr_writer :environment, :logger, :queue_name, :queue_params, :reconnect_wait_time
17
+
18
+ def initialize(config)
19
+ config.each do |key, value|
20
+ setter = "#{key}="
21
+ if respond_to?(setter)
22
+ send(setter, value)
23
+ end
24
+ end
25
+ end
26
+
27
+ def start(&handler)
28
+ logger.info "Starting #{name} daemon..."
29
+ AMQP.start(connection_params) do |connection|
30
+ connection.on_error(&method(:handle_connection_error))
31
+ channel = initialize_channel(connection)
32
+ connection.on_tcp_connection_loss(&method(:handle_tcp_connection_loss))
33
+
34
+ queue = initialize_queue(channel)
35
+ queue.subscribe(&handler)
36
+
37
+ show_stopper = Proc.new do
38
+ logger.info "Signal INT received. #{name} is going down... I REPEAT: WE ARE GOING DOWN!"
39
+ connection.close { EventMachine.stop }
40
+ end
41
+ Signal.trap 'INT', show_stopper
42
+ end
43
+ end
44
+
45
+ def environment
46
+ @environment ||= 'development'
47
+ end
48
+
49
+ def queue_params
50
+ @queue_params ||= {}
51
+ end
52
+
53
+ def queue_name
54
+ @queue_name ||= "#{Socket.gethostname}.#{name}"
55
+ end
56
+
57
+ def logger
58
+ @logger ||= if environment == 'development'
59
+ Logger.new(STDOUT)
60
+ else
61
+ Syslogger.new(name, Syslog::LOG_PID, Syslog::LOG_LOCAL0)
62
+ end
63
+ end
64
+
65
+ def reconnect_wait_time
66
+ @reconnect_wait_time ||= DEFAULT_RECONNECT_WAIT_TIME
67
+ end
68
+
69
+ protected
70
+ def handle_connection_error(connection, connection_close)
71
+ logger.error "[connection.close] Reply code = #{connection_close.reply_code}, reply text = #{connection_close.reply_text}"
72
+ # check if graceful broker shutdown
73
+ if connection_close.reply_code == 320
74
+ logger.info "[connection.close] Setting up a periodic reconnection timer (#{reconnect_wait_time}s)..."
75
+ connection.periodically_reconnect(reconnect_wait_time)
76
+ else
77
+ raise ConnectionError, connection_close.reply_text
78
+ end
79
+ end
80
+
81
+ def handle_channel_error(channel, channel_close)
82
+ logger.error "[channel.close] Reply code = #{channel_close.reply_code}, reply text = #{channel_close.reply_text}"
83
+ raise ChannelError, channel_close.reply_text
84
+ end
85
+
86
+ def handle_tcp_connection_loss(connection, settings)
87
+ logger.error '[network failure] Trying to reconnect...'
88
+ connection.reconnect(false, 10)
89
+ end
90
+
91
+ def initialize_channel(connection)
92
+ channel = AMQP::Channel.new(connection)
93
+ channel.auto_recovery = true
94
+ channel.on_error(&method(:handle_channel_error))
95
+ end
96
+
97
+ def initialize_queue(channel)
98
+ channel.queue(queue_name, queue_params).tap do |queue|
99
+ exchanges.each do |exchange_name, exchange_config|
100
+ exchange = channel.topic(exchange_name, exchange_config[:params])
101
+ (exchange_config[:bindings] || []).each do |params|
102
+ queue.bind(exchange, params)
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,2 @@
1
+ module AMQPHelpers
2
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ require 'amqp_helpers/daemon'
4
+
5
+ describe AMQPHelpers::Daemon do
6
+ describe '#start' do
7
+ subject { described_class.new }
8
+
9
+ it 'starts the AMQP loop with the given config' do
10
+ AMQP.should_receive(:start).with instance_of(Hash)
11
+ subject.start
12
+ end
13
+
14
+ let(:config) {
15
+ {
16
+ name: 'lala',
17
+ queue_name: 'example-queue',
18
+ queue_params: { durable: false },
19
+ logger: Logger.new('/dev/null'),
20
+ connection_params: { host: 'localhost', port: 1337 },
21
+ exchanges: {
22
+ 'test-topic' => {
23
+ params: { type: :topic, durable: false },
24
+ bindings: [ { routing_key: 'lala.#' } ]
25
+ }
26
+ }
27
+ }
28
+ }
29
+ let(:block) { @block }
30
+ let(:connection) { double(AMQP) }
31
+ let(:exchange) { Object.new }
32
+ let(:queue) { double(AMQP::Queue) }
33
+
34
+ subject { described_class.new(config) }
35
+
36
+ before(:each) do
37
+ AMQP.stub(:start) do |&block|
38
+ @block = block
39
+ end
40
+ subject.start { |a, b| nil }
41
+ end
42
+
43
+ before(:each) do
44
+ EM.stub(:reactor_running?).and_return(true)
45
+ connection.stub(:auto_recovering?).and_return(true)
46
+ connection.stub(:open?).and_return(true)
47
+ connection.stub(:channel_max).and_return(0)
48
+ connection.stub(:on_connection)
49
+ connection.stub(:on_error)
50
+ connection.stub(:on_tcp_connection_loss)
51
+ connection.stub(:next_channel_id).and_return(0)
52
+ AMQP::Channel.any_instance.stub(:topic).and_return(exchange)
53
+ AMQP::Channel.any_instance.stub(:queue).and_return(queue)
54
+ queue.stub(:bind)
55
+ queue.stub(:subscribe)
56
+ end
57
+
58
+ it 'creates a non-durable queue' do
59
+ AMQP::Channel.any_instance.should_receive(:queue).with('example-queue', durable: false).and_return(queue)
60
+ block.call(connection)
61
+ end
62
+
63
+ it 'creates the topic' do
64
+ AMQP::Channel.any_instance.should_receive(:topic).with('test-topic', type: :topic, durable: false).and_return(exchange)
65
+ block.call(connection)
66
+ end
67
+
68
+ it 'binds the queue to the exchange' do
69
+ queue.should_receive(:bind).with(exchange, routing_key: 'lala.#')
70
+ block.call(connection)
71
+ end
72
+
73
+ it 'subscribes' do
74
+ queue.should_receive(:subscribe)
75
+ block.call(connection)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amqp_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nils Caspar
9
+ - Raffael Schmid
10
+ - Samuel Sieg
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2014-05-23 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ requirement: !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: '2.14'
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ version: '2.14'
32
+ - !ruby/object:Gem::Dependency
33
+ name: rake
34
+ requirement: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ~>
38
+ - !ruby/object:Gem::Version
39
+ version: '10.3'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10.3'
48
+ - !ruby/object:Gem::Dependency
49
+ name: amqp
50
+ requirement: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: '1.3'
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ~>
62
+ - !ruby/object:Gem::Version
63
+ version: '1.3'
64
+ - !ruby/object:Gem::Dependency
65
+ name: syslogger
66
+ requirement: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ~>
70
+ - !ruby/object:Gem::Version
71
+ version: '1.5'
72
+ type: :runtime
73
+ prerelease: false
74
+ version_requirements: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ~>
78
+ - !ruby/object:Gem::Version
79
+ version: '1.5'
80
+ description: Simple helpers to achieve various AMQP tasks.
81
+ email: development@nine.ch
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - .rspec
88
+ - .ruby-gemset
89
+ - .travis.yml
90
+ - Gemfile
91
+ - LICENSE
92
+ - README.md
93
+ - Rakefile
94
+ - VERSION
95
+ - amqp_helpers.gemspec
96
+ - lib/amqp_helpers.rb
97
+ - lib/amqp_helpers/daemon.rb
98
+ - spec/daemon_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: http://nine.ch/
101
+ licenses:
102
+ - MIT
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.23
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Simple helpers to achieve various AMQP tasks.
125
+ test_files:
126
+ - spec/daemon_spec.rb
127
+ - spec/spec_helper.rb