eventq_rabbitmq 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e6bfe725e5c8daeae165116c3566fca4e29781c
4
- data.tar.gz: 12e56c05bda41c078959786fbae65f2cb1f88590
3
+ metadata.gz: 1947d069e568742cb144ffdb363926f06c3fd6e8
4
+ data.tar.gz: 19e5bb0f6cbc3dbb1e0945810ef9c2110db897a9
5
5
  SHA512:
6
- metadata.gz: 875c45b1f76b326b897640919b37a72a4cb1a762b483b4c93d3297934c9241fad1fbde80e4bfc40eebbe2fb0c58b13091089cdc287e0643e14ac6d9521c1cab0
7
- data.tar.gz: bc3f15d933a43d7bb19f5c5a35f081eeebe2aa168ef6d4a566407b7ca21b2f7ac598082279f478bdb152cfa53f226b681724868d1b9aae6384517b8ae13a1262
6
+ metadata.gz: e2bd1f695251534c604e3741ef9ad3ce33463523f3353cd95dde0bc42a065399bddf597cce16da7475e9a1b937938d2591d55252d38033b7aa4124e803524ac5
7
+ data.tar.gz: e86fc3bf3935b738a4ea037b47393a5225ddd5c97fc137aeedbf6a0f1a2c31a2151b598fdf0201a3df482c87029968338fe473cac7c465baa8b404c4b2364231
@@ -0,0 +1,24 @@
1
+ require 'oj'
2
+
3
+ class RabbitMqEventQClient
4
+
5
+ def initialize
6
+ @client = RabbitMqQueueClient.new
7
+ @queue_manager = RabbitMqQueueManager.new
8
+ @event_raised_exchange = EventRaisedExchange.new
9
+ end
10
+
11
+ def raise(event_type, event)
12
+ channel = @client.get_channel
13
+ ex = @queue_manager.get_exchange(channel, @event_raised_exchange)
14
+
15
+ qm = QueueMessage.new
16
+ qm.content = event
17
+ qm.type = event_type
18
+
19
+ message = Oj.dump(qm)
20
+
21
+ ex.publish(message, :routing_key => event_type)
22
+ end
23
+
24
+ end
@@ -0,0 +1,43 @@
1
+ class RabbitMqQueueClient
2
+
3
+ attr_accessor :channel
4
+
5
+ def initialize
6
+
7
+ @endpoint = 'localhost'
8
+ if ENV['MQ_ENDPOINT'] != nil
9
+ @endpoint = ENV['MQ_ENDPOINT']
10
+ end
11
+
12
+ @port = 5672
13
+ if ENV['MQ_PORT'] != nil
14
+ @port = Integer(ENV['MQ_PORT'])
15
+ end
16
+
17
+ @user = 'guest'
18
+ if ENV['MQ_USER'] != nil
19
+ @user = ENV['MQ_USER']
20
+ end
21
+
22
+ @password = 'guest'
23
+ if ENV['MQ_PASSWORD'] != nil
24
+ @password = ENV['MQ_PASSWORD']
25
+ end
26
+
27
+ @ssl = false
28
+ if ENV['MQ_SSL'] != nil
29
+ @ssl = ENV['MQ_SSL']
30
+ end
31
+
32
+ #@conn = Bunny.new(:host => @endpoint, :port => @port, :user => @user, :pass => @password, :ssl => @ssl)
33
+ #@conn.start
34
+ #@channel = conn.create_channel
35
+ end
36
+
37
+ def get_channel
38
+ conn = Bunny.new(:host => @endpoint, :port => @port, :user => @user, :pass => @password, :ssl => @ssl)
39
+ conn.start
40
+ return conn.create_channel
41
+ end
42
+
43
+ end
@@ -0,0 +1,42 @@
1
+ class RabbitMqQueueManager
2
+
3
+ def initialize
4
+ @event_raised_exchange = EventRaisedExchange.new
5
+ end
6
+
7
+ def get_queue(channel, queue)
8
+
9
+ #get/create the queue
10
+ q = channel.queue(queue.name, :durable => true)
11
+
12
+ if queue.allow_retry
13
+ retry_exchange = get_retry_exchange(channel, queue)
14
+ subscriber_exchange = get_subscriber_exchange(channel, queue)
15
+
16
+ retry_queue = get_retry_queue(channel, queue)
17
+ retry_queue.bind(retry_exchange)
18
+
19
+ q.bind(subscriber_exchange)
20
+ end
21
+
22
+ return q
23
+ end
24
+
25
+ def get_retry_exchange(channel, queue)
26
+ return channel.fanout("#{queue.name}.r.ex")
27
+ end
28
+
29
+ def get_subscriber_exchange(channel, queue)
30
+ return channel.fanout("#{queue.name}.ex")
31
+ end
32
+
33
+ def get_retry_queue(channel, queue)
34
+ subscriber_exchange = get_subscriber_exchange(channel, queue)
35
+ return channel.queue("#{queue.name}.r", :durable => true, :arguments => { "x-dead-letter-exchange" => subscriber_exchange.name, "x-message-ttl" => queue.retry_delay })
36
+ end
37
+
38
+ def get_exchange(channel, exchange)
39
+ return channel.direct(exchange.name, :durable => true)
40
+ end
41
+
42
+ end
@@ -0,0 +1,145 @@
1
+ class RabbitMqQueueWorker
2
+
3
+ @threads = []
4
+ @is_running = false
5
+
6
+ @retry_exceeded_block = nil
7
+
8
+ def start(queue, options = {})
9
+
10
+ configure(queue, options)
11
+
12
+ puts '[QUEUE_WORKER] Listening for messages.'
13
+
14
+ if @is_running
15
+ raise 'Worker is already running.'
16
+ end
17
+
18
+ @threads = []
19
+
20
+ #loop through each thread count
21
+ @thread_count.times do
22
+ thr = Thread.new do
23
+
24
+ client = RabbitMqQueueClient.new
25
+ manager = RabbitMqQueueManager.new
26
+
27
+ #begin the queue loop for this thread
28
+ while true do
29
+
30
+ channel = client.get_channel
31
+
32
+ #get the queue
33
+ q = manager.get_queue(channel, queue)
34
+ retry_exchange = manager.get_retry_exchange(channel, queue)
35
+
36
+ received = false
37
+ error = false
38
+
39
+ begin
40
+ delivery_info, properties, payload = q.pop(:manual_ack => true, :block => true)
41
+
42
+ #check that message was received
43
+ if payload != nil
44
+
45
+ message = Oj.load(payload)
46
+
47
+ puts "[QUEUE_WORKER] Message received. Retry Attempts: #{message.retry_attempts}"
48
+ puts properties
49
+
50
+ #begin worker block for queue message
51
+ begin
52
+ yield message.content, message.type, message.retry_attempts
53
+ #accept the message as processed
54
+ channel.acknowledge(delivery_info.delivery_tag, false)
55
+ puts '[QUEUE_WORKER] Message acknowledged.'
56
+ received = true
57
+ rescue => e
58
+ puts '[QUEUE_WORKER] An unhandled error happened attempting to process a queue message.'
59
+ puts "Error: #{e}"
60
+
61
+ #reject the message to remove from queue
62
+ channel.reject(delivery_info.delivery_tag, false)
63
+ error = true
64
+ puts '[QUEUE_WORKER] Message rejected.'
65
+
66
+ #check if the message is allowed to be retried
67
+ if queue.allow_retry
68
+
69
+ puts '[QUEUE_WORKER] Checking retry attempts...'
70
+ if message.retry_attempts < queue.max_retry_attempts
71
+ puts'[QUEUE_WORKER] Incrementing retry attempts count.'
72
+ message.retry_attempts += 1
73
+ puts '[QUEUE_WORKER] Sending message for retry.'
74
+ retry_exchange.publish(Oj.dump(message))
75
+ puts '[QUEUE_WORKER] Published message to retry exchange.'
76
+ else
77
+ if @retry_exceeded_block != nil
78
+ @retry_exceeded_block.call(message)
79
+ else
80
+ raise "[QUEUE_WORKER] No retry exceeded block specified."
81
+ end
82
+ end
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+
89
+ rescue Timeout::Error
90
+ puts 'Timeout occured attempting to pop a message from the queue.'
91
+ end
92
+
93
+ #check if any message was received
94
+ if !received && !error
95
+ puts "[QUEUE_WORKER] No message received. Sleeping for #{@sleep} seconds"
96
+ #no message received so sleep before attempting to pop another message from the queue
97
+ sleep(@sleep)
98
+ end
99
+
100
+ end
101
+
102
+ end
103
+ @threads.push(thr)
104
+
105
+ end
106
+
107
+ if options.key?(:wait) && options[:wait] == true
108
+ @threads.each { |thr| thr.join }
109
+ end
110
+
111
+ end
112
+
113
+ def stop
114
+ @threads.each do |t|
115
+ t.exit
116
+ end
117
+
118
+ @is_running = false
119
+ end
120
+
121
+ def on_retry_exceeded(&block)
122
+ @retry_exceeded_block = block
123
+ end
124
+
125
+ private
126
+
127
+ def configure(queue, options = {})
128
+
129
+ @queue = queue
130
+
131
+ #default thread count
132
+ @thread_count = 5
133
+ if options.key?(:thread_count)
134
+ @thread_count = options[:thread_count]
135
+ end
136
+
137
+ #default sleep time in seconds
138
+ @sleep = 15
139
+ if options.key?(:sleep)
140
+ @sleep = options[:sleep]
141
+ end
142
+
143
+ end
144
+
145
+ end
@@ -0,0 +1,28 @@
1
+ class RabbitMqSubscriptionManager
2
+
3
+ def initialize
4
+ @client = RabbitMqQueueClient.new
5
+ @queue_manager = RabbitMqQueueManager.new
6
+ @event_raised_exchange = EventRaisedExchange.new
7
+ end
8
+
9
+ def subscribe(event_type, queue)
10
+
11
+ channel = @client.get_channel
12
+ queue = @queue_manager.get_queue(channel, queue)
13
+ exchange = @queue_manager.get_exchange(channel, @event_raised_exchange)
14
+
15
+ queue.bind(exchange, :routing_key => event_type)
16
+ end
17
+
18
+ def unsubscribe(queue)
19
+
20
+ channel = @client.get_channel
21
+
22
+ queue = @queue_manager.get_queue(channel, queue)
23
+ exchange = @queue_manager.get_exchange(channel, @event_raised_exchange)
24
+
25
+ queue.unbind(exchange)
26
+ end
27
+
28
+ end
@@ -1,3 +1,3 @@
1
1
  module EventqRabbitmq
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eventq_rabbitmq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - vaughanbrittonsage
@@ -73,16 +73,14 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
- - ".gitignore"
77
- - CODE_OF_CONDUCT.md
78
- - Gemfile
79
- - LICENSE.txt
80
- - README.md
81
- - Rakefile
82
76
  - bin/console
