gorg_service 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fc37bb278653aa7e8ba4d3f121a49859bbf0160c
4
+ data.tar.gz: c2dcce39d7c97c510cf012bd3f20a0a2351562fc
5
+ SHA512:
6
+ metadata.gz: 3c8fec6cf54bc2a14bfab458a97a22c90bf60ddb4b60959a8d4b324cbf8c145e83c506b86b4e9959ce22d52679d6b927f500407cef1e54779a20fdf1a631dcfb
7
+ data.tar.gz: 2e0f7391a8298dea96bc0b370b0d9e29272ffddf03a93a93649c4667ec01a0eb2a0840c27e6cb9dd01a7b40f78c9290258bd27041c8a8c1abad3c349651e4294
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /build/
11
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.11.2
5
+ addons:
6
+ code_climate:
7
+ repo_token: d01c5d7da4ec77cd04397e7fa442db957c5b8028a7a68604a4abcb6c6fe42e45
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ gem "codeclimate-test-reporter", group: :test, require: nil
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Alexandre Narbonne
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,74 @@
1
+ # GorgService
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'gorg_service'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install gorg_service
18
+
19
+ ## Setup
20
+
21
+ Before being used GramV1Client must be configured. In Rails app, put it in an Initializer.
22
+
23
+ ```ruby
24
+ GorgService.configure do |c|
25
+ # application name for display usage
26
+ c.application_name="My Application Name"
27
+ # application id used to find message from this producer
28
+ c.application_id="my_app_id"
29
+
30
+ ## RabbitMQ configuration
31
+ #
32
+ ### Authentification
33
+ # If your RabbitMQ server is password protected put it here
34
+ #
35
+ # c.rabbitmq_user = nil
36
+ # c.rabbitmq_password = nil
37
+ #
38
+ ### Network configuration :
39
+ #
40
+ # c.rabbitmq_host = "localhost"
41
+ # c.rabbitmq_port = 5672
42
+ #
43
+ #
44
+ # c.rabbitmq_queue_name = c.application_name
45
+ # c.rabbitmq_exchange_name = "exchange"
46
+ #
47
+ # time before trying again on softfail (temporary error)
48
+ # c.rabbitmq_deferred_time = 1800000 # 30min
49
+ #
50
+ # maximum number of try before discard a message
51
+ # c.rabbitmq_max_attempts = 48 # 24h with default deferring delay
52
+
53
+ end
54
+ ```
55
+
56
+ ## Usage
57
+
58
+ TODO: Write usage instructions here
59
+
60
+ ## Development
61
+
62
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
63
+
64
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Zooip/gorg_service.
69
+
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
74
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gorg_service"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gorg_service/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gorg_service"
8
+ spec.version = GorgService::VERSION
9
+ spec.authors = ["Alexandre Narbonne"]
10
+ spec.email = ["alexandre.narbonne@gadz.org"]
11
+
12
+ spec.summary = "Standard RabbitMQ bot used in Gadz.org SOA"
13
+ spec.homepage = "https://github.com/Zooip/gorg_service"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency 'bunny', '~> 2.2', '>= 2.2.2'
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.11"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ end
@@ -0,0 +1,44 @@
1
+ class GorgService
2
+ class << self
3
+ attr_writer :configuration
4
+
5
+ def configuration
6
+ @configuration ||= Configuration.new
7
+ end
8
+
9
+
10
+ def configure
11
+ yield(configuration)
12
+ end
13
+ end
14
+
15
+ class Configuration
16
+ attr_accessor :application_name,
17
+ :application_id,
18
+ :rabbitmq_host,
19
+ :rabbitmq_port,
20
+ :rabbitmq_queue_name,
21
+ :rabbitmq_exchange_name,
22
+ :rabbitmq_deferred_time,
23
+ :rabbitmq_max_attempts,
24
+ :rabbitmq_user,
25
+ :rabbitmq_password,
26
+ :message_handler_map,
27
+
28
+
29
+ def initialize
30
+
31
+ @application_name = "GorgService"
32
+ @application_id = "gs"
33
+ @message_handler_map = {}
34
+ @rabbitmq_host = "localhost"
35
+ @rabbitmq_port = 5672
36
+ @rabbitmq_queue_name = @application_id
37
+ @rabbitmq_deferred_time = 1800000 #30 minutes
38
+ @rabbitmq_exchange_name = "exchange"
39
+ @rabbitmq_user = nil
40
+ @rabbitmq_password = nil
41
+ @rabbitmq_max_attempts = 48 #24h with default timeout
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ class GorgService
5
+ class FailError < StandardError
6
+ attr_reader :error_raised
7
+
8
+ def initialize(error_raised)
9
+ @error_raised = error_raised
10
+ end
11
+ end
12
+
13
+ class SoftfailError < FailError
14
+ end
15
+
16
+ class HardfailError < FailError
17
+ end
18
+ end
@@ -0,0 +1,95 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require "bunny"
5
+
6
+ class GorgService
7
+ class Listener
8
+
9
+ def initialize(host: "localhost", port: 5672, queue_name: "gapps", rabbitmq_user: nil, rabbitmq_password: nil, exchange_name: nil, message_handler_map: {default: DefaultMessageHandler}, deferred_time: 1800000)
10
+ @host=host
11
+ @port=port
12
+ @queue_name=queue_name
13
+ @exchange_name=exchange_name
14
+ @rabbitmq_user=rabbitmq_user
15
+ @rabbitmq_password=rabbitmq_password
16
+ @message_handler_map=message_handler_map
17
+ @deferred_time=deferred_time
18
+
19
+ end
20
+
21
+ def listen
22
+
23
+ conn = rmq_connection
24
+ ch = conn.create_channel
25
+ x = ch.topic(@exchange_name, :durable => true)
26
+ q = ch.queue(@queue_name, :durable => true)
27
+
28
+ @message_handler_map.keys.each do |routing_key|
29
+ q.bind(@exchange_name, :routing_key => routing_key)
30
+ end
31
+
32
+ q.bind(@exchange_name, :routing_key => '#')
33
+
34
+ puts " [*] Waiting for messages in #{q.name}. To exit press CTRL+C"
35
+ ch.prefetch(1)
36
+
37
+ begin
38
+ q.subscribe(:manual_ack => true, :block => true) do |delivery_info, properties, body|
39
+ routing_key=delivery_info[:routing_key]
40
+ puts " [#] Received message with routing key #{routing_key} containing : #{body}"
41
+ message_handler=message_handler_for routing_key
42
+ message=Message.parse_body(body)
43
+ begin
44
+ if message_handler
45
+ message_handler.new(message)
46
+ else
47
+ raise HardfailError.new(), "Unknown routing_key"
48
+ end
49
+ rescue SoftfailError => e
50
+ message.log_error(e)
51
+ if message.errors.count > 5
52
+
53
+ else
54
+ send_to_deferred_queue(message)
55
+ end
56
+ rescue HardfailError => e
57
+ end
58
+
59
+ ch.ack(delivery_info.delivery_tag)
60
+ end
61
+ rescue Interrupt => _
62
+ conn.close
63
+ end
64
+ end
65
+
66
+ def rmq_connection
67
+ if @rmq_connection
68
+ @rmq_connection
69
+ else
70
+ @rmq_connection=Bunny.new(:hostname => @host, :user => @rabbitmq_user, :pass => @rabbitmq_password)
71
+ @rmq_connection.start
72
+ @rmq_connection
73
+ end
74
+ end
75
+
76
+ def send_to_deferred_queue(msg)
77
+ conn=rmq_connection
78
+ @delayed_chan||=conn.create_channel
79
+ q=@delayed_chan.queue("#{@queue_name}_deferred",
80
+ durable: true,
81
+ arguments: {
82
+ 'x-message-ttl' => @deferred_time,
83
+ 'x-dead-letter-exchange' => @exchange_name,
84
+ 'x-dead-letter-routing-key' => msg.event,
85
+ }
86
+ )
87
+ q.publish(msg.to_str, :routing_key => msg.event)
88
+ end
89
+
90
+ def message_handler_for routing_key
91
+ @message_handler_map[routing_key]
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+ require 'json'
4
+ require 'time'
5
+
6
+ class GorgService
7
+ class Message
8
+
9
+ attr_accessor :event
10
+ attr_accessor :id
11
+ attr_accessor :data
12
+ attr_accessor :errors
13
+
14
+
15
+ def initialize(id: nil, data: nil, event: nil, errors: [])
16
+ @id=id || generate_id
17
+ @event=event
18
+ @data=data
19
+ @errors=errors
20
+ end
21
+
22
+ # Generate new id
23
+ #
24
+ # TODO
25
+ # Avoid using AppConfig
26
+ def generate_id
27
+ "#{GorgService.configuration.application_id}_#{Time.now.to_i}"
28
+ end
29
+
30
+ # Generate RabbitMQ message body
31
+ def to_str
32
+ body={
33
+ id: @id,
34
+ event: @event,
35
+ data: @data,
36
+ }
37
+ if errors.any?
38
+ body[:errors_count]=@errors.count
39
+ body[:errors]=@errors
40
+ end
41
+ body.to_json
42
+ end
43
+
44
+ # Log FailError in message body
45
+ def log_error error
46
+ @errors<<{
47
+ type: error.class.to_s.downcase,
48
+ message: error.message,
49
+ timestamp: Time.now.utc.iso8601,
50
+ extra: error.error_raised.inspect,
51
+ }
52
+ end
53
+
54
+ ### Class methods
55
+
56
+ # Parse RabbitMQ message body
57
+ # @return Message
58
+ # parsed message
59
+ # Errors
60
+ # Hardfail if un-parsable JSON body
61
+ def self.parse_body(body)
62
+ begin
63
+ json_body=JSON.parse(body)
64
+
65
+ self.new(
66
+ id: json_body["id"],
67
+ event: json_body["event"],
68
+ data: convert_keys_to_sym(json_body["data"]),
69
+ errors: convert_keys_to_sym(json_body["errors"]),
70
+ )
71
+
72
+ rescue JSON::ParserError => e
73
+ raise HardfailError(e), "Unprocessable message : Unable to parse JSON message body"
74
+ end
75
+ end
76
+
77
+
78
+ def self.convert_keys_to_sym input_hash
79
+ s2s =
80
+ lambda do |h|
81
+ Hash === h ?
82
+ Hash[
83
+ h.map do |k, v|
84
+ [k.respond_to?(:to_sym) ? k.to_sym : k, s2s[v]]
85
+ end
86
+ ] : h
87
+ end
88
+ s2s[input_hash]
89
+ end
90
+
91
+
92
+ end
93
+ end
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ class GorgService
5
+ class MessageHandler
6
+
7
+ def initialize(message)
8
+ puts "WARNING : Defined your MessageHandler behavior in its 'initialize' method"
9
+ end
10
+
11
+ def raise_hardfail(message, error:nil)
12
+ raise HardfailError.new(error), message
13
+ end
14
+
15
+ def raise_softfail(message, error:nil)
16
+ raise HardfailError.new(error), message
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ class GorgService
5
+ VERSION = "0.1.1"
6
+ end
@@ -0,0 +1,25 @@
1
+ require "gorg_service/configuration"
2
+ require "gorg_service/version"
3
+ require "gorg_service/errors"
4
+ require "gorg_service/listener"
5
+ require "gorg_service/message"
6
+ require "gorg_service/message_handler"
7
+
8
+ class GorgService
9
+ def initialize()
10
+ @listener=Listener.new(
11
+ message_handler_map:GorgService.configuration.message_handler_map,
12
+ host: GorgService.configuration.rabbitmq_host,
13
+ port: GorgService.configuration.rabbitmq_port,
14
+ queue_name: GorgService.configuration.rabbitmq_queue_name,
15
+ exchange_name: GorgService.configuration.rabbitmq_exchange_name,
16
+ rabbitmq_user: GorgService.configuration.rabbitmq_user,
17
+ rabbitmq_password: GorgService.configuration.rabbitmq_password,
18
+ deferred_time: GorgService.configuration.rabbitmq_deferred_time,
19
+ )
20
+ end
21
+
22
+ def run
23
+ @listener.listen
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gorg_service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexandre Narbonne
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bunny
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.2.2
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.2.2
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.11'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.11'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ description:
76
+ email:
77
+ - alexandre.narbonne@gadz.org
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - ".gitignore"
83
+ - ".rspec"
84
+ - ".travis.yml"
85
+ - Gemfile
86
+ - LICENSE.txt
87
+ - README.md
88
+ - Rakefile
89
+ - bin/console
90
+ - bin/setup
91
+ - gorg_service.gemspec
92
+ - lib/gorg_service.rb
93
+ - lib/gorg_service/configuration.rb
94
+ - lib/gorg_service/errors.rb
95
+ - lib/gorg_service/listener.rb
96
+ - lib/gorg_service/message.rb
97
+ - lib/gorg_service/message_handler.rb
98
+ - lib/gorg_service/version.rb
99
+ homepage: https://github.com/Zooip/gorg_service
100
+ licenses:
101
+ - MIT
102
+ metadata:
103
+ allowed_push_host: https://rubygems.org
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.4.8
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Standard RabbitMQ bot used in Gadz.org SOA
124
+ test_files: []
125
+ has_rdoc: