ost 0.1.6 → 1.0.0.rc1
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 +4 -4
- data/.gems +3 -0
- data/.gitignore +1 -0
- data/CHANGELOG.md +67 -0
- data/README.md +24 -36
- data/lib/ost.rb +23 -22
- data/makefile +4 -0
- data/ost.gemspec +6 -11
- data/test/access.rb +25 -0
- data/test/backup.rb +38 -0
- data/test/connection.rb +16 -0
- data/test/core.rb +17 -0
- data/test/helper.rb +8 -0
- data/test/stop.rb +22 -0
- metadata +39 -17
- data/Rakefile +0 -6
- data/test/ost_test.rb +0 -160
- data/test/test_helper.rb +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0c37dcb37d2a4a8083505a4cda90d414ae46ba4
|
4
|
+
data.tar.gz: 5fb22633c7b154355b487a764227b2566a5925a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f58ea6507fd16c105124a5fcfd5d8865b81154e23a8d2f111b8e92824e71cdeeb3ab0968d97c6bae5ef82f79e0eb6dbcde6ed054cd0bc52db677d7b0a4898bb5
|
7
|
+
data.tar.gz: 6e1184ac0c347eff90229108f384e561248b90636bb0f408e53eb21d0c4670863d5d2298c174077ffa618678f4bfeded253746a3c5347cb388113f849df4e6ec
|
data/.gems
ADDED
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
/pkg
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
## 1.0.0.rc1
|
2
|
+
|
3
|
+
* Use [redic][redic] instead of `redis-rb` as the Redis client. This changes
|
4
|
+
how to connect to a Redis database:
|
5
|
+
|
6
|
+
Before:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
Ost.connect(localhost: 6379, port: 6380, db: 2)
|
10
|
+
```
|
11
|
+
|
12
|
+
Now:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
Ost.redis = Redic.new("redis://127.0.0.1:6379/2")
|
16
|
+
```
|
17
|
+
|
18
|
+
## 0.1.6
|
19
|
+
|
20
|
+
* Fix bug with non-integer timeouts
|
21
|
+
|
22
|
+
* Add `Ost::Queue#size` method. Returns the current size of the queue.
|
23
|
+
|
24
|
+
Example:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
Ost[:events].size # => 0
|
28
|
+
Ost[:events].push(1)
|
29
|
+
Ost[:events].size # => 1
|
30
|
+
```
|
31
|
+
|
32
|
+
## 0.1.5
|
33
|
+
|
34
|
+
* Fix bug with redis connection handling. Previously, it was just using the default connection
|
35
|
+
of `localhost:6379`, regardless if you setup your app with an `Ost.connect`.
|
36
|
+
|
37
|
+
## 0.1.4
|
38
|
+
|
39
|
+
* No changes.
|
40
|
+
|
41
|
+
## 0.1.3
|
42
|
+
|
43
|
+
* Don't yield the block on timeouts.
|
44
|
+
|
45
|
+
## 0.1.2
|
46
|
+
|
47
|
+
* Change default timeout to 2 seconds.
|
48
|
+
|
49
|
+
## 0.1.1
|
50
|
+
|
51
|
+
* Use `backup.lpop` instead of `backup.del`.
|
52
|
+
|
53
|
+
## 0.1.0
|
54
|
+
|
55
|
+
* `Ost#each` no longer rescues exceptions for you.
|
56
|
+
|
57
|
+
You are in charge of rescuing and deciding what to do.
|
58
|
+
|
59
|
+
* You can inspect the status of the queue by calling `Ost::Queue#items`.
|
60
|
+
|
61
|
+
* If you need access to the underlying Redis key, it's in `Ost::Queue#key`.
|
62
|
+
|
63
|
+
* Ost now uses `BRPOPLPUSH` and maintains a backup queue while working.
|
64
|
+
|
65
|
+
You can access this queue using `Ost::Queue#backup`.
|
66
|
+
|
67
|
+
[redic]: https://github.com/amakawa/redic
|
data/README.md
CHANGED
@@ -31,18 +31,34 @@ end
|
|
31
31
|
Usage
|
32
32
|
-----
|
33
33
|
|
34
|
-
|
35
|
-
|
34
|
+
Ost uses a lightweight Redis client called [Redic][redic]. To connect to
|
35
|
+
a Redis database, you will need to set an instance of `Redic`, with a URL
|
36
|
+
of the form `redis://:<passwd>@<host>:<port>/<db>`.
|
36
37
|
|
37
|
-
You can customize the connection by calling `
|
38
|
+
You can customize the connection by calling `Ost.redis=`:
|
38
39
|
|
39
40
|
``` ruby
|
40
|
-
|
41
|
+
require "ost"
|
42
|
+
|
43
|
+
Ost.redis = Redic.new("redis://127.0.0.1:6379")
|
41
44
|
```
|
42
45
|
|
43
46
|
Then you only need to refer to a queue for it to pop into existence:
|
44
47
|
|
45
48
|
``` ruby
|
49
|
+
require "ost"
|
50
|
+
|
51
|
+
Ost.redis = Redic.new("redis://127.0.0.1:6379")
|
52
|
+
|
53
|
+
Ost[:rss_feeds] << @feed.id
|
54
|
+
```
|
55
|
+
|
56
|
+
Ost defaults to a Redic connection to `redis://127.0.0.1:6379`. The example
|
57
|
+
above could be rewritten as:
|
58
|
+
|
59
|
+
``` ruby
|
60
|
+
require "ost"
|
61
|
+
|
46
62
|
Ost[:rss_feeds] << @feed.id
|
47
63
|
```
|
48
64
|
|
@@ -57,7 +73,7 @@ end
|
|
57
73
|
```
|
58
74
|
|
59
75
|
It will pop items from the queue as soon as they become available. It
|
60
|
-
uses BRPOPLPUSH with a timeout that can be specified with the
|
76
|
+
uses `BRPOPLPUSH` with a timeout that can be specified with the
|
61
77
|
`OST_TIMEOUT` environment variable.
|
62
78
|
|
63
79
|
Note that in these examples we are pushing numbers to the queue. As
|
@@ -68,12 +84,6 @@ pop.
|
|
68
84
|
Available methods
|
69
85
|
=================
|
70
86
|
|
71
|
-
`Ost.connect`: configure the connection to Redis. By default, it
|
72
|
-
connects to localhost in port 6379 and uses the database 0. It accepts
|
73
|
-
the same options as [redis-rb](https://github.com/redis/redis-rb).
|
74
|
-
|
75
|
-
`Ost.stop`: halt processing for all queues.
|
76
|
-
|
77
87
|
`Ost[:example].push item`, `Ost[:some_queue] << item`: add `item` to
|
78
88
|
the `:example` queue.
|
79
89
|
|
@@ -81,6 +91,8 @@ the `:example` queue.
|
|
81
91
|
}`: consume `item` from the `:example` queue. If the block doesn't
|
82
92
|
complete successfully, the item will be left at a backup queue.
|
83
93
|
|
94
|
+
`Ost.stop`: halt processing for all queues.
|
95
|
+
|
84
96
|
`Ost[:example].stop`: halt processing for the `example` queue.
|
85
97
|
|
86
98
|
Failures
|
@@ -131,28 +143,4 @@ Installation
|
|
131
143
|
|
132
144
|
$ gem install ost
|
133
145
|
|
134
|
-
|
135
|
-
-------
|
136
|
-
|
137
|
-
Copyright (c) 2010, 2011, 2012 Michel Martens
|
138
|
-
|
139
|
-
Permission is hereby granted, free of charge, to any person
|
140
|
-
obtaining a copy of this software and associated documentation
|
141
|
-
files (the "Software"), to deal in the Software without
|
142
|
-
restriction, including without limitation the rights to use,
|
143
|
-
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
144
|
-
copies of the Software, and to permit persons to whom the
|
145
|
-
Software is furnished to do so, subject to the following
|
146
|
-
conditions:
|
147
|
-
|
148
|
-
The above copyright notice and this permission notice shall be
|
149
|
-
included in all copies or substantial portions of the Software.
|
150
|
-
|
151
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
152
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
153
|
-
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
154
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
155
|
-
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
156
|
-
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
157
|
-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
158
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
146
|
+
[redic]: https://github.com/amakawa/redic
|
data/lib/ost.rb
CHANGED
@@ -1,34 +1,33 @@
|
|
1
|
-
require "
|
1
|
+
require "nido"
|
2
|
+
require "redic"
|
2
3
|
|
3
4
|
module Ost
|
4
|
-
TIMEOUT =
|
5
|
+
TIMEOUT = ENV["OST_TIMEOUT"] || "2"
|
5
6
|
|
6
7
|
class Queue
|
7
8
|
attr :key
|
8
9
|
attr :backup
|
9
10
|
|
10
11
|
def initialize(name)
|
11
|
-
@key =
|
12
|
+
@key = Nido.new(:ost)[name]
|
12
13
|
@backup = @key[Socket.gethostname][Process.pid]
|
14
|
+
@stopping = false
|
13
15
|
end
|
14
16
|
|
15
17
|
def push(value)
|
16
|
-
|
18
|
+
redis.call("LPUSH", @key, value)
|
17
19
|
end
|
18
20
|
|
19
21
|
def each(&block)
|
20
|
-
@stopping = false
|
21
|
-
|
22
22
|
loop do
|
23
|
-
|
24
|
-
|
25
|
-
item = @key.brpoplpush(@backup, TIMEOUT)
|
23
|
+
item = redis.call("BRPOPLPUSH", @key, @backup, TIMEOUT)
|
26
24
|
|
27
|
-
|
25
|
+
if item
|
26
|
+
block.call(item)
|
27
|
+
redis.call("LPOP", @backup)
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
@backup.lpop
|
30
|
+
break if @stopping
|
32
31
|
end
|
33
32
|
end
|
34
33
|
|
@@ -37,18 +36,22 @@ module Ost
|
|
37
36
|
end
|
38
37
|
|
39
38
|
def items
|
40
|
-
|
39
|
+
redis.call("LRANGE", @key, 0, -1)
|
41
40
|
end
|
42
41
|
|
43
42
|
alias << push
|
44
43
|
alias pop each
|
45
44
|
|
46
45
|
def size
|
47
|
-
key
|
46
|
+
redis.call("LLEN", @key)
|
48
47
|
end
|
49
48
|
|
50
49
|
def redis
|
51
|
-
@redis
|
50
|
+
defined?(@redis) ? @redis : Ost.redis
|
51
|
+
end
|
52
|
+
|
53
|
+
def redis=(redis)
|
54
|
+
@redis = redis
|
52
55
|
end
|
53
56
|
end
|
54
57
|
|
@@ -64,13 +67,11 @@ module Ost
|
|
64
67
|
@queues.each { |_, queue| queue.stop }
|
65
68
|
end
|
66
69
|
|
67
|
-
|
68
|
-
|
69
|
-
def self.connect(options = {})
|
70
|
-
@options = options
|
70
|
+
def self.redis
|
71
|
+
@redis ||= Redic.new
|
71
72
|
end
|
72
73
|
|
73
|
-
def self.
|
74
|
-
@
|
74
|
+
def self.redis=(redis)
|
75
|
+
@redis = redis
|
75
76
|
end
|
76
77
|
end
|
data/makefile
ADDED
data/ost.gemspec
CHANGED
@@ -1,21 +1,16 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "ost"
|
3
|
-
s.version = "0.
|
3
|
+
s.version = "1.0.0.rc1"
|
4
4
|
s.summary = "Redis based queues and workers."
|
5
5
|
s.description = "Ost lets you manage queues and workers with Redis."
|
6
6
|
s.authors = ["Michel Martens"]
|
7
7
|
s.email = ["michel@soveran.com"]
|
8
8
|
s.homepage = "http://github.com/soveran/ost"
|
9
|
+
s.license = "MIT"
|
9
10
|
|
10
|
-
s.files =
|
11
|
-
"LICENSE",
|
12
|
-
"README.md",
|
13
|
-
"Rakefile",
|
14
|
-
"lib/**/*.rb",
|
15
|
-
"*.gemspec",
|
16
|
-
"test/*.*"
|
17
|
-
]
|
11
|
+
s.files = `git ls-files`.split("\n")
|
18
12
|
|
19
|
-
s.add_dependency "
|
20
|
-
s.
|
13
|
+
s.add_dependency "redic"
|
14
|
+
s.add_dependency "nido"
|
15
|
+
s.add_development_dependency "cutest"
|
21
16
|
end
|
data/test/access.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative "helper"
|
2
|
+
|
3
|
+
scope do
|
4
|
+
test "access to underlying key" do
|
5
|
+
assert_equal "ost:events", Ost[:events].key
|
6
|
+
end
|
7
|
+
|
8
|
+
test "access to queued items" do
|
9
|
+
Ost[:events].push(1)
|
10
|
+
|
11
|
+
assert_equal ["1"], Ost[:events].items
|
12
|
+
end
|
13
|
+
|
14
|
+
test "access to queue size" do
|
15
|
+
queue = Ost[:events]
|
16
|
+
assert_equal 0, queue.size
|
17
|
+
|
18
|
+
queue.push(1)
|
19
|
+
assert_equal 1, queue.size
|
20
|
+
|
21
|
+
queue.stop
|
22
|
+
queue.each { }
|
23
|
+
assert_equal 0, queue.size
|
24
|
+
end
|
25
|
+
end
|
data/test/backup.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require_relative "helper"
|
2
|
+
|
3
|
+
scope do
|
4
|
+
test "returns backup key" do
|
5
|
+
hostname = Socket.gethostname
|
6
|
+
pid = Process.pid
|
7
|
+
|
8
|
+
assert_equal "ost:events:#{hostname}:#{pid}", Ost[:events].backup
|
9
|
+
end
|
10
|
+
|
11
|
+
test "maintains a backup queue for when worker dies" do
|
12
|
+
queue = Ost[:events]
|
13
|
+
|
14
|
+
queue.push(1)
|
15
|
+
|
16
|
+
assert_equal 0, queue.redis.call("LLEN", queue.backup)
|
17
|
+
|
18
|
+
begin
|
19
|
+
queue.stop
|
20
|
+
queue.each { |item| item.some_error }
|
21
|
+
rescue
|
22
|
+
end
|
23
|
+
|
24
|
+
assert_equal ["1"], queue.redis.call("LRANGE", queue.backup, 0, -1)
|
25
|
+
end
|
26
|
+
|
27
|
+
test "cleans up the backup queue on success" do
|
28
|
+
queue = Ost[:events]
|
29
|
+
queue.push(1)
|
30
|
+
|
31
|
+
queue.stop
|
32
|
+
queue.each do |item|
|
33
|
+
assert_equal [item], queue.redis.call("LRANGE", queue.backup, 0, -1)
|
34
|
+
end
|
35
|
+
|
36
|
+
assert_equal [], queue.redis.call("LRANGE", queue.backup, 0, -1)
|
37
|
+
end
|
38
|
+
end
|
data/test/connection.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require_relative "helper"
|
2
|
+
|
3
|
+
scope do
|
4
|
+
test "inherits ost redis connection by default" do
|
5
|
+
queue = Ost[:events]
|
6
|
+
|
7
|
+
assert_equal Ost.redis.url, queue.redis.url
|
8
|
+
end
|
9
|
+
|
10
|
+
test "queue can define its own connection" do
|
11
|
+
queue = Ost[:people]
|
12
|
+
queue.redis = Redic.new("redis://localhost:6379/1")
|
13
|
+
|
14
|
+
assert Ost.redis.url != queue.redis.url
|
15
|
+
end
|
16
|
+
end
|
data/test/core.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative "helper"
|
2
|
+
|
3
|
+
scope do
|
4
|
+
test "process items from the queue" do
|
5
|
+
Ost[:events].push(1)
|
6
|
+
|
7
|
+
results = []
|
8
|
+
|
9
|
+
Ost[:events].stop
|
10
|
+
Ost[:events].each do |item|
|
11
|
+
results << item
|
12
|
+
end
|
13
|
+
|
14
|
+
assert_equal [], Ost[:events].items
|
15
|
+
assert_equal ["1"], results
|
16
|
+
end
|
17
|
+
end
|
data/test/helper.rb
ADDED
data/test/stop.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative "helper"
|
2
|
+
|
3
|
+
scope do
|
4
|
+
test "stop processing a queue" do
|
5
|
+
Ost[:events].push(1)
|
6
|
+
Ost[:events].stop
|
7
|
+
Ost[:events].each { }
|
8
|
+
|
9
|
+
assert(true)
|
10
|
+
end
|
11
|
+
|
12
|
+
test "stop processing all queues" do
|
13
|
+
Ost[:events].push(1)
|
14
|
+
Ost[:people].push(1)
|
15
|
+
|
16
|
+
Ost.stop
|
17
|
+
Ost[:events].each { }
|
18
|
+
Ost[:people].each { }
|
19
|
+
|
20
|
+
assert(true)
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,43 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ost
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0.rc1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michel Martens
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: redic
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: nido
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: cutest
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
|
-
- -
|
45
|
+
- - '>='
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
47
|
+
version: '0'
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
|
-
- -
|
52
|
+
- - '>='
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
54
|
+
version: '0'
|
41
55
|
description: Ost lets you manage queues and workers with Redis.
|
42
56
|
email:
|
43
57
|
- michel@soveran.com
|
@@ -45,15 +59,23 @@ executables: []
|
|
45
59
|
extensions: []
|
46
60
|
extra_rdoc_files: []
|
47
61
|
files:
|
62
|
+
- .gems
|
63
|
+
- .gitignore
|
64
|
+
- CHANGELOG.md
|
48
65
|
- LICENSE
|
49
66
|
- README.md
|
50
|
-
- Rakefile
|
51
67
|
- lib/ost.rb
|
68
|
+
- makefile
|
52
69
|
- ost.gemspec
|
53
|
-
- test/
|
54
|
-
- test/
|
70
|
+
- test/access.rb
|
71
|
+
- test/backup.rb
|
72
|
+
- test/connection.rb
|
73
|
+
- test/core.rb
|
74
|
+
- test/helper.rb
|
75
|
+
- test/stop.rb
|
55
76
|
homepage: http://github.com/soveran/ost
|
56
|
-
licenses:
|
77
|
+
licenses:
|
78
|
+
- MIT
|
57
79
|
metadata: {}
|
58
80
|
post_install_message:
|
59
81
|
rdoc_options: []
|
@@ -66,9 +88,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
88
|
version: '0'
|
67
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
90
|
requirements:
|
69
|
-
- - '
|
91
|
+
- - '>'
|
70
92
|
- !ruby/object:Gem::Version
|
71
|
-
version:
|
93
|
+
version: 1.3.1
|
72
94
|
requirements: []
|
73
95
|
rubyforge_project:
|
74
96
|
rubygems_version: 2.0.3
|
data/Rakefile
DELETED
data/test/ost_test.rb
DELETED
@@ -1,160 +0,0 @@
|
|
1
|
-
ENV["OST_TIMEOUT"] = "1"
|
2
|
-
|
3
|
-
require File.expand_path("test_helper", File.dirname(__FILE__))
|
4
|
-
|
5
|
-
scope do
|
6
|
-
def ost(&job)
|
7
|
-
thread = Thread.new do
|
8
|
-
Ost[:events].each do |item|
|
9
|
-
begin
|
10
|
-
yield(item)
|
11
|
-
ensure
|
12
|
-
thread.kill
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
thread.join
|
18
|
-
end
|
19
|
-
|
20
|
-
def enqueue(id)
|
21
|
-
Ost[:events].push(id)
|
22
|
-
end
|
23
|
-
|
24
|
-
prepare do
|
25
|
-
Redis.current.flushall
|
26
|
-
end
|
27
|
-
|
28
|
-
setup do
|
29
|
-
Ost[:events].redis.quit
|
30
|
-
Redis.new
|
31
|
-
end
|
32
|
-
|
33
|
-
test "allows access to the queued items" do
|
34
|
-
enqueue(1)
|
35
|
-
|
36
|
-
assert_equal ["1"], Ost[:events].items
|
37
|
-
end
|
38
|
-
|
39
|
-
test "allows access to the underlying key" do
|
40
|
-
assert_equal 0, Ost[:events].key.llen
|
41
|
-
end
|
42
|
-
|
43
|
-
test "process items from the queue" do |redis|
|
44
|
-
enqueue(1)
|
45
|
-
|
46
|
-
results = []
|
47
|
-
|
48
|
-
ost do |item|
|
49
|
-
results << item
|
50
|
-
end
|
51
|
-
|
52
|
-
assert_equal [], Ost[:events].items
|
53
|
-
assert_equal ["1"], results
|
54
|
-
end
|
55
|
-
|
56
|
-
test "doesn't yield the block on timeout" do |redis|
|
57
|
-
results = []
|
58
|
-
|
59
|
-
Thread.new do
|
60
|
-
sleep 2
|
61
|
-
redis.lpush(Ost[:events].key, 1)
|
62
|
-
end
|
63
|
-
|
64
|
-
ost do |item|
|
65
|
-
results << item
|
66
|
-
end
|
67
|
-
|
68
|
-
assert_equal [], Ost[:events].items
|
69
|
-
assert_equal ["1"], results
|
70
|
-
end
|
71
|
-
|
72
|
-
test "halt processing a queue" do
|
73
|
-
Thread.new do
|
74
|
-
sleep 0.5
|
75
|
-
Ost[:always_empty].stop
|
76
|
-
end
|
77
|
-
|
78
|
-
Ost[:always_empty].each { }
|
79
|
-
|
80
|
-
assert true
|
81
|
-
end
|
82
|
-
|
83
|
-
test "halt processing all queues" do
|
84
|
-
Thread.new do
|
85
|
-
sleep 0.5
|
86
|
-
Ost.stop
|
87
|
-
end
|
88
|
-
|
89
|
-
t1 = Thread.new { Ost[:always_empty].each { } }
|
90
|
-
t2 = Thread.new { Ost[:always_empty_too].each { } }
|
91
|
-
|
92
|
-
t1.join
|
93
|
-
t2.join
|
94
|
-
|
95
|
-
assert true
|
96
|
-
end
|
97
|
-
|
98
|
-
test "maintains a backup queue for when worker dies" do
|
99
|
-
enqueue(1)
|
100
|
-
|
101
|
-
assert_equal 0, Ost[:events].backup.llen
|
102
|
-
|
103
|
-
begin
|
104
|
-
Ost[:events].each do |item|
|
105
|
-
item.some_error
|
106
|
-
end
|
107
|
-
rescue
|
108
|
-
end
|
109
|
-
|
110
|
-
assert_equal ["1"], Ost[:events].backup.lrange(0, -1)
|
111
|
-
end
|
112
|
-
|
113
|
-
test "cleans up the backup queue on success" do
|
114
|
-
enqueue(1)
|
115
|
-
|
116
|
-
done = false
|
117
|
-
|
118
|
-
Thread.new do
|
119
|
-
Ost[:events].each do |item|
|
120
|
-
assert_equal ["1"], Ost[:events].backup.lrange(0, -1)
|
121
|
-
done = true
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
until done; end
|
126
|
-
|
127
|
-
Ost[:events].stop
|
128
|
-
|
129
|
-
assert_equal 0, Ost[:events].backup.llen
|
130
|
-
assert_equal false, Ost[:events].backup.exists
|
131
|
-
end
|
132
|
-
|
133
|
-
test "uses same redis instance" do
|
134
|
-
queue = Ost['foo']
|
135
|
-
|
136
|
-
assert_equal queue.key.redis, queue.redis
|
137
|
-
end
|
138
|
-
|
139
|
-
test "report the queue size" do
|
140
|
-
queue = Ost[:size]
|
141
|
-
|
142
|
-
assert_equal 0, queue.size
|
143
|
-
|
144
|
-
queue << 1
|
145
|
-
|
146
|
-
assert_equal 1, queue.size
|
147
|
-
|
148
|
-
done = false
|
149
|
-
|
150
|
-
Thread.new do
|
151
|
-
Ost[:size].each do |item|
|
152
|
-
done = true
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
until done; end
|
157
|
-
|
158
|
-
assert_equal 0, queue.size
|
159
|
-
end
|
160
|
-
end
|