bunny_priority_queue 0.0.1

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: 80c8adb42bfdad05da54a586ef20e1a9335519ff
4
+ data.tar.gz: d9ee1f4e411a7cc7473c0d2483ba26c2d4449fa2
5
+ SHA512:
6
+ metadata.gz: 539f1067c4b75689013866f4ad66bdcb221ca1ac83ccfccde8a6e9c004543a48dcf594f1219d37372aa754f33154500a798b3c6dd1f0d7307d0e098e2fcbaf8a
7
+ data.tar.gz: b247415a0889e9d84976bafc5651ed67f7c30de230612853feab28cc100d6982494eff3842b982751f12d88bc5a84ed84f2c7568226b643600ad8882a6df2f2f
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .idea
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ .ruby-version
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
20
+ vendor/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bunny_priority_queue.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Takahiro Kikumoto
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # BunnyPriorityQueue
2
+
3
+ Priority Queue library using Bunny with RabbitMQ.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'bunny_priority_queue'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install bunny_priority_queue
18
+
19
+ ## Usage
20
+
21
+ see https://github.com/kikumoto/bunny_priority_queue/tree/master/examples
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/kikumoto/bunny_priority_queue/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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 'bunny_priority_queue/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bunny_priority_queue"
8
+ spec.version = BunnyPriorityQueue::VERSION
9
+ spec.authors = ["Takahiro Kikumoto"]
10
+ spec.email = ["takakiku810@gmail.com"]
11
+ spec.summary = %q{Priority Queue library using Bunny with RabbitMQ.}
12
+ spec.description = %q{Priority Queue library using Bunny with RabbitMQ.}
13
+ spec.homepage = "https://github.com/kikumoto/bunny_priority_queue"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_runtime_dependency "bunny"
25
+ end
@@ -0,0 +1,15 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ EXCHANE_NAME = "sample_exchange"
4
+ QUEUE_PREFIX = "sample_queue"
5
+
6
+
7
+ PRIORITY_HIGH = "high"
8
+ PRIORITY_NORMAL = "normal"
9
+ PRIORITY_LOW = "low"
10
+
11
+ PRIORITIES = [
12
+ PRIORITY_HIGH,
13
+ PRIORITY_NORMAL,
14
+ PRIORITY_LOW,
15
+ ]
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require "bundler"
5
+ Bundler.setup
6
+
7
+ $:.unshift(File.expand_path("../../lib", __FILE__))
8
+
9
+ require 'bunny_priority_queue'
10
+ require_relative './common.rb'
11
+
12
+ begin
13
+ conn = Bunny.new.start
14
+ exchange = conn.create_channel.direct(EXCHANE_NAME)
15
+
16
+ q = BunnyPriorityQueue.create(
17
+ QUEUE_PREFIX,
18
+ PRIORITIES,
19
+ exchange
20
+ )
21
+
22
+ q.bind
23
+
24
+ q.subscribe(:block => true) do |delivery_info, properties, body|
25
+ puts "routing_key: #{delivery_info.routing_key}"
26
+ puts "properties : #{properties}"
27
+ puts "payload : #{body}"
28
+ end
29
+
30
+ ensure
31
+ conn.close
32
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require "bundler"
5
+ Bundler.setup
6
+
7
+ $:.unshift(File.expand_path("../../lib", __FILE__))
8
+
9
+ require 'bunny_priority_queue'
10
+ require_relative './common.rb'
11
+
12
+ begin
13
+ conn = Bunny.new.start
14
+
15
+ exchange = conn.create_channel.direct(EXCHANE_NAME)
16
+
17
+ q = BunnyPriorityQueue.new(
18
+ QUEUE_PREFIX,
19
+ PRIORITIES,
20
+ )
21
+
22
+ exchange.publish("normal", :routing_key => q.name(PRIORITY_NORMAL))
23
+ ensure
24
+ conn.close
25
+ end
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8; mode: ruby -*-
2
+
3
+ require "bunny"
4
+
5
+ require "bunny_priority_queue/version"
6
+ require "bunny_priority_queue/queue"
7
+
8
+ module BunnyPriorityQueue
9
+ # @return [String] Bunny version
10
+ def self.version
11
+ VERSION
12
+ end
13
+
14
+ # Instantiates a new priority queue.
15
+ #
16
+ # @return [BunnyPriorityQueue::Queue]
17
+ # @see BunnyPriorityQueue::Queue#initialize
18
+ # @api public
19
+ def self.new(prefix, priorities)
20
+ Queue.new(prefix, priorities)
21
+ end
22
+
23
+ # Instantiates a new priority queue and create queue.
24
+ #
25
+ # @return [BunnyPriorityQueue::Queue]
26
+ # @see BunnyPriorityQueue::Queue#initialize
27
+ # @see BunnyPriorityQueue::Queue#create
28
+ # @api public
29
+ def self.create(prefix, priorities, exchange, level_ttl = 2000, queue_opts = {})
30
+ q = Queue.new(prefix, priorities)
31
+ q.create(exchange, level_ttl, queue_opts)
32
+ q
33
+ end
34
+ end
@@ -0,0 +1,114 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "bunny"
3
+
4
+
5
+ module BunnyPriorityQueue
6
+ class Queue
7
+ # @param prefix [String] prefix of queue name
8
+ # @param priorities [Array] each prirority queue names
9
+ def initialize(prefix, priorities)
10
+ @prefix = prefix
11
+ @priorities = priorities
12
+ end
13
+
14
+ # @return [String] a priority queue name
15
+ def name(priority)
16
+ "#{@prefix}.#{priority}"
17
+ end
18
+
19
+ # create priority queues
20
+ #
21
+ # @return [Queue]
22
+ # @param exchange [Bunny::Exchange] exchange
23
+ # @param level_ttl [Integer] PriorityQueue Message TTL (msec)
24
+ # @param queue_opts [Hash] Bunny::Queue options
25
+ def create(exchange, level_ttl = 2000, queue_opts = {})
26
+ @exchange = exchange
27
+
28
+ queue_opts[:arguments] ||= {}
29
+ args = queue_opts[:arguments]
30
+
31
+ original_arguments = backup_arguments(args, ["x-dead-letter-exchange", "x-message-ttl", "x-dead-letter-routing-key"])
32
+
33
+ @queues = @priorities.map.with_index do |priority, i|
34
+ args["x-priority-level-index"] = i
35
+
36
+ if i == 0
37
+ original_arguments.each do |k, v|
38
+ args[k] = v unless v.nil?
39
+ end
40
+ else
41
+ # when a message is expired, its is deivered to upper priority queue fia DLX.
42
+ args["x-dead-letter-routing-key"] = self.name(@priorities[i-1])
43
+ args["x-message-ttl"] = level_ttl
44
+ args["x-dead-letter-exchange"] = @exchange.name
45
+ end
46
+
47
+ @exchange.channel.queue("#{self.name(priority)}", queue_opts)
48
+ end
49
+
50
+ self
51
+ end
52
+
53
+ # bind queue to exchange
54
+ #
55
+ def bind
56
+ @queues.each do |q|
57
+ q.bind(@exchange, :routing_key => q.name)
58
+ end
59
+ self
60
+ end
61
+
62
+ # subscribe queue
63
+ #
64
+ def subscribe(opts, &block)
65
+ manual_ack = opts[:ack] || opts[:manual_ack]
66
+ consumers = @queues.map do |q|
67
+ q.subscribe(:ack => true) do |delivery_info, properties, body|
68
+ d, p, b = check_higher_queue(q.arguments["x-priority-level-index"])
69
+
70
+ unless d.nil?
71
+ channel.reject(delivery_info.delivery_tag, true)
72
+ delivery_info = d
73
+ properties = p
74
+ body = b
75
+ end
76
+
77
+ channel.ack(delivery_info.delivery_tag) unless manual_ack
78
+ yield delivery_info, properties, body
79
+ end
80
+ end
81
+
82
+ if opts[:block]
83
+ channel.work_pool.join
84
+ end
85
+
86
+ consumers
87
+ end
88
+
89
+
90
+ private
91
+
92
+ def channel
93
+ @exchange.channel
94
+ end
95
+
96
+ def check_higher_queue(current_queue_index)
97
+ 0.upto(current_queue_index -1) do |i|
98
+ d, p, b = @queues[i].pop(:ack => true)
99
+ return [d, p, b] unless d.nil?
100
+ end
101
+
102
+ [nil, nil, nil]
103
+ end
104
+
105
+ def backup_arguments(args, keys)
106
+ original = {}
107
+ keys.each do |k|
108
+ original[k] = args[k]
109
+ args.delete k
110
+ end
111
+ original
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,3 @@
1
+ module BunnyPriorityQueue
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bunny_priority_queue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Takahiro Kikumoto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-04 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: bunny
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
+ description: Priority Queue library using Bunny with RabbitMQ.
56
+ email:
57
+ - takakiku810@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bunny_priority_queue.gemspec
68
+ - examples/common.rb
69
+ - examples/consumer.rb
70
+ - examples/producer.rb
71
+ - lib/bunny_priority_queue.rb
72
+ - lib/bunny_priority_queue/queue.rb
73
+ - lib/bunny_priority_queue/version.rb
74
+ homepage: https://github.com/kikumoto/bunny_priority_queue
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.0
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Priority Queue library using Bunny with RabbitMQ.
98
+ test_files: []