rabbitmq-receiver 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +62 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/rabbitmq/receiver/config.rb +26 -0
- data/lib/rabbitmq/receiver/exception.rb +11 -0
- data/lib/rabbitmq/receiver/rabbit_consumer.rb +93 -0
- data/lib/rabbitmq/receiver/receiver.rb +40 -0
- data/lib/rabbitmq/receiver/version.rb +12 -0
- data/lib/rabbitmq/receiver.rb +20 -0
- data/rabbitmq-receiver.gemspec +37 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d647e6f17a960acc00f66b4f2b930b818f7d2d9c
|
4
|
+
data.tar.gz: afff7c64f8ff8e5e7cb0994c207dc3c740f08357
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 217c02828bc135515965035c69438a2d42b903b8e5af0881d9cb163b0940b723ffd13b55223cc6a189a56095fba0015bae546a81b5f916f8a93c0c6c87807bc1
|
7
|
+
data.tar.gz: c1a52a4520adc1cd764f232d617b4868137739db1441528489d630e43c1365fe536361b23ad26a351a7e1739f5fa95077d75800aaeeed34b1c07596a13aea6d3
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Gaëtan JUVIN
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# Rabbitmq::Receiver
|
2
|
+
|
3
|
+
Rabbitmq ruby receiver
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require 'rabbitmq/receiver'
|
9
|
+
|
10
|
+
Rabbitmq::Receiver.config do |config|
|
11
|
+
config.host = '8.8.8.8'
|
12
|
+
config.user = 'superuser'
|
13
|
+
config.password = 'superpassword'
|
14
|
+
config.vhost = 'supervhost'
|
15
|
+
config.verbose = true
|
16
|
+
end
|
17
|
+
|
18
|
+
Rabbitmq::Receiver.start do |package|
|
19
|
+
package = Package.unpack(package)
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
## Configuration - defaults
|
24
|
+
```
|
25
|
+
logger: Logger.new(STDOUT)
|
26
|
+
host: '8.8.8.8'
|
27
|
+
port: 5672
|
28
|
+
user: nil
|
29
|
+
password: nil
|
30
|
+
queue: nil
|
31
|
+
vhost: '/'
|
32
|
+
verbose: false
|
33
|
+
prefetch: 100
|
34
|
+
heartbeat: 5
|
35
|
+
|
36
|
+
```
|
37
|
+
|
38
|
+
## Installation
|
39
|
+
|
40
|
+
Add this line to your application's Gemfile:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
gem 'rabbitmq-receiver'
|
44
|
+
```
|
45
|
+
|
46
|
+
And then execute:
|
47
|
+
|
48
|
+
$ bundle
|
49
|
+
|
50
|
+
Or install it yourself as:
|
51
|
+
|
52
|
+
$ gem install rabbitmq-receiver
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rabbitmq-receiver. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
57
|
+
|
58
|
+
|
59
|
+
## License
|
60
|
+
|
61
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
62
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rabbitmq/receiver"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
##
|
4
|
+
## config.rb
|
5
|
+
## Gaetan JUVIN 27/07/2015
|
6
|
+
##
|
7
|
+
|
8
|
+
require 'gconfig'
|
9
|
+
|
10
|
+
module Rabbitmq
|
11
|
+
class Receiver
|
12
|
+
extend GConfig
|
13
|
+
|
14
|
+
default logger: Logger.new(STDOUT)
|
15
|
+
default host: '8.8.8.8'
|
16
|
+
default port: 5672
|
17
|
+
default user: nil
|
18
|
+
default password: nil
|
19
|
+
default queue: nil
|
20
|
+
default vhost: '/'
|
21
|
+
default verbose: false
|
22
|
+
default prefetch: 100
|
23
|
+
default heartbeat: 5
|
24
|
+
default heartbeat: 5
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
##
|
4
|
+
## rabbit_consumer.rb
|
5
|
+
## Gaetan JUVIN 27/07/2015
|
6
|
+
##
|
7
|
+
|
8
|
+
require 'singleton_from'
|
9
|
+
require 'bunny'
|
10
|
+
|
11
|
+
module Rabbitmq
|
12
|
+
class Receiver
|
13
|
+
class RabbitConsumer
|
14
|
+
singleton_from :start
|
15
|
+
|
16
|
+
attr_accessor :action, :options
|
17
|
+
attr_accessor :_state
|
18
|
+
attr_accessor :rmq_connection, :rmq_channel, :rmq_queue
|
19
|
+
|
20
|
+
def start(options, action)
|
21
|
+
@options = options
|
22
|
+
@action = action
|
23
|
+
|
24
|
+
@_state = :initialized
|
25
|
+
self.print
|
26
|
+
self.run
|
27
|
+
end
|
28
|
+
|
29
|
+
def state
|
30
|
+
@_state.to_s.capitalize
|
31
|
+
end
|
32
|
+
|
33
|
+
def print
|
34
|
+
if @options[:verbose]
|
35
|
+
@options[:logger].info "------- Consumer #{self.__id__} on #{@options[:host]}:#{@options[:port]} -- #{self.state.upcase}".white
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def connect
|
40
|
+
@_state = :connecting
|
41
|
+
self.print
|
42
|
+
sleep_duration = 10
|
43
|
+
loop do
|
44
|
+
begin
|
45
|
+
@rmq_connection = Bunny.new(host: @options[:host],
|
46
|
+
port: @options[:port],
|
47
|
+
vhost: @options[:vhost],
|
48
|
+
user: @options[:user],
|
49
|
+
password: @options[:password],
|
50
|
+
heartbeat: 5)
|
51
|
+
@rmq_connection.start
|
52
|
+
|
53
|
+
@rmq_channel = @rmq_connection.create_channel
|
54
|
+
@rmq_channel.prefetch(@options[:prefetch])
|
55
|
+
@rmq_queue = @rmq_channel.queue(@options[:queue], durable: true, exlusive: false)
|
56
|
+
rescue Exception => error
|
57
|
+
@options[:logger].error "Error: #{error.message}".red
|
58
|
+
sleep(sleep_duration)
|
59
|
+
else
|
60
|
+
@_state = :connected
|
61
|
+
break
|
62
|
+
end
|
63
|
+
sleep_duration = [300, sleep_duration + 20].max
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def run
|
68
|
+
self.connect
|
69
|
+
self.print
|
70
|
+
if @_state == :connected
|
71
|
+
@rmq_queue.subscribe(manual_ack: true, block: true) do |delivery_info, properties, body|
|
72
|
+
@options[:logger].info "Receive package.".light_yellow if @options[:verbose]
|
73
|
+
next unless delivery_info
|
74
|
+
begin
|
75
|
+
@action.call(body, delivery_info, properties, self)
|
76
|
+
@rmq_channel.ack(delivery_info.delivery_tag)
|
77
|
+
rescue RejectPackage => error
|
78
|
+
@options[:logger].debug "Exception RejectPackage".yellow
|
79
|
+
@rmq_channel.reject(delivery_info.delivery_tag, true)
|
80
|
+
rescue Bunny::ChannelAlreadyClosed => error
|
81
|
+
@options[:logger].error "Exception ChannelAlreadyClosed".red
|
82
|
+
raise error if error.class == Bunny::ChannelAlreadyClosed
|
83
|
+
rescue Exception => error
|
84
|
+
@options[:logger].error "Error: " + error.message
|
85
|
+
@options[:logger].error error.backtrace
|
86
|
+
@rmq_channel.reject(delivery_info.delivery_tag, true)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
##
|
4
|
+
## receiver.rb
|
5
|
+
## Gaetan JUVIN 27/07/2015
|
6
|
+
##
|
7
|
+
|
8
|
+
require 'gconfig'
|
9
|
+
|
10
|
+
module Rabbitmq
|
11
|
+
class Receiver
|
12
|
+
attr_accessor :running, :options, :action
|
13
|
+
|
14
|
+
def run
|
15
|
+
@running = true
|
16
|
+
|
17
|
+
while @running
|
18
|
+
begin
|
19
|
+
@options[:logger].info "New instance" if @options[:verbose]
|
20
|
+
rabbit_consumer = RabbitConsumer.start(@options, @action)
|
21
|
+
@options[:logger].info "Instance finish" if @options[:verbose]
|
22
|
+
rescue Exception => error
|
23
|
+
@options[:logger].error "Error: " + error.message
|
24
|
+
@options[:logger].error error.backtrace
|
25
|
+
end
|
26
|
+
sleep 30
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def start(options = {}, &block)
|
31
|
+
@options = Rabbitmq::Receiver.config.to_hash.merge(options)
|
32
|
+
@running = false
|
33
|
+
@action = block
|
34
|
+
|
35
|
+
@options[:logger].info "------- Runner on #{@options[:host]} starting.".white
|
36
|
+
|
37
|
+
self.run
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
##
|
4
|
+
## receiver.rb
|
5
|
+
## Gaetan JUVIN 27/07/2015
|
6
|
+
##
|
7
|
+
|
8
|
+
require 'rabbitmq/receiver/config'
|
9
|
+
require 'rabbitmq/receiver/exception'
|
10
|
+
require 'rabbitmq/receiver/rabbit_consumer'
|
11
|
+
require 'rabbitmq/receiver/receiver'
|
12
|
+
require 'rabbitmq/receiver/version'
|
13
|
+
|
14
|
+
require 'singleton_from'
|
15
|
+
|
16
|
+
module Rabbitmq
|
17
|
+
class Receiver
|
18
|
+
singleton_from :start
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
##
|
4
|
+
## rabbitmq-receiver.gemspec
|
5
|
+
## Gaetan JUVIN 27/07/2015
|
6
|
+
##
|
7
|
+
|
8
|
+
lib = File.expand_path('../lib', __FILE__)
|
9
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
10
|
+
require 'rabbitmq/receiver/version'
|
11
|
+
|
12
|
+
Gem::Specification.new do |spec|
|
13
|
+
spec.name = "rabbitmq-receiver"
|
14
|
+
spec.version = Rabbitmq::Receiver::VERSION
|
15
|
+
spec.authors = ["Gaëtan JUVIN"]
|
16
|
+
spec.email = ["gaetanjuvin@gmail.com"]
|
17
|
+
|
18
|
+
spec.summary = %q{Rabbitmq ruby receiver}
|
19
|
+
spec.homepage = 'https://github.com/GaetanJUVIN/rabbitmq-ruby-sender'
|
20
|
+
spec.license = 'MIT'
|
21
|
+
|
22
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
23
|
+
# delete this section to allow pushing this gem to any host.
|
24
|
+
if spec.respond_to?(:metadata)
|
25
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
26
|
+
else
|
27
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
28
|
+
end
|
29
|
+
|
30
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
34
|
+
spec.add_dependency 'bunny', '~> 0'
|
35
|
+
spec.add_dependency 'gconfig', '~> 0'
|
36
|
+
spec.add_dependency 'singleton_from', '~> 0'
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rabbitmq-receiver
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gaëtan JUVIN
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bunny
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: gconfig
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: singleton_from
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- gaetanjuvin@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- lib/rabbitmq/receiver.rb
|
85
|
+
- lib/rabbitmq/receiver/config.rb
|
86
|
+
- lib/rabbitmq/receiver/exception.rb
|
87
|
+
- lib/rabbitmq/receiver/rabbit_consumer.rb
|
88
|
+
- lib/rabbitmq/receiver/receiver.rb
|
89
|
+
- lib/rabbitmq/receiver/version.rb
|
90
|
+
- rabbitmq-receiver.gemspec
|
91
|
+
homepage: https://github.com/GaetanJUVIN/rabbitmq-ruby-sender
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata:
|
95
|
+
allowed_push_host: https://rubygems.org
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.2.2
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Rabbitmq ruby receiver
|
116
|
+
test_files: []
|