Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starting to work on the RedisSizedQueue
- Loading branch information
1 parent
9d737a5
commit a554f2a
Showing
17 changed files
with
483 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
data/mongodb | ||
data/redis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
source "https://rubygems.org" | ||
|
||
gem 'mongo' | ||
gem 'bson_ext' | ||
gem 'redis' | ||
|
||
group :test do | ||
gem 'debugger' | ||
gem 'foreman' | ||
gem 'minitest' | ||
gem 'turn' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
ansi (1.4.3) | ||
bson (1.7.0) | ||
bson_ext (1.7.0) | ||
bson (~> 1.7.0) | ||
columnize (0.3.6) | ||
debugger (1.2.0) | ||
columnize (>= 0.3.1) | ||
debugger-linecache (~> 1.1.1) | ||
debugger-ruby_core_source (~> 1.1.3) | ||
debugger-linecache (1.1.2) | ||
debugger-ruby_core_source (>= 1.1.1) | ||
debugger-ruby_core_source (1.1.3) | ||
foreman (0.56.0) | ||
thor (>= 0.13.6) | ||
minitest (3.4.0) | ||
mongo (1.7.0) | ||
bson (~> 1.7.0) | ||
redis (3.0.1) | ||
thor (0.16.0) | ||
turn (0.9.6) | ||
ansi | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
bson_ext | ||
debugger | ||
foreman | ||
minitest | ||
mongo | ||
redis | ||
turn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mongo: mongod run --config config/mongod.conf --rest | ||
redis: redis-server config/redis.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require 'rake/testtask' | ||
|
||
Rake::TestTask.new do |t| | ||
t.libs << "test" | ||
t.test_files = FileList['test/*_test.rb'] | ||
t.verbose = true | ||
end | ||
|
||
task :default => :test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Store data in /usr/local/var/mongodb instead of the default /data/db | ||
dbpath = data/mongodb | ||
|
||
# Only accept local connections | ||
bind_ip = 127.0.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
daemonize no | ||
port 6379 | ||
bind 127.0.0.1 | ||
logfile stdout | ||
loglevel warning | ||
databases 16 | ||
save 900 1 | ||
save 300 10 | ||
save 60 10000 | ||
dbfilename redis.rdb | ||
dir ./data/redis/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require_relative 'persistent-queue-classes/version' | ||
require_relative 'persistent-queue-classes/mongodb/queue' | ||
require_relative 'persistent-queue-classes/redis/queue' | ||
require_relative 'persistent-queue-classes/redis/sized_queue' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
begin | ||
require 'mongo' | ||
rescue LoadError | ||
warn "To use the `PersistentQueueClasses::Mongo::Queue` please ensure the `mongo` and `bson_ext` gems are installed and on the load path." | ||
exit 1 | ||
end | ||
|
||
module PersistentQueueClasses | ||
|
||
module MongoDB | ||
|
||
class Queue | ||
|
||
attr_reader :options | ||
|
||
def initialize(options={}) | ||
@options = default_options.merge(options) | ||
end | ||
|
||
private | ||
|
||
def default_options | ||
Hash.new | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
require 'base64' | ||
begin | ||
require 'redis' | ||
rescue LoadError | ||
warn "To use the `PersistentQueueClasses::Redis::Queue` please ensure the `redis` Gem is installed and on the load path." | ||
exit 1 | ||
end | ||
|
||
module PersistentQueueClasses | ||
|
||
module Redis | ||
|
||
class Queue | ||
|
||
attr_reader :options, :redis | ||
|
||
def initialize(options={}) | ||
@options = default_options.merge(options) | ||
end | ||
|
||
def length | ||
redis.llen(options[:queue_key_name]) || 0 | ||
end | ||
alias :size :length | ||
|
||
def empty? | ||
length == 0 | ||
end | ||
|
||
def num_waiting | ||
(redis.get(options[:waiting_key_name]) || 0).to_i | ||
end | ||
|
||
def push(object) | ||
redis.rpush options[:queue_key_name], Base64.encode64(Marshal.dump(object)) | ||
end | ||
alias :enq :push | ||
alias :<< :push | ||
|
||
def pop | ||
redis.incr options[:waiting_key_name] | ||
key, object = bredis.blpop(options[:queue_key_name]) | ||
Marshal.load(Base64.decode64(object)) | ||
ensure | ||
redis.decr options[:waiting_key_name] | ||
clear if empty? | ||
end | ||
alias :deq :pop | ||
alias :shift :pop | ||
|
||
def clear | ||
r = redis.multi do | ||
redis.del options[:queue_key_name] | ||
redis.del options[:waiting_key_name] | ||
end | ||
return [] | ||
end | ||
|
||
private | ||
|
||
def redis | ||
@redis ||= begin | ||
::Redis.new(options).tap do |r| | ||
r.setnx options[:waiting_key_name], 0 | ||
end | ||
end | ||
end | ||
|
||
def bredis | ||
@bredis ||= ::Redis.new(options) | ||
end | ||
|
||
def default_options | ||
{ | ||
queue_key_name: "persistent-queue-classes:redis:queue:#{self.hash.abs}:queue", | ||
waiting_key_name: "persistent-queue-classes:redis:queue:#{self.hash.abs}:waiting", | ||
} | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require_relative 'queue' | ||
|
||
module PersistentQueueClasses | ||
|
||
module Redis | ||
|
||
class SizedQueue < Queue | ||
|
||
attr_accessor :max | ||
|
||
def initialize(max, options={}) | ||
@max = max | ||
super default_options.merge(options) | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module PersistentQueueClasses | ||
|
||
VERSION = '1.0.0' | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
$:.unshift(File.dirname(__FILE__)) | ||
|
||
require 'rubygems' | ||
require 'debugger' | ||
require 'bundler/setup' | ||
require 'turn/autorun' | ||
require 'persistent-queue-classes' | ||
|
||
require_relative 'queue_tests' | ||
|
||
Thread.abort_on_exception = true | ||
|
||
Turn.config.tap do |t| | ||
t.ansi = true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'helper' | ||
|
||
module PersistentQueueClasses | ||
|
||
module MongoDB | ||
|
||
class QueueTest < MiniTest::Unit::TestCase | ||
|
||
#include QueueTests | ||
|
||
private | ||
|
||
def queue | ||
Queue.new | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |
Oops, something went wrong.