peons 0.0.1
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.
- data/Rakefile +7 -0
- data/lib/peons.rb +68 -0
- data/test/helper.rb +7 -0
- data/test/peons_test.rb +93 -0
- metadata +105 -0
data/Rakefile
ADDED
data/lib/peons.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require "redis"
|
2
|
+
require "nest"
|
3
|
+
|
4
|
+
module Peons
|
5
|
+
VERSION = "0.0.1"
|
6
|
+
|
7
|
+
class Queue < Nest
|
8
|
+
alias :push :lpush
|
9
|
+
|
10
|
+
def pop
|
11
|
+
return unless item = rpoplpush(backup)
|
12
|
+
|
13
|
+
begin
|
14
|
+
yield item
|
15
|
+
rescue Exception => e
|
16
|
+
fail(e, item)
|
17
|
+
ensure
|
18
|
+
backup.lrem 1, item
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def each
|
23
|
+
catch :empty do
|
24
|
+
loop do
|
25
|
+
pop! { |e| yield e }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def backup
|
31
|
+
self[:backup]
|
32
|
+
end
|
33
|
+
|
34
|
+
def errors
|
35
|
+
self[:errors]
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
def fail(exception, item)
|
40
|
+
error = "#{Time.now} #{item} => #{exception.inspect}"
|
41
|
+
|
42
|
+
errors.rpush(error)
|
43
|
+
errors.publish(error)
|
44
|
+
end
|
45
|
+
|
46
|
+
def pop!
|
47
|
+
throw(:empty) unless pop { |e| yield e }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.[](key)
|
52
|
+
Queue.new(key, redis)
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.redis
|
56
|
+
threaded[:redis] ||= Redis.new
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.redis=(redis)
|
60
|
+
threaded[:redis] = redis
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
def self.threaded
|
65
|
+
Thread.current[:peons] ||= {}
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
data/test/helper.rb
ADDED
data/test/peons_test.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
require File.expand_path("./helper", File.dirname(__FILE__))
|
2
|
+
|
3
|
+
setup do
|
4
|
+
Peons[:fortress]
|
5
|
+
end
|
6
|
+
|
7
|
+
test "adding elements to the queue" do |q|
|
8
|
+
q.push "1"
|
9
|
+
q.push "2"
|
10
|
+
|
11
|
+
assert ["2", "1"] == q.lrange(0, -1)
|
12
|
+
end
|
13
|
+
|
14
|
+
test "popping from an empty queue" do |q|
|
15
|
+
popped = nil
|
16
|
+
|
17
|
+
q.pop do |e|
|
18
|
+
popped = e
|
19
|
+
end
|
20
|
+
|
21
|
+
assert nil == popped
|
22
|
+
end
|
23
|
+
|
24
|
+
test "popping from a queue with one element" do |q|
|
25
|
+
popped = nil
|
26
|
+
|
27
|
+
q.push "1"
|
28
|
+
q.pop do |e|
|
29
|
+
popped = e
|
30
|
+
end
|
31
|
+
|
32
|
+
assert "1" == popped
|
33
|
+
assert 0 == q.backup.llen
|
34
|
+
end
|
35
|
+
|
36
|
+
test "popping from a queue with two distinct elements" do |q|
|
37
|
+
popped = nil
|
38
|
+
|
39
|
+
q.push "1"
|
40
|
+
q.push "2"
|
41
|
+
|
42
|
+
q.pop do |e|
|
43
|
+
popped = e
|
44
|
+
end
|
45
|
+
|
46
|
+
assert "1" == popped
|
47
|
+
assert 0 == q.backup.llen
|
48
|
+
end
|
49
|
+
|
50
|
+
test "popping from a queue with two equal elements" do |q|
|
51
|
+
popped = nil
|
52
|
+
|
53
|
+
q.push "1"
|
54
|
+
q.push "1"
|
55
|
+
|
56
|
+
q.pop do |e|
|
57
|
+
popped = e
|
58
|
+
end
|
59
|
+
|
60
|
+
assert "1" == popped
|
61
|
+
assert 0 == q.backup.llen
|
62
|
+
end
|
63
|
+
|
64
|
+
test "popping and producing an error inside the block" do |q|
|
65
|
+
q.push "1"
|
66
|
+
q.push "2"
|
67
|
+
|
68
|
+
q.pop do |e|
|
69
|
+
raise RuntimeError, "Fabrication"
|
70
|
+
end
|
71
|
+
|
72
|
+
assert 1 == q.llen
|
73
|
+
assert 0 == q.backup.llen
|
74
|
+
|
75
|
+
assert 1 == q.errors.llen
|
76
|
+
|
77
|
+
assert q.errors.lrange(0, -1).grep(/1 => #<RuntimeError: Fabrication>/).any?
|
78
|
+
end
|
79
|
+
|
80
|
+
test "looping against the queue" do |q|
|
81
|
+
q.push "1"
|
82
|
+
q.push "2"
|
83
|
+
q.push "3"
|
84
|
+
|
85
|
+
output = []
|
86
|
+
|
87
|
+
q.each do |e|
|
88
|
+
output << e
|
89
|
+
end
|
90
|
+
|
91
|
+
assert ["1", "2", "3"] == output
|
92
|
+
end
|
93
|
+
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: peons
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Cyril David
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-10-01 00:00:00 +08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: redis
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: nest
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
type: :runtime
|
45
|
+
version_requirements: *id002
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: cutest
|
48
|
+
prerelease: false
|
49
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id003
|
59
|
+
description:
|
60
|
+
email: cyx.ucron@gmail.com
|
61
|
+
executables: []
|
62
|
+
|
63
|
+
extensions: []
|
64
|
+
|
65
|
+
extra_rdoc_files: []
|
66
|
+
|
67
|
+
files:
|
68
|
+
- lib/peons.rb
|
69
|
+
- Rakefile
|
70
|
+
- test/helper.rb
|
71
|
+
- test/peons_test.rb
|
72
|
+
has_rdoc: true
|
73
|
+
homepage: http://github.com/cyx/peons
|
74
|
+
licenses: []
|
75
|
+
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
version: "0"
|
97
|
+
requirements: []
|
98
|
+
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 1.3.7
|
101
|
+
signing_key:
|
102
|
+
specification_version: 2
|
103
|
+
summary: Hard working queues on top of Redis
|
104
|
+
test_files: []
|
105
|
+
|