mloughran-job_queue 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.markdown +58 -0
- data/Rakefile +57 -0
- data/lib/job_queue/adapters/amqp_adapter.rb +28 -0
- data/lib/job_queue/adapters/beanstalk_adapter.rb +24 -0
- data/lib/job_queue/adapters/test_adapter.rb +20 -0
- data/lib/job_queue/adapters/verbose_adapter.rb +19 -0
- data/lib/job_queue/job_queue.rb +52 -0
- data/lib/job_queue.rb +7 -0
- data/spec/amqp_adapter_spec.rb +8 -0
- data/spec/beanstalk_adapter_spec.rb +48 -0
- data/spec/job_queue_spec.rb +5 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/test_adapter_spec.rb +39 -0
- data/spec/verbose_adapter_spec.rb +15 -0
- metadata +70 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Martyn Loughran
|
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.markdown
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
JobQueue
|
2
|
+
========
|
3
|
+
|
4
|
+
`job_queue` allows you to use lots of message queues with exactly the same interface so you don't need to worry about which queue to pick :)
|
5
|
+
|
6
|
+
This should get you started:
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'job_queue'
|
10
|
+
|
11
|
+
Before you can do anything you must specify an adapter to use
|
12
|
+
|
13
|
+
JobQueue.adapter = JobQueue::BeanstalkAdapter.new
|
14
|
+
|
15
|
+
Jobs can then be simply added to the queue
|
16
|
+
|
17
|
+
JobQueue.put("flubble bubble")
|
18
|
+
|
19
|
+
In your workers you'll want to subscribe to a queue
|
20
|
+
|
21
|
+
JobQueue.subscribe do |job|
|
22
|
+
puts job
|
23
|
+
end
|
24
|
+
|
25
|
+
This subscribe block takes care of waiting for the next job to arrive and the block is passed exactly what you passed in. If you want to exit the loop just throw :stop.
|
26
|
+
|
27
|
+
JobQueue.subscribe do |job|
|
28
|
+
# Wait - I changed my mind!
|
29
|
+
throw :stop
|
30
|
+
end
|
31
|
+
|
32
|
+
What should you put on the queue
|
33
|
+
--------------------------------
|
34
|
+
|
35
|
+
You might love Ruby right now, but why lock yourself in? Often the kinds of things you use queues for are the kind of things you'll want to optimize. This is a good place to start:
|
36
|
+
|
37
|
+
JSON.generate({:some => "hash"})
|
38
|
+
JSON.parse(job)
|
39
|
+
|
40
|
+
Can you show me a nice processing daemon?
|
41
|
+
-----------------------------------------
|
42
|
+
|
43
|
+
Yes. Just a minute...
|
44
|
+
|
45
|
+
Adapters
|
46
|
+
========
|
47
|
+
|
48
|
+
Take your pick! Right now we have:
|
49
|
+
|
50
|
+
Beanstalk
|
51
|
+
---------
|
52
|
+
<http://xph.us/software/beanstalkd/>
|
53
|
+
|
54
|
+
AMQP
|
55
|
+
----
|
56
|
+
<http://github.com/tmm1/amqp/>
|
57
|
+
|
58
|
+
You need to run all your code within an eventmachine loop to use AMQP.
|
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rubygems/specification'
|
4
|
+
require 'date'
|
5
|
+
require 'spec/rake/spectask'
|
6
|
+
|
7
|
+
GEM = "job_queue"
|
8
|
+
GEM_VERSION = "0.0.3"
|
9
|
+
AUTHOR = "Martyn Loughran"
|
10
|
+
EMAIL = "me@mloughran.com"
|
11
|
+
HOMEPAGE = "http://github.com/mloughran/job_queue/"
|
12
|
+
SUMMARY = "JobQueue means you don't have to worry about your queue any more!"
|
13
|
+
|
14
|
+
spec = Gem::Specification.new do |s|
|
15
|
+
s.name = GEM
|
16
|
+
s.version = GEM_VERSION
|
17
|
+
s.platform = Gem::Platform::RUBY
|
18
|
+
s.has_rdoc = true
|
19
|
+
s.extra_rdoc_files = ["README.markdown", "LICENSE"]
|
20
|
+
s.summary = SUMMARY
|
21
|
+
s.description = s.summary
|
22
|
+
s.author = AUTHOR
|
23
|
+
s.email = EMAIL
|
24
|
+
s.homepage = HOMEPAGE
|
25
|
+
|
26
|
+
# Uncomment this to add a dependency
|
27
|
+
# s.add_dependency "foo"
|
28
|
+
|
29
|
+
s.require_path = 'lib'
|
30
|
+
s.autorequire = GEM
|
31
|
+
s.files = %w(LICENSE README.markdown Rakefile) + Dir.glob("{lib,spec}/**/*")
|
32
|
+
end
|
33
|
+
|
34
|
+
task :default => :spec
|
35
|
+
|
36
|
+
desc "Run specs"
|
37
|
+
Spec::Rake::SpecTask.new do |t|
|
38
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
39
|
+
t.spec_opts = %w(-fs --color)
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
44
|
+
pkg.gem_spec = spec
|
45
|
+
end
|
46
|
+
|
47
|
+
desc "install the gem locally"
|
48
|
+
task :install => [:package] do
|
49
|
+
sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
|
50
|
+
end
|
51
|
+
|
52
|
+
desc "create a gemspec file"
|
53
|
+
task :make_spec do
|
54
|
+
File.open("#{GEM}.gemspec", "w") do |file|
|
55
|
+
file.puts spec.to_ruby
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'mq'
|
2
|
+
|
3
|
+
class JobQueue::AMQPAdapter
|
4
|
+
def initialize
|
5
|
+
amq = MQ.new
|
6
|
+
@exchange = amq.direct('photo', :durable => true)
|
7
|
+
@queue = amq.queue('photo_worker', :durable => true)
|
8
|
+
@queue.bind(@exchange)
|
9
|
+
end
|
10
|
+
|
11
|
+
def put(string)
|
12
|
+
@queue.publish(string, :persistent => true)
|
13
|
+
end
|
14
|
+
|
15
|
+
def subscribe(error_report, &block)
|
16
|
+
EM.add_periodic_timer(0) do
|
17
|
+
begin
|
18
|
+
@queue.pop do |header, body|
|
19
|
+
next unless body
|
20
|
+
JobQueue.logger.info "AMQP received #{body}"
|
21
|
+
yield body
|
22
|
+
end
|
23
|
+
rescue => e
|
24
|
+
error_report.call(job.body, e)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'beanstalk-client'
|
2
|
+
|
3
|
+
class JobQueue::BeanstalkAdapter
|
4
|
+
def initialize
|
5
|
+
@beanstalk = Beanstalk::Pool.new(['localhost:11300'])
|
6
|
+
end
|
7
|
+
|
8
|
+
def put(string)
|
9
|
+
@beanstalk.put(string)
|
10
|
+
end
|
11
|
+
|
12
|
+
def subscribe(error_report, &block)
|
13
|
+
loop do
|
14
|
+
begin
|
15
|
+
job = @beanstalk.reserve
|
16
|
+
JobQueue.logger.info "Beanstalk received #{job.body}"
|
17
|
+
yield job.body
|
18
|
+
job.delete
|
19
|
+
rescue => e
|
20
|
+
error_report.call(job.body, e)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class JobQueue::TestAdapter
|
2
|
+
def initialize
|
3
|
+
@queue = []
|
4
|
+
end
|
5
|
+
|
6
|
+
def put(string)
|
7
|
+
@queue << string
|
8
|
+
end
|
9
|
+
|
10
|
+
def subscribe(error_report, &block)
|
11
|
+
loop do
|
12
|
+
begin
|
13
|
+
sleep 0.1 if @queue.empty?
|
14
|
+
yield @queue.shift
|
15
|
+
rescue
|
16
|
+
error_report.call(job.body, e)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# This isn't a queue at all, it just writes to standard output.
|
2
|
+
#
|
3
|
+
# It might be useful for testing.
|
4
|
+
#
|
5
|
+
class JobQueue::VerboseAdapter
|
6
|
+
def initialize
|
7
|
+
|
8
|
+
end
|
9
|
+
|
10
|
+
def put(string)
|
11
|
+
JobQueue.logger.debug "===== NEW JOB ADDED TO QUEUE ===="
|
12
|
+
JobQueue.logger.debug string
|
13
|
+
JobQueue.logger.debug "===== END OF MESSAGE ============"
|
14
|
+
end
|
15
|
+
|
16
|
+
def subscribe(error_report, &block)
|
17
|
+
raise "Not implemented. Use a better adapter!!"
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# JobQueue abstracts the task of adding work to a queue.
|
2
|
+
#
|
3
|
+
# Beanstalk is fantastic, but maybe not "enterprise grade".
|
4
|
+
#
|
5
|
+
# AMQP is fantastic, but it's bloody complex and has to run inside an
|
6
|
+
# eventmachine loop.
|
7
|
+
#
|
8
|
+
# Take your pick!
|
9
|
+
#
|
10
|
+
# Before use, an adapter must be chosen:
|
11
|
+
#
|
12
|
+
# JobQueue.adapter = JobQueue::BeanstalkAdapter.new
|
13
|
+
#
|
14
|
+
# Jobs can then be simply added to the queue with
|
15
|
+
#
|
16
|
+
# JobQueue.put("flubble bubble")
|
17
|
+
#
|
18
|
+
class JobQueue
|
19
|
+
class << self
|
20
|
+
attr_accessor :adapter
|
21
|
+
attr_accessor :logger
|
22
|
+
|
23
|
+
def logger
|
24
|
+
@logger ||= begin
|
25
|
+
logger = Logger.new(STDOUT)
|
26
|
+
logger.level = Logger::WARN
|
27
|
+
logger.debug("Created logger")
|
28
|
+
logger
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.put(string)
|
34
|
+
adapter.put(string)
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.subscribe(error_report = nil, &block)
|
38
|
+
catch :stop do
|
39
|
+
error_report ||= Proc.new do |job, e|
|
40
|
+
JobQueue.logger.error \
|
41
|
+
"Job failed\n" \
|
42
|
+
"==========\n" \
|
43
|
+
"Job content: #{job.inspect}\n" \
|
44
|
+
"Exception: #{e.message}\n" \
|
45
|
+
"#{e.backtrace.join("\n")}\n" \
|
46
|
+
"\n"
|
47
|
+
end
|
48
|
+
|
49
|
+
adapter.subscribe(error_report, &block)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/job_queue.rb
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'job_queue/job_queue'
|
3
|
+
|
4
|
+
JobQueue.autoload 'AMQPAdapter', 'job_queue/adapters/amqp_adapter'
|
5
|
+
JobQueue.autoload 'BeanstalkAdapter', 'job_queue/adapters/beanstalk_adapter'
|
6
|
+
JobQueue.autoload 'TestAdapter', 'job_queue/adapters/test_adapter'
|
7
|
+
JobQueue.autoload 'VerboseAdapter', 'job_queue/adapters/verbose_adapter'
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe JobQueue::BeanstalkAdapter do
|
4
|
+
before :all do
|
5
|
+
JobQueue.adapter = JobQueue::BeanstalkAdapter.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should write onto queue and fetch stuff back off" do
|
9
|
+
JobQueue.put("hello")
|
10
|
+
|
11
|
+
JobQueue.subscribe do |job|
|
12
|
+
@job = job
|
13
|
+
throw :stop
|
14
|
+
end
|
15
|
+
|
16
|
+
@job.should == "hello"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should output message if error raised in job" do
|
20
|
+
JobQueue.put("hello")
|
21
|
+
|
22
|
+
JobQueue.logger.should_receive(:error).with(/Job failed\w*/)
|
23
|
+
|
24
|
+
index = 0
|
25
|
+
JobQueue.subscribe do |job|
|
26
|
+
index +=1
|
27
|
+
raise 'foo' if index == 1
|
28
|
+
throw :stop
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should use error_report block if supplied" do
|
33
|
+
JobQueue.put("hello")
|
34
|
+
|
35
|
+
error_report = Proc.new do |job, e|
|
36
|
+
JobQueue.logger.error "Yikes that broke matey!"
|
37
|
+
end
|
38
|
+
|
39
|
+
JobQueue.logger.should_receive(:error).with("Yikes that broke matey!")
|
40
|
+
|
41
|
+
index = 0
|
42
|
+
JobQueue.subscribe(error_report) do |job|
|
43
|
+
index +=1
|
44
|
+
raise 'foo' if index == 1
|
45
|
+
throw :stop
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe JobQueue::TestAdapter do
|
4
|
+
before :all do
|
5
|
+
JobQueue.adapter = JobQueue::TestAdapter.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should write onto queue and fetch stuff back off" do
|
9
|
+
JobQueue.put("hello")
|
10
|
+
|
11
|
+
JobQueue.subscribe do |job|
|
12
|
+
@job = job
|
13
|
+
throw :stop
|
14
|
+
end
|
15
|
+
|
16
|
+
@job.should == "hello"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should pull items off in the order the were added" do
|
20
|
+
JobQueue.put("foo")
|
21
|
+
JobQueue.put("bar")
|
22
|
+
|
23
|
+
retrieved_jobs = []
|
24
|
+
|
25
|
+
begin
|
26
|
+
Timeout::timeout(0.5) do
|
27
|
+
JobQueue.subscribe do |job|
|
28
|
+
retrieved_jobs << job
|
29
|
+
end
|
30
|
+
end
|
31
|
+
rescue Timeout::Error
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
retrieved_jobs[0].should == "foo"
|
36
|
+
retrieved_jobs[1].should == "bar"
|
37
|
+
retrieved_jobs[2].should == nil
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe JobQueue::VerboseAdapter do
|
4
|
+
before :all do
|
5
|
+
JobQueue.adapter = JobQueue::VerboseAdapter.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should write onto queue and output a very verbose message to stdout" do
|
9
|
+
JobQueue.logger.should_receive(:debug).with("===== NEW JOB ADDED TO QUEUE ====")
|
10
|
+
JobQueue.logger.should_receive(:debug).with("hello")
|
11
|
+
JobQueue.logger.should_receive(:debug).with("===== END OF MESSAGE ============")
|
12
|
+
|
13
|
+
JobQueue.put("hello")
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mloughran-job_queue
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martyn Loughran
|
8
|
+
autorequire: job_queue
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-05 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: JobQueue means you don't have to worry about your queue any more!
|
17
|
+
email: me@mloughran.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.markdown
|
24
|
+
- LICENSE
|
25
|
+
files:
|
26
|
+
- LICENSE
|
27
|
+
- README.markdown
|
28
|
+
- Rakefile
|
29
|
+
- lib/job_queue
|
30
|
+
- lib/job_queue/adapters
|
31
|
+
- lib/job_queue/adapters/amqp_adapter.rb
|
32
|
+
- lib/job_queue/adapters/beanstalk_adapter.rb
|
33
|
+
- lib/job_queue/adapters/test_adapter.rb
|
34
|
+
- lib/job_queue/adapters/verbose_adapter.rb
|
35
|
+
- lib/job_queue/job_queue.rb
|
36
|
+
- lib/job_queue.rb
|
37
|
+
- spec/amqp_adapter_spec.rb
|
38
|
+
- spec/beanstalk_adapter_spec.rb
|
39
|
+
- spec/job_queue_spec.rb
|
40
|
+
- spec/spec_helper.rb
|
41
|
+
- spec/test_adapter_spec.rb
|
42
|
+
- spec/verbose_adapter_spec.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/mloughran/job_queue/
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.2.0
|
66
|
+
signing_key:
|
67
|
+
specification_version: 2
|
68
|
+
summary: JobQueue means you don't have to worry about your queue any more!
|
69
|
+
test_files: []
|
70
|
+
|