redis-queue 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in redis-queue.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Francesco Laurita
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ redis-queue
2
+ =============
3
+ Requires the redis gem.
4
+
5
+ Adds Redis::Queue class which can be used as Distributed-Queue based on Redis.
6
+ Redis is often used as a messaging server to implement processing of background jobs or other kinds of messaging tasks.
7
+ It implements Reliable-queue pattern decribed here: http://redis.io/commands/rpoplpush.
8
+
9
+ Installation
10
+ ----------------
11
+ $ gem install redis-queue
12
+
13
+ Testing
14
+ ----------------
15
+ $ bundle install
16
+ $ rake
17
+
18
+ Simple usage
19
+ ----------------
20
+
21
+ ```ruby
22
+ require "redis-queue"
23
+ redis = Redis.new
24
+ queue = Redis::Queue.new('q_test','bp_q_test', :redis => redis)
25
+
26
+ #Adding some elements
27
+ queue.push "b"
28
+ queue << "a" # << is an alias of push
29
+
30
+ # Process messages
31
+
32
+ # By default, calling pop methid is a blocking operation
33
+ # Your code will wait here for a new
34
+ while message=@queue.pop
35
+ #Remove message from the backup queue if the message has been processed without errors
36
+ queue.commit if YourTask.new(message).perform.succeed?
37
+ end
38
+
39
+ #Process messages using block
40
+
41
+ @queue.process do |message|
42
+ #@queue.commit is called if last statement of the block returns true
43
+ YourTask.new(message).perform.succeed?
44
+ end
45
+
46
+ ```
47
+ Contributing to redis-queue
48
+ ----------------
49
+
50
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
51
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
52
+ * Fork the project.
53
+ * Start a feature/bugfix branch.
54
+ * Commit and push until you are happy with your contribution.
55
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
56
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
57
+
58
+ Copyright
59
+ ----------------
60
+
61
+ Copyright (c) 2013 Francesco Laurita. See LICENSE.txt for
62
+ further details.
63
+
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |spec|
5
+ spec.pattern = 'spec/*_spec.rb'
6
+ end
7
+
8
+ task :default => :spec
9
+ task :test => :spec
@@ -0,0 +1,2 @@
1
+ require "redis"
2
+ require "redis/queue"
@@ -0,0 +1,71 @@
1
+ class Redis
2
+ class Queue
3
+
4
+ VERSION = "0.0.1"
5
+
6
+ def self.version
7
+ "redis-queue version #{VERSION}"
8
+ end
9
+
10
+ def initialize(queue_name, process_queue_name, options = {})
11
+ raise ArgumentError, 'First argument must be a non emmpty string' if !queue_name.is_a?(String) || queue_name.empty?
12
+ raise ArgumentError, 'Second argument must be a non emmpty string' if !process_queue_name.is_a?(String) || process_queue_name.empty?
13
+ raise ArgumentError, 'Queue and Process queue have the same name' if process_queue_name == queue_name
14
+
15
+ @redis = options[:redis] || Redis.current
16
+ @queue_name = queue_name
17
+ @process_queue_name = process_queue_name
18
+ @last_message = nil
19
+ end
20
+
21
+ def length
22
+ @redis.llen @queue_name
23
+ end
24
+
25
+ def clear(clear_process_queue = false)
26
+ @redis.del @queue_name
27
+ @redis.del @process_queue_name if clear_process_queue
28
+ end
29
+
30
+ def empty?
31
+ !(length > 0)
32
+ end
33
+
34
+ def push(obj)
35
+ @redis.lpush(@queue_name, obj)
36
+ end
37
+
38
+ def pop(non_block=false)
39
+ if non_block
40
+ @last_message = @redis.rpoplpush(@queue_name,@process_queue_name)
41
+ else
42
+ @last_message = @redis.brpoplpush(@queue_name,@process_queue_name)
43
+ end
44
+ @last_message
45
+ end
46
+
47
+ def commit
48
+ @redis.lrem(@process_queue_name, 0, @last_message)
49
+ end
50
+
51
+ def process(non_block=false)
52
+ while message=pop(non_block)
53
+ ret = yield message if block_given?
54
+ commit if ret
55
+ end
56
+ end
57
+
58
+ def refill
59
+ while message=@redis.lpop(@process_queue_name)
60
+ @redis.rpush(@queue_name, message)
61
+ end
62
+ true
63
+ end
64
+
65
+ alias :size :length
66
+ alias :dec :pop
67
+ alias :shift :pop
68
+ alias :enc :push
69
+ alias :<< :push
70
+ end
71
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "redis-queue"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "redis-queue"
7
+ s.version = Redis::Queue::VERSION
8
+ s.authors = ["Francesco Laurita"]
9
+ s.email = ["francesco.laurita@gmail.com"]
10
+ s.homepage = "https://github.com/taganaka/redis-queue"
11
+ s.summary = %q{A distributed queue based on Redis}
12
+ s.description = %q{
13
+ Adds Redis::Queue class which can be used as Distributed-Queue based on Redis.
14
+ Redis is often used as a messaging server to implement processing of background jobs or other kinds of messaging tasks.
15
+ It implements Reliable-queue pattern decribed here: http://redis.io/commands/rpoplpush
16
+ }
17
+
18
+ s.rubyforge_project = "redis-queue"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+
25
+ s.add_dependency "redis", "~> 3.0.4"
26
+ s.add_dependency "hiredis", "~> 0.4.5"
27
+
28
+ s.add_development_dependency "rspec", "~> 2.13.0"
29
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe Redis::Queue do
4
+ before(:all) do
5
+ @redis = Redis.new
6
+ @queue = Redis::Queue.new('__test', 'bp__test')
7
+ @queue.clear true
8
+ end
9
+
10
+ after(:all) do
11
+ @queue.clear true
12
+ end
13
+
14
+ it 'should return correct version string' do
15
+ Redis::Queue.version.should == "redis-queue version #{Redis::Queue::VERSION}"
16
+ end
17
+
18
+ it 'should create a new redis-queue object' do
19
+ queue = Redis::Queue.new('__test', 'bp__test')
20
+ queue.class.should == Redis::Queue
21
+ end
22
+
23
+ it 'should add an element to the queue' do
24
+ @queue << "a"
25
+ @queue.size.should be == 1
26
+ end
27
+
28
+ it 'should return an element from the queue' do
29
+ message = @queue.pop(true)
30
+ message.should be == "a"
31
+ end
32
+
33
+ it 'should remove the element from bp_queue if commit is called' do
34
+ @redis.llen('bp__test').should be == 1
35
+ @queue.commit
36
+ @redis.llen('bp__test').should be == 0
37
+ end
38
+
39
+ it 'should implements fifo pattern' do
40
+ @queue.clear
41
+ payload = %w(a b c d e)
42
+ payload.each {|e| @queue << e}
43
+ test = []
44
+ while e=@queue.pop(true)
45
+ test << e
46
+ end
47
+ payload.should be == test
48
+ end
49
+
50
+ it 'should remove all of the elements from the main queue' do
51
+ %w(a b c d e).each {|e| @queue << e}
52
+ @queue.size.should be > 0
53
+ @queue.pop(true)
54
+ @queue.clear
55
+ @redis.llen('bp__test').should be > 0
56
+ end
57
+
58
+ it 'should reset queues content' do
59
+ @queue.clear(true)
60
+ @redis.llen('bp__test').should be == 0
61
+ end
62
+
63
+ it 'should prcess a message' do
64
+ @queue << "a"
65
+ @queue.process(true){|m|m.should be == "a"; true}
66
+ end
67
+
68
+ it 'should prcess a message leaving it into the bp_queue' do
69
+ @queue << "a"
70
+ @queue << "a"
71
+ @queue.process(true){|m|m.should be == "a"; false}
72
+ @redis.lrange('bp__test',0, -1).should be == ['a', 'a']
73
+ end
74
+
75
+ it 'should refill a main queue' do
76
+ @queue.clear(true)
77
+ @queue << "a"
78
+ @queue << "a"
79
+ @queue.process(true){|m|m.should be == "a"; false}
80
+ @redis.lrange('bp__test',0, -1).should be == ['a', 'a']
81
+ @queue.refill
82
+ @redis.lrange('__test',0, -1).should be == ['a', 'a']
83
+ @redis.llen('bp__test').should be == 0
84
+ end
85
+
86
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'rspec'
4
+
5
+
6
+ RSpec.configure do |config|
7
+ config.color_enabled = true
8
+ end
9
+
10
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
11
+ require 'redis-queue'
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis-queue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Francesco Laurita
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.4
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.4
30
+ - !ruby/object:Gem::Dependency
31
+ name: hiredis
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.4.5
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.4.5
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 2.13.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.13.0
62
+ description: ! "\n Adds Redis::Queue class which can be used as Distributed-Queue
63
+ based on Redis.\n Redis is often used as a messaging server to implement processing
64
+ of background jobs or other kinds of messaging tasks.\n It implements Reliable-queue
65
+ pattern decribed here: http://redis.io/commands/rpoplpush\n "
66
+ email:
67
+ - francesco.laurita@gmail.com
68
+ executables: []
69
+ extensions: []
70
+ extra_rdoc_files: []
71
+ files:
72
+ - .gitignore
73
+ - Gemfile
74
+ - LICENSE.txt
75
+ - README.md
76
+ - Rakefile
77
+ - lib/redis-queue.rb
78
+ - lib/redis/queue.rb
79
+ - redis-queue.gemspec
80
+ - spec/redis_queue_spec.rb
81
+ - spec/spec_helper.rb
82
+ homepage: https://github.com/taganaka/redis-queue
83
+ licenses: []
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project: redis-queue
102
+ rubygems_version: 1.8.25
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: A distributed queue based on Redis
106
+ test_files:
107
+ - spec/redis_queue_spec.rb
108
+ - spec/spec_helper.rb