caerbannog 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd7777dd47aea5fb2c75fe83337676f39bdcfe4a
4
- data.tar.gz: 2d43c2b1f4d9e262483e45bbf7e0fff161ddda34
3
+ metadata.gz: 023d6338359111413e0f62e40ec72aa34442e002
4
+ data.tar.gz: 6a54db3d5aac7ab4deced6c17e2100473a513ea4
5
5
  SHA512:
6
- metadata.gz: 87f4705b972e70f7ba5ebcbab7236f609050b3c8c370722e5ef3e1a8d4ec388d137643c1ba65c5e38c275f34287e9e9902dc5f175673c849859d7c75b508d49e
7
- data.tar.gz: 9be692dc762855119244d7a912ed19c36de91aa5fc31f8ab9e0cad5cfffd984f04bb59db01add5f992d63869d9f93529f598e6c8f495457b60ba01110caca3ec
6
+ metadata.gz: 7181df709957bb02a3ef3f3959ca1641382b2847c811c7a36b4266d2c609e18b33a3c1a90570a4a94e4a3c73a45ab4adb9123cf5f53877dfb52363af47286bb5
7
+ data.tar.gz: 693e29df25da07ec0902af3892dde90ff7bd8621bdf5caee0c636de9f0ae3805c34e3112f37d9da88e7dadc3a5c83a4b5003785b7677bf0b43b61b41c2bbb451
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # Caerbannog
2
+ [![Circle CI](https://circleci.com/gh/magplus/caerbannog.png?style=shield)](https://circleci.com/gh/magplus/caerbannog)
2
3
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/caerbannog`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+ Implements a database buffer and workers for sending and receiving events
5
+ to/from RabbitMQ.
4
6
 
5
- TODO: Delete this and the text above, and describe your gem
6
7
 
7
8
  ## Installation
8
9
 
@@ -22,13 +23,87 @@ Or install it yourself as:
22
23
 
23
24
  ## Usage
24
25
 
25
- TODO: Write usage instructions here
26
+ The gem is meant to be used by a sender application and one or more receiver
27
+ applications.
28
+
29
+ ### On the sender side
30
+
31
+ Caerbannog needs to be configured with a message storage class that is an
32
+ ActiveRecord model or works like an ActiveRecord model with regards to the
33
+ methods `.all`, `.create!` and `#destroy`, and has two attributes `name` and
34
+ `payload`. It also needs a RabbitMQ read and a write URL.
35
+
36
+ ```ruby
37
+ Caerbannog.configure do |config|
38
+ config.message_class = MessageQueueMessage
39
+ config.rabbit_read_url = ENV['RABBIT_URL']
40
+ config.rabbit_write_url = ENV['RABBIT_URL']
41
+ end
42
+ ```
43
+
44
+ If you are using the gem from within a Rails application, you can run the
45
+ following to generate this model and the configuration:
46
+
47
+ $ rails generate caerbannog
48
+
49
+ This generates a migration file, an ActiveRecord message class
50
+ `MessageQueueMessage`, and an initializer file.
51
+
52
+ This class will be used to store messages when you call
53
+ `Caerbannog::Queue.push`, before they are sent to RabbitMQ,
54
+
55
+ Call the `Caerbannog::Queue.push` method with two parameters: the message name,
56
+ that will be used as the routing key in RabbitMQ, and the message payload,
57
+ which should be a hash or an array.
58
+
59
+ ```ruby
60
+ Caerbannog::Queue.push('message name', { one_field: 'one', two_field: 'two' })
61
+ ```
62
+
63
+ We then need a background publisher that uses the `all` method of the message
64
+ class to fetch all pushed messages and send them to RabbitMQ, and then
65
+ `destroy`s them.
66
+
67
+ If you have initialized the `Caerbannog::Queue#message_class=`, you can use
68
+ something like this to start the publisher process:
69
+
70
+ $ bundle exec rails runner 'Caerbannog::Queue.publish'
71
+
72
+ ### On the receiving side
73
+
74
+ To receive messages you need to run a subscriber process that calls the
75
+ `Caerbannog::Queue.subscribe` method. An example subscriber class with some
76
+ error handling might look like this:
77
+
78
+ ```ruby
79
+ class MessageQueueWorker
80
+ def perform
81
+ Caerbannog::Queue.subscribe('my-apps-queue-name', 'message-name1', 'message-name2') do |delivery_info, properties, payload|
82
+ parsed_message = JSON.parse(payload)
83
+ # Do something with the parsed message
84
+ end
85
+ rescue Bunny::TCPConnectionFailedForAllHosts => e
86
+ puts 'Oh no' # Handle the error somehow
87
+ sleep 10 # Wait for RabbitMQ to come back up
88
+ retry # Try to reconnect
89
+ end
90
+ end
91
+ ```
92
+
93
+ and you might run this process like
94
+
95
+ $ bundle exec rails runner 'MessageQueueWorker'
26
96
 
27
97
  ## Development
28
98
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
99
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
100
+ `bin/console` for an interactive prompt that will allow you to experiment.
30
101
 
31
- 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
102
+ To install this gem onto your local machine, run `bundle exec rake install`. To
103
+ release a new version, update the version number in `version.rb`, and then run
104
+ `bundle exec rake release` to create a git tag for the version, push git
105
+ commits and tags, and push the `.gem` file to
106
+ [rubygems.org](https://rubygems.org).
32
107
 
33
108
  ## Contributing
34
109
 
data/Rakefile CHANGED
@@ -1,2 +1,2 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
 
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "caerbannog"
3
+ require 'bundler/setup'
4
+ require 'caerbannog'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
8
8
 
9
9
  # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
10
+ # require 'pry'
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start
data/caerbannog.gemspec CHANGED
@@ -4,40 +4,39 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'caerbannog/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "caerbannog"
7
+ spec.name = 'caerbannog'
8
8
  spec.version = Caerbannog::VERSION
9
9
  spec.authors = [
10
- "Dennis Rogenius",
11
- "Karl Eklund",
12
- "Kristoffer Roupé",
13
- "Lennart Fridén",
14
- "Martin Svalin",
15
- "Mikael Amborn",
16
- "Victoria Wagman"
10
+ 'Dennis Rogenius',
11
+ 'Karl Eklund',
12
+ 'Kristoffer Roupé',
13
+ 'Lennart Fridén',
14
+ 'Martin Svalin',
15
+ 'Mikael Amborn',
16
+ 'Victoria Wagman'
17
17
  ]
18
18
  spec.email = [
19
- "dennis@magplus.com",
20
- "karl@magplus.com",
21
- "kristoffer@maglus.com",
22
- "lennart@magplus.com",
23
- "martin@magplus.com",
24
- "mikael.amborn@magplus.com",
25
- "victoria@magplus.com"
19
+ 'dennis@magplus.com',
20
+ 'karl@magplus.com',
21
+ 'kristoffer@maglus.com',
22
+ 'lennart@magplus.com',
23
+ 'martin@magplus.com',
24
+ 'mikael.amborn@magplus.com',
25
+ 'victoria@magplus.com'
26
26
  ]
27
27
 
28
28
  spec.summary = %q{Library for handling messages}
29
29
  spec.description = %q{Implements a database buffer and workers for sending events to RabbitMQ}
30
- spec.homepage = "https://github.com/magplus/caerbannog"
31
- spec.license = "MIT"
30
+ spec.homepage = 'https://github.com/magplus/caerbannog'
31
+ spec.license = 'MIT'
32
32
 
33
33
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
34
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
- spec.require_paths = ["lib"]
35
+ spec.require_paths = ['lib']
36
+ spec.required_ruby_version = '>= 2.0.0'
36
37
 
37
- spec.add_development_dependency "bundler", "~> 1.8"
38
- spec.add_development_dependency "rake", "~> 10.0"
39
- spec.add_development_dependency "mocha"
40
- spec.add_development_dependency "rspec"
41
-
42
- spec.add_dependency "bunny"
38
+ spec.add_development_dependency 'rake', "~> 10.0"
39
+ spec.add_development_dependency 'mocha'
40
+ spec.add_development_dependency 'rspec'
41
+ spec.add_dependency 'bunny'
43
42
  end
data/circle.yml ADDED
@@ -0,0 +1,3 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.0.0
@@ -1,15 +1,15 @@
1
1
  module Caerbannog
2
2
  class Queue
3
- def self.message_class=(message_class)
4
- @message_class = message_class
5
- end
6
-
7
3
  def self.push(name, payload)
8
- @message_class.create!(:name => name, :payload => JSON.generate(payload))
4
+ raise ConfigurationError.new("Must configure #{self.name} with message_class") unless Caerbannog.message_class
5
+
6
+ Caerbannog.message_class.create!(:name => name, :payload => JSON.generate(payload))
9
7
  end
10
8
 
11
- def self.rabbitmq
12
- Bunny.run ENV['RABBIT_URL'] do |conn|
9
+ def self.rabbitmq(rabbit_url)
10
+ raise ConfigurationError.new("Must configure #{self.name} with rabbit_read_url and/or rabbit_write_url") unless rabbit_url
11
+
12
+ Bunny.run rabbit_url do |conn|
13
13
  ch = conn.create_channel
14
14
  exchange = ch.direct('events', :durable => true)
15
15
  yield exchange, ch
@@ -17,7 +17,7 @@ module Caerbannog
17
17
  end
18
18
 
19
19
  def self.subscribe(queue_name, *routing_keys, &block)
20
- rabbitmq do |exchange, channel|
20
+ rabbitmq Caerbannog.rabbit_read_url do |exchange, channel|
21
21
  queue = channel.queue(queue_name)
22
22
  routing_keys.each { |routing_key| queue.bind(exchange, :routing_key => routing_key) }
23
23
  queue.subscribe(:block => true, &block)
@@ -25,7 +25,7 @@ module Caerbannog
25
25
  end
26
26
 
27
27
  def self.publish(messages = MessagePoller.new(@message_class))
28
- rabbitmq do |exchange|
28
+ rabbitmq Caerbannog.rabbit_write_url do |exchange|
29
29
  messages.each do |message|
30
30
  exchange.publish(message.payload, :routing_key => message.name, :persistent => true)
31
31
  message.destroy
@@ -1,3 +1,3 @@
1
1
  module Caerbannog
2
- VERSION = "0.1.0"
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/caerbannog.rb CHANGED
@@ -1,7 +1,19 @@
1
- require "bundler/setup"
2
- require "bunny"
1
+ require 'bundler/setup'
2
+ require 'bunny'
3
3
  require 'json'
4
4
 
5
- require "caerbannog/version"
6
- require "caerbannog/message_poller"
7
- require "caerbannog/queue"
5
+ require 'caerbannog/version'
6
+ require 'caerbannog/message_poller'
7
+ require 'caerbannog/queue'
8
+
9
+ module Caerbannog
10
+ class ConfigurationError < StandardError; end
11
+
12
+ class << self
13
+ attr_accessor :message_class, :rabbit_read_url, :rabbit_write_url
14
+
15
+ def configure
16
+ yield self
17
+ end
18
+ end
19
+ end
@@ -1,21 +1,21 @@
1
1
  class CaerbannogGenerator < Rails::Generators::Base
2
2
  include Rails::Generators::Migration
3
3
  source_root File.expand_path('../templates', __FILE__)
4
-
4
+
5
5
  def self.next_migration_number(path)
6
- Time.now.utc.strftime("%Y%m%d%H%M%S")
6
+ Time.now.utc.strftime('%Y%m%d%H%M%S')
7
7
  end
8
8
 
9
9
  def copy_migration
10
- migration_template "add_caerbannog_messages.rb", "db/migrate/add_caerbannog_messages.rb"
10
+ migration_template 'add_caerbannog_messages.rb', 'db/migrate/add_caerbannog_messages.rb'
11
11
  end
12
12
 
13
13
  def create_message_model
14
- copy_file "caerbannog_message.rb", "app/models/caerbannog_message.rb"
14
+ copy_file 'caerbannog_message.rb', 'app/models/caerbannog_message.rb'
15
15
  end
16
16
 
17
17
  def create_caerbannog_initializer
18
- copy_file "caerbannog_initializer.rb", "config/initializers/caerbannog_initializer.rb"
18
+ copy_file 'caerbannog_initializer.rb', 'config/initializers/caerbannog_initializer.rb'
19
19
  end
20
20
  end
21
-
21
+
@@ -1 +1,5 @@
1
- Caerbannog::Queue.message_class = CaerbannogMessage
1
+ Caerbannog::Queue.configure do |config|
2
+ config.message_class = CaerbannogMessage
3
+ config.rabbit_read_url = ENV['RABBIT_URL']
4
+ config.rabbit_write_url = ENV['RABBIT_URL']
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caerbannog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Rogenius
@@ -14,22 +14,8 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2015-03-18 00:00:00.000000000 Z
17
+ date: 2015-03-20 00:00:00.000000000 Z
18
18
  dependencies:
19
- - !ruby/object:Gem::Dependency
20
- name: bundler
21
- requirement: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - "~>"
24
- - !ruby/object:Gem::Version
25
- version: '1.8'
26
- type: :development
27
- prerelease: false
28
- version_requirements: !ruby/object:Gem::Requirement
29
- requirements:
30
- - - "~>"
31
- - !ruby/object:Gem::Version
32
- version: '1.8'
33
19
  - !ruby/object:Gem::Dependency
34
20
  name: rake
35
21
  requirement: !ruby/object:Gem::Requirement
@@ -110,6 +96,7 @@ files:
110
96
  - bin/console
111
97
  - bin/setup
112
98
  - caerbannog.gemspec
99
+ - circle.yml
113
100
  - lib/caerbannog.rb
114
101
  - lib/caerbannog/message_poller.rb
115
102
  - lib/caerbannog/queue.rb
@@ -130,7 +117,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
130
117
  requirements:
131
118
  - - ">="
132
119
  - !ruby/object:Gem::Version
133
- version: '0'
120
+ version: 2.0.0
134
121
  required_rubygems_version: !ruby/object:Gem::Requirement
135
122
  requirements:
136
123
  - - ">="