83
77
  - bin/setup
84
- - eventq_rabbitmq.gemspec
85
78
  - lib/eventq_rabbitmq.rb
79
+ - lib/eventq_rabbitmq/rabbitmq_eventq_client.rb
80
+ - lib/eventq_rabbitmq/rabbitmq_queue_client.rb
81
+ - lib/eventq_rabbitmq/rabbitmq_queue_manager.rb
82
+ - lib/eventq_rabbitmq/rabbitmq_queue_worker.rb
83
+ - lib/eventq_rabbitmq/rabbitmq_subscription_manager.rb
86
84
  - lib/eventq_rabbitmq/version.rb
87
85
  homepage: https://github.com/vaughanbrittonsage/eventq
88
86
  licenses:
data/.gitignore DELETED
@@ -1,9 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
data/CODE_OF_CONDUCT.md DELETED
@@ -1,49 +0,0 @@
1
- # Contributor Code of Conduct
2
-
3
- As contributors and maintainers of this project, and in the interest of
4
- fostering an open and welcoming community, we pledge to respect all people who
5
- contribute through reporting issues, posting feature requests, updating
6
- documentation, submitting pull requests or patches, and other activities.
7
-
8
- We are committed to making participation in this project a harassment-free
9
- experience for everyone, regardless of level of experience, gender, gender
10
- identity and expression, sexual orientation, disability, personal appearance,
11
- body size, race, ethnicity, age, religion, or nationality.
12
-
13
- Examples of unacceptable behavior by participants include:
14
-
15
- * The use of sexualized language or imagery
16
- * Personal attacks
17
- * Trolling or insulting/derogatory comments
18
- * Public or private harassment
19
- * Publishing other's private information, such as physical or electronic
20
- addresses, without explicit permission
21
- * Other unethical or unprofessional conduct
22
-
23
- Project maintainers have the right and responsibility to remove, edit, or
24
- reject comments, commits, code, wiki edits, issues, and other contributions
25
- that are not aligned to this Code of Conduct, or to ban temporarily or
26
- permanently any contributor for other behaviors that they deem inappropriate,
27
- threatening, offensive, or harmful.
28
-
29
- By adopting this Code of Conduct, project maintainers commit themselves to
30
- fairly and consistently applying these principles to every aspect of managing
31
- this project. Project maintainers who do not follow or enforce the Code of
32
- Conduct may be permanently removed from the project team.
33
-
34
- This code of conduct applies both within project spaces and in public spaces
35
- when an individual is representing the project or its community.
36
-
37
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
- reported by contacting a project maintainer at vaughan.britton@sage.com. All
39
- complaints will be reviewed and investigated and will result in a response that
40
- is deemed necessary and appropriate to the circumstances. Maintainers are
41
- obligated to maintain confidentiality with regard to the reporter of an
42
- incident.
43
-
44
- This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
- version 1.3.0, available at
46
- [http://contributor-covenant.org/version/1/3/0/][version]
47
-
48
- [homepage]: http://contributor-covenant.org
49
- [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile DELETED
@@ -1,8 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in eventq_rabbitmq.gemspec
4
- gemspec
5
-
6
- gem 'bunny'
7
- gem 'oj'
8
- gem 'eventq_base'
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2016 vaughanbrittonsage
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 DELETED
@@ -1,36 +0,0 @@
1
- # EventQ [RabbitMq]
2
-
3
- Welcome to EventQ. This gem contains the RabbitMq implementations of the EventQ framework components.
4
-
5
- ## Installation
6
-
7
- Add this line to your application's Gemfile:
8
-
9
- ```ruby
10
- gem 'eventq_base'
11
- ```
12
-
13
- And then execute:
14
-
15
- $ bundle
16
-
17
- Or install it yourself as:
18
-
19
- $ gem install eventq_base
20
-
21
-
22
- ## Development
23
-
24
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
25
-
26
- 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).
27
-
28
- ## Contributing
29
-
30
- Bug reports and pull requests are welcome on GitHub at https://github.com/vaughanbrittonsage/eventq. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
31
-
32
-
33
- ## License
34
-
35
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
36
-
data/Rakefile DELETED
@@ -1,2 +0,0 @@
1
- require "bundler/gem_tasks"
2
- task :default => :spec
@@ -1,26 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'eventq_rabbitmq/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "eventq_rabbitmq"
8
- spec.version = EventqRabbitmq::VERSION
9
- spec.authors = ["vaughanbrittonsage"]
10
- spec.email = ["vaughanbritton@gmail.com"]
11
-
12
- spec.summary = 'This is the rabbitmq implementation for EventQ'
13
- spec.description = 'This is the rabbitmq implementation for EventQ'
14
- spec.homepage = "https://github.com/vaughanbrittonsage/eventq"
15
- spec.license = "MIT"
16
-
17
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = "exe"
19
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
21
-
22
- spec.add_development_dependency "bundler", "~> 1.11"
23
- spec.add_development_dependency "rake", "~> 10.0"
24
- spec.add_development_dependency "rspec"
25
- spec.add_development_dependency "pry"
26
- end