ost 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README.markdown +109 -0
- data/Rakefile +5 -0
- data/lib/ost.rb +68 -0
- data/ost.gemspec +10 -0
- data/test/ost_test.rb +77 -0
- data/test/test_helper.rb +3 -0
- metadata +69 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 Michel Martens
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
Ost
|
2
|
+
===
|
3
|
+
|
4
|
+
Redis based queues and workers.
|
5
|
+
|
6
|
+
Description
|
7
|
+
-----------
|
8
|
+
|
9
|
+
**Ost** makes it easy to enqueue object ids and process them with workers.
|
10
|
+
|
11
|
+
Say you want to process video uploads. In your application you will have something like this:
|
12
|
+
|
13
|
+
Ost[:videos_to_process].push(@video.id)
|
14
|
+
|
15
|
+
Then, you will have a worker that will look like this:
|
16
|
+
|
17
|
+
require "ost"
|
18
|
+
|
19
|
+
Ost[:videos_to_process].each do |id|
|
20
|
+
# Do something with it!
|
21
|
+
end
|
22
|
+
|
23
|
+
Usage
|
24
|
+
-----
|
25
|
+
|
26
|
+
**Ost** connects to Redis automatically with the default options (localhost:6379, database 0).
|
27
|
+
|
28
|
+
You can customize the connection by calling `connect`:
|
29
|
+
|
30
|
+
Ost.connect port: 6380, db: 2
|
31
|
+
|
32
|
+
Then you only need to refer to a queue for it to pop into existence:
|
33
|
+
|
34
|
+
Ost[:rss_feeds] << @feed.id
|
35
|
+
|
36
|
+
A worker is a Ruby file with this basic code:
|
37
|
+
|
38
|
+
require "ost"
|
39
|
+
|
40
|
+
Ost[:rss_feeds].each do |id|
|
41
|
+
...
|
42
|
+
end
|
43
|
+
|
44
|
+
It will pop items from the queue with a timeout of two seconds and retry indefinitely. If you want to configure the timeout, set the environment variable `OST_TIMEOUT`.
|
45
|
+
|
46
|
+
Available methods for a queue are `push` (aliased to `<<`) and `pop` (aliased to `each`).
|
47
|
+
|
48
|
+
Note that in these examples we are pushing numbers to the queue. As we have unlimited queues, each queue should be specialized and the workers must be smart enough to know what to do with the numbers they pop.
|
49
|
+
|
50
|
+
Failures
|
51
|
+
========
|
52
|
+
|
53
|
+
If the block raises an error, it is captured by **Ost** and the exception is logged in Redis.
|
54
|
+
|
55
|
+
Consider this example:
|
56
|
+
|
57
|
+
require "ost"
|
58
|
+
|
59
|
+
Ost[:rss_feeds].each do |id|
|
60
|
+
...
|
61
|
+
raise "Invalid format"
|
62
|
+
end
|
63
|
+
|
64
|
+
Then, in the console you can do:
|
65
|
+
|
66
|
+
>> Ost[:rss_feeds].push 1
|
67
|
+
=> 1
|
68
|
+
|
69
|
+
>> Ost[:rss_feeds].errors
|
70
|
+
=> ["2010-04-12 21:57:23 -0300 ost:rss_feeds:1 => #<RuntimeError: Invalid format>"]
|
71
|
+
|
72
|
+
Differences with Delayed::Job and Resque
|
73
|
+
--------------------------------------
|
74
|
+
|
75
|
+
Both [Delayed::Job](http://github.com/tobi/delayed_job) and [Resque](http://github.com/defunkt/resque)
|
76
|
+
provide queues and workers (the later using Redis). They provide dumb workers that process jobs, which are specialized for each task. The specialization takes place in the application side, and the job is serialized and pushed into a queue.
|
77
|
+
|
78
|
+
**Ost**, by contrast, just pushes numbers into specialized queues, and uses workers that are subscribed to specific queues and know what to do with the items they get. The total sum of logic is almost the same.
|
79
|
+
|
80
|
+
Installation
|
81
|
+
------------
|
82
|
+
|
83
|
+
$ sudo gem install ost
|
84
|
+
|
85
|
+
License
|
86
|
+
-------
|
87
|
+
|
88
|
+
Copyright (c) 2010 Michel Martens
|
89
|
+
|
90
|
+
Permission is hereby granted, free of charge, to any person
|
91
|
+
obtaining a copy of this software and associated documentation
|
92
|
+
files (the "Software"), to deal in the Software without
|
93
|
+
restriction, including without limitation the rights to use,
|
94
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
95
|
+
copies of the Software, and to permit persons to whom the
|
96
|
+
Software is furnished to do so, subject to the following
|
97
|
+
conditions:
|
98
|
+
|
99
|
+
The above copyright notice and this permission notice shall be
|
100
|
+
included in all copies or substantial portions of the Software.
|
101
|
+
|
102
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
103
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
104
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
105
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
106
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
107
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
108
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
109
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/lib/ost.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require "redis"
|
2
|
+
require "nest"
|
3
|
+
|
4
|
+
module Ost
|
5
|
+
VERSION = "0.0.1"
|
6
|
+
TIMEOUT = ENV["OST_TIMEOUT"] || 2
|
7
|
+
|
8
|
+
class Queue
|
9
|
+
attr :ns
|
10
|
+
|
11
|
+
def initialize(name)
|
12
|
+
@ns = Nest.new(:ost)[name]
|
13
|
+
end
|
14
|
+
|
15
|
+
def push(value)
|
16
|
+
redis.lpush(ns, value)
|
17
|
+
end
|
18
|
+
|
19
|
+
def each(&block)
|
20
|
+
loop do
|
21
|
+
_, item = redis.brpop(ns, TIMEOUT)
|
22
|
+
next if item.nil? or item.empty?
|
23
|
+
|
24
|
+
begin
|
25
|
+
block.call(item)
|
26
|
+
rescue Exception => e
|
27
|
+
error = "#{Time.now} #{ns[item]} => #{e.inspect}"
|
28
|
+
|
29
|
+
redis.rpush ns[:errors], error
|
30
|
+
redis.publish ns[:errors], error
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def errors
|
36
|
+
redis.lrange ns[:errors], 0, -1
|
37
|
+
end
|
38
|
+
|
39
|
+
alias << push
|
40
|
+
alias pop each
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def redis
|
45
|
+
Ost.redis
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
@queues = Hash.new do |hash, key|
|
50
|
+
hash[key] = Queue.new(key)
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.[](queue)
|
54
|
+
@queues[queue]
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.connect(options = {})
|
58
|
+
@redis = Redis.new(options)
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.redis
|
62
|
+
@redis ||= Redis.new
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.redis=(redis)
|
66
|
+
@redis = redis
|
67
|
+
end
|
68
|
+
end
|
data/ost.gemspec
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "ost"
|
3
|
+
s.version = "0.0.1"
|
4
|
+
s.summary = "Redis based queues and workers."
|
5
|
+
s.description = "Ost lets you manage queues and workers with Redis."
|
6
|
+
s.authors = ["Michel Martens"]
|
7
|
+
s.email = ["michel@soveran.com"]
|
8
|
+
s.homepage = "http://github.com/soveran/ost"
|
9
|
+
s.files = ["LICENSE", "README.markdown", "Rakefile", "lib/ost.rb", "ost.gemspec", "test/ost_test.rb", "test/test_helper.rb"]
|
10
|
+
end
|
data/test/ost_test.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
+
|
3
|
+
class TestOst < Test::Unit::TestCase
|
4
|
+
def ost(&job)
|
5
|
+
thread = Thread.new do
|
6
|
+
Ost[:events].each(&job)
|
7
|
+
end
|
8
|
+
|
9
|
+
sleep 0.2
|
10
|
+
|
11
|
+
thread.kill
|
12
|
+
end
|
13
|
+
|
14
|
+
setup do
|
15
|
+
@redis = Redis.new
|
16
|
+
@redis.flushdb
|
17
|
+
|
18
|
+
Ost.connect
|
19
|
+
Ost[:events].push(1)
|
20
|
+
end
|
21
|
+
|
22
|
+
teardown do
|
23
|
+
Ost.redis.flushdb
|
24
|
+
end
|
25
|
+
|
26
|
+
should "insert items in the queue" do
|
27
|
+
assert_equal ["1"], @redis.lrange("ost:events", 0, -1)
|
28
|
+
end
|
29
|
+
|
30
|
+
should "process items from the queue" do
|
31
|
+
@results = []
|
32
|
+
|
33
|
+
ost do |item|
|
34
|
+
@results << item
|
35
|
+
end
|
36
|
+
|
37
|
+
assert_equal [], @redis.lrange("ost:events", 0, -1)
|
38
|
+
assert_equal ["1"], @results
|
39
|
+
end
|
40
|
+
|
41
|
+
should "add failures to a special list" do
|
42
|
+
ost do |item|
|
43
|
+
raise "Wrong answer"
|
44
|
+
end
|
45
|
+
|
46
|
+
assert_equal 0, @redis.llen("ost:events")
|
47
|
+
assert_equal 1, @redis.llen("ost:events:errors")
|
48
|
+
|
49
|
+
assert_match /ost:events:1 => #<RuntimeError: Wrong answer/, @redis.rpop("ost:events:errors")
|
50
|
+
end
|
51
|
+
|
52
|
+
should "publish the error to a specific channel" do
|
53
|
+
@results = []
|
54
|
+
|
55
|
+
t1 = Thread.new do
|
56
|
+
@redis.subscribe("ost:events:errors") do |on|
|
57
|
+
on.message do |channel, message|
|
58
|
+
if message[/ost:events:1 => #<RuntimeError: Wrong answer/]
|
59
|
+
@results << message
|
60
|
+
@redis.unsubscribe
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
ost do |item|
|
67
|
+
raise "Wrong answer"
|
68
|
+
end
|
69
|
+
|
70
|
+
t1.kill
|
71
|
+
|
72
|
+
assert_equal 0, @redis.llen("ost:events")
|
73
|
+
assert_equal 1, @redis.llen("ost:events:errors")
|
74
|
+
|
75
|
+
assert_match /ost:events:1 => #<RuntimeError: Wrong answer/, @redis.rpop("ost:events:errors")
|
76
|
+
end
|
77
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ost
|
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
|
+
- Michel Martens
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-13 00:00:00 -03:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Ost lets you manage queues and workers with Redis.
|
22
|
+
email:
|
23
|
+
- michel@soveran.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- LICENSE
|
32
|
+
- README.markdown
|
33
|
+
- Rakefile
|
34
|
+
- lib/ost.rb
|
35
|
+
- ost.gemspec
|
36
|
+
- test/ost_test.rb
|
37
|
+
- test/test_helper.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/soveran/ost
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.3.6
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Redis based queues and workers.
|
68
|
+
test_files: []
|
69
|
+
|