urabbit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: adcc16557fc9a39bb74252719281f9d5ba85cabe
4
+ data.tar.gz: d74ecf49a85e96b09b9926608ed3240983096616
5
+ SHA512:
6
+ metadata.gz: bab9f320bbd2e39e5bc4276bf7a185dd93276c9cdef05f1b12a9a407e09c7233cde9d97427c9801713ad79510f0f4e4352f19db392df441b72ee467a69f43700
7
+ data.tar.gz: 3675a9c90aab6927d38200bba95ff7e5785cf003441e3530efa73ba47623869ddfd1b33aa34602e8a13c8ad5983a34685ac6baf84a5a16a54363d73938ab3aa4
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in urabbit.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Jacek Becela
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.
@@ -0,0 +1,34 @@
1
+ # Urabbit
2
+
3
+ A small microservices library for RabbitMQ.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'urabbit'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install urabbit
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ 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).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ncr/urabbit.
30
+
31
+
32
+ ## License
33
+
34
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ t.warning = false
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "urabbit"
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
@@ -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,24 @@
1
+ require "bunny"
2
+ require "logger"
3
+ require "json"
4
+ require "securerandom"
5
+
6
+ require "urabbit/version"
7
+ require "urabbit/rpc"
8
+ require "urabbit/rpc/server"
9
+ require "urabbit/rpc/client"
10
+ require "urabbit/publisher"
11
+
12
+ module Urabbit
13
+ def self.logger
14
+ @logger ||= if defined?(Rails)
15
+ Rails.logger
16
+ else
17
+ Logger.new(STDOUT)
18
+ end
19
+ end
20
+
21
+ def self.logger=(logger)
22
+ @logger = logger
23
+ end
24
+ end
@@ -0,0 +1,39 @@
1
+ # Usage:
2
+ # begin
3
+ # pubisher = Publisher.new(
4
+ # exchange_name: "courier_tracker",
5
+ # routing_key: "in.courier_statuses.created"
6
+ # )
7
+ # publisher.publish(message)
8
+ # rescue Publisher::Error => exception
9
+ # puts exception.message
10
+ # puts exception.cause
11
+ # end
12
+ #
13
+ # Message is usually a JSON.
14
+ # Exception can contain a cause raised from Bunny.
15
+ class Urabbit::Publisher
16
+ class Error < Exception; end
17
+
18
+ def initialize(opts)
19
+ cloudamqp_url = opts[:cloudamqp_url] || ENV["CLOUDAMQP_URL"]
20
+ exchange_type = opts[:exchange_type] || :topic
21
+ exchange_name = opts[:exchange_name] ||
22
+ raise(Error.new("Please provide an 'exchange_name'"))
23
+ @routing_key = opts[:routing_key] ||
24
+ raise(Error.new("Please provide a 'routing_key'"))
25
+
26
+ @connection = Bunny.new(cloudamqp_url, logger: Urabbit.logger)
27
+ @connection.start
28
+ @channel = @connection.create_channel
29
+ @exchange = Bunny::Exchange.new(@channel, exchange_type, exchange_name)
30
+ rescue Bunny::Exception
31
+ raise Error.new("Error connecting to queue")
32
+ end
33
+
34
+ def publish(message)
35
+ @exchange.publish(message, routing_key: @routing_key)
36
+ rescue Bunny::Exception
37
+ raise Error.new("Error communicating with queue")
38
+ end
39
+ end
@@ -0,0 +1 @@
1
+ module Urabbit::RPC; end
@@ -0,0 +1,73 @@
1
+ # RPC::Client is a low level RPC client for RabbitMQ.
2
+ # It does not assume anything about message format.
3
+ #
4
+ # Usage:
5
+ #
6
+ # begin
7
+ # client = RPC::Client.new
8
+ # result = client.call(routing_key, message)
9
+ # rescue RPC::Client::Error => exception
10
+ # puts exception.message
11
+ # puts excpetion.cause
12
+ # end
13
+ #
14
+ # routing_key - a function name
15
+ # message - a String with function params as JSON
16
+ # result - a String with the result as JSON or nil in case of error
17
+ # exception - an Exception with message describing what went wrong,
18
+ # can be thrown during initialization and method calls.
19
+ # It can also contain a cause raised from Bunny itself.
20
+ class Urabbit::RPC::Client
21
+ class Error < Exception; end
22
+
23
+ def initialize(cloudamqp_url = ENV["CLOUDAMQP_URL"])
24
+ @connection = Bunny.new(cloudamqp_url, logger: Urabbit.logger)
25
+ @connection.start
26
+
27
+ @channel = @connection.create_channel
28
+ @exchange = @channel.default_exchange
29
+ @reply_queue = @channel.queue("amq.rabbitmq.reply-to")
30
+
31
+ @lock = Mutex.new
32
+ @condition = ConditionVariable.new
33
+
34
+ @reply_queue.subscribe do |delivery_info, properties, payload|
35
+ if properties[:correlation_id] == @correlation_id
36
+ # Headers are only present if explicitly set.
37
+ if error = properties.to_hash.dig(:headers, 'error')
38
+ @error = error
39
+ else
40
+ @result = payload
41
+ end
42
+
43
+ @lock.synchronize{@condition.signal}
44
+ end
45
+ end
46
+ rescue Bunny::Exception
47
+ raise Error.new("Error connecting to queue")
48
+ end
49
+
50
+ def call(routing_key, message, timeout = 10)
51
+ @correlation_id = SecureRandom.uuid
52
+
53
+ @exchange.publish(message,
54
+ routing_key: routing_key,
55
+ correlation_id: @correlation_id,
56
+ reply_to: "amq.rabbitmq.reply-to"
57
+ )
58
+
59
+ @lock.synchronize{@condition.wait(@lock, timeout)}
60
+
61
+ if @error.nil? && @result.nil?
62
+ raise Error.new("Timed out waiting for reply")
63
+ end
64
+
65
+ if @error
66
+ raise Error.new(@error['message'])
67
+ else
68
+ @result
69
+ end
70
+ rescue Bunny::Exception
71
+ raise Error.new("Error communicating with queue")
72
+ end
73
+ end
@@ -0,0 +1,70 @@
1
+ module Urabbit::RPC::Server
2
+ def initialize(cloudamqp_url = ENV["CLOUDAMQP_URL"])
3
+ @connection = Bunny.new(cloudamqp_url, logger: logger)
4
+ @connection.start
5
+
6
+ @channel = @connection.create_channel
7
+
8
+ # TODO: Test which setting is the best
9
+ # @channel.prefetch(1)
10
+ end
11
+
12
+ # TODO: Currently this is called directly, but it should
13
+ # be called using server_engine.
14
+ def start
15
+ logger.info("Starting RPC server for #{self.class.name}")
16
+
17
+ @queue = @channel.queue(self.class.queue_name)
18
+ @exchange = @channel.default_exchange
19
+
20
+ @queue.subscribe(block: true) do |delivery_info, properties, payload|
21
+ begin
22
+ result = work(payload)
23
+
24
+ @exchange.publish(result,
25
+ routing_key: properties.reply_to,
26
+ correlation_id: properties.correlation_id
27
+ )
28
+ rescue => exception
29
+ @exchange.publish("",
30
+ routing_key: properties.reply_to,
31
+ correlation_id: properties.correlation_id,
32
+ headers: {
33
+ error: {
34
+ code: 500,
35
+ message: exception.message
36
+ }
37
+ }
38
+ )
39
+ end
40
+ end
41
+
42
+ logger.info("Stopped responding to RPC calls for #{self.class.name}")
43
+ end
44
+
45
+ # TODO: Use this method when server_engine is implemented.
46
+ def stop
47
+ @channel.close
48
+ @connection.close
49
+
50
+ logger.info("Stopped RPC server for #{self.class.name}")
51
+ end
52
+
53
+ private
54
+
55
+ def logger
56
+ Urabbit.logger
57
+ end
58
+
59
+ def self.included(base)
60
+ base.extend ClassMethods
61
+ end
62
+
63
+ module ClassMethods
64
+ attr_reader :queue_name
65
+
66
+ def from_queue(queue_name)
67
+ @queue_name = queue_name
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module Urabbit
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'urabbit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "urabbit"
8
+ spec.version = Urabbit::VERSION
9
+ spec.authors = ["Jacek Becela"]
10
+ spec.email = ["jacek.becela@gmail.com"]
11
+
12
+ spec.summary = %q{A small microservices library using RabbitMQ}
13
+ spec.homepage = "https://github.com/ncr/urabbit"
14
+ spec.license = "MIT"
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.12"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "minitest", "~> 5.0"
23
+
24
+ spec.add_dependency "bunny", "~> 2.6.1"
25
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: urabbit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jacek Becela
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-26 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bunny
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.6.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.6.1
69
+ description:
70
+ email:
71
+ - jacek.becela@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/urabbit.rb
85
+ - lib/urabbit/publisher.rb
86
+ - lib/urabbit/rpc.rb
87
+ - lib/urabbit/rpc/client.rb
88
+ - lib/urabbit/rpc/server.rb
89
+ - lib/urabbit/version.rb
90
+ - urabbit.gemspec
91
+ homepage: https://github.com/ncr/urabbit
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: A small microservices library using RabbitMQ
115
+ test_files: []