simplejob 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +21 -17
- data/VERSION +1 -1
- data/examples/chained_jobs/README.md +9 -0
- data/examples/chained_jobs/consumer.rb +35 -0
- data/examples/chained_jobs/producer.rb +5 -0
- data/lib/simplejob/client.rb +28 -2
- data/lib/simplejob/worker.rb +7 -2
- data/lib/simplejob.rb +3 -7
- data/simplejob.gemspec +70 -0
- metadata +7 -3
data/README.md
CHANGED
@@ -1,32 +1,37 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
simplejob
|
2
|
+
=========
|
3
3
|
|
4
4
|
Painless job queueing, backed by AMQP.
|
5
5
|
|
6
|
-
The design goals were:
|
7
|
-
|
8
6
|
* Super simple interface
|
9
7
|
* Robust jobs that never get lost
|
10
|
-
* Jobs don't get marked complete until they are finished (they survive reboots)
|
11
|
-
* One
|
12
|
-
* Jobs which throw exceptions are logged and removed, preventing infinite job loops
|
8
|
+
* Jobs don't get marked complete until they are finished (they survive reboots/kill -9)
|
9
|
+
* One work queue per worker pool, with one or more job names mapped to each work queue (for easy monitoring)
|
10
|
+
* Jobs which throw exceptions are logged and removed, preventing infinite job-retry loops
|
11
|
+
|
12
|
+
Installation
|
13
|
+
------------
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
<pre>
|
16
|
+
$ gem install simplejob
|
17
|
+
</pre>
|
18
|
+
|
19
|
+
Example
|
20
|
+
-------
|
16
21
|
|
17
22
|
Start your [RabbitMQ](http://www.rabbitmq.com/) server (install one if necessary).
|
18
23
|
|
19
|
-
|
20
|
-
producer.rb:
|
24
|
+
### Producer
|
21
25
|
|
26
|
+
<pre>
|
22
27
|
require "simplejob"
|
23
28
|
|
24
29
|
SimpleJob.send("hello")
|
25
30
|
</pre>
|
26
31
|
|
27
|
-
|
28
|
-
consumer.rb:
|
32
|
+
### Consumer
|
29
33
|
|
34
|
+
<pre>
|
30
35
|
require "simplejob"
|
31
36
|
|
32
37
|
SimpleJob::Worker.start do
|
@@ -36,15 +41,14 @@ SimpleJob::Worker.start do
|
|
36
41
|
end
|
37
42
|
</pre>
|
38
43
|
|
39
|
-
|
40
|
-
$ ruby -rubygems consumer.rb
|
41
|
-
</pre>
|
44
|
+
### Run
|
42
45
|
|
43
46
|
<pre>
|
44
47
|
$ ruby -rubygems producer.rb
|
48
|
+
$ ruby -rubygems consumer.rb
|
45
49
|
</pre>
|
46
50
|
|
47
51
|
More examples
|
48
52
|
-------------
|
49
53
|
|
50
|
-
See the [examples directory](http://github.com/kenpratt/
|
54
|
+
See the [examples directory](http://github.com/kenpratt/simplejob/tree/master/examples).
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -0,0 +1,9 @@
|
|
1
|
+
Chained jobs
|
2
|
+
============
|
3
|
+
|
4
|
+
Open the consumer in a terminal window, and then run the producer:
|
5
|
+
|
6
|
+
ruby -rubygems consumer.rb
|
7
|
+
ruby -rubygems producer.rb
|
8
|
+
|
9
|
+
You should see the consumer processing each step of the request. Try starting up more consumers or running the producer more than once.
|
@@ -0,0 +1,35 @@
|
|
1
|
+
$:.unshift(File.expand_path("../../../lib", __FILE__))
|
2
|
+
|
3
|
+
require "simplejob"
|
4
|
+
|
5
|
+
SimpleJob::Worker.start do
|
6
|
+
handle "step1" do |props|
|
7
|
+
x = props[:x]
|
8
|
+
puts "step1: x = #{x}"
|
9
|
+
|
10
|
+
sleep 0.5
|
11
|
+
send "step2", { :x => x + x }
|
12
|
+
end
|
13
|
+
|
14
|
+
handle "step2" do |props|
|
15
|
+
x = props[:x]
|
16
|
+
puts "step2: x = #{x}"
|
17
|
+
|
18
|
+
sleep 0.5
|
19
|
+
send "step3", { :x => x * 3 }
|
20
|
+
end
|
21
|
+
|
22
|
+
handle "step3" do |props|
|
23
|
+
x = props[:x]
|
24
|
+
puts "step3: x = #{x}"
|
25
|
+
|
26
|
+
sleep 0.5
|
27
|
+
send "step4", { :x => x - 42 }
|
28
|
+
end
|
29
|
+
|
30
|
+
handle "step4" do |props|
|
31
|
+
x = props[:x]
|
32
|
+
puts "step4: x = #{x}"
|
33
|
+
puts "FIN"
|
34
|
+
end
|
35
|
+
end
|
data/lib/simplejob/client.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require "amqp"
|
2
|
-
|
3
1
|
module SimpleJob
|
4
2
|
DEFAULT_EXCHANGE_NAME = "simplejob"
|
5
3
|
|
@@ -46,5 +44,33 @@ module SimpleJob
|
|
46
44
|
def subscribe(queue, &proc)
|
47
45
|
queue.subscribe({ :ack => true }, &proc)
|
48
46
|
end
|
47
|
+
|
48
|
+
alias_method :orig_send, :send
|
49
|
+
def send(topic, props = {})
|
50
|
+
raise "Message properties should be a Hash" unless props.kind_of?(Hash)
|
51
|
+
log.info "[simplejob] New job: #{topic}, #{props.inspect}"
|
52
|
+
publish(topic, props.to_json)
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def log
|
58
|
+
Client.log
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.log
|
62
|
+
@logger ||= setup_logger
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.setup_logger
|
66
|
+
if defined?(LOGGER)
|
67
|
+
LOGGER
|
68
|
+
elsif defined?(Rails.logger)
|
69
|
+
Rails.logger
|
70
|
+
else
|
71
|
+
Logger.new(STDOUT)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
49
75
|
end
|
50
76
|
end
|
data/lib/simplejob/worker.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require "active_support/core_ext/hash"
|
2
|
-
|
3
1
|
module SimpleJob
|
4
2
|
class Worker < Client
|
5
3
|
def start(opts, &proc)
|
@@ -15,9 +13,16 @@ module SimpleJob
|
|
15
13
|
|
16
14
|
# start processing jobs
|
17
15
|
start_handler_loop
|
16
|
+
|
17
|
+
log.info "[worker] Started"
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
+
def stop
|
22
|
+
super
|
23
|
+
log.info "[worker] Stopped"
|
24
|
+
end
|
25
|
+
|
21
26
|
def handle(key, &proc)
|
22
27
|
# bind the message to the queue
|
23
28
|
bind(@queue, key)
|
data/lib/simplejob.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
require "amqp"
|
1
2
|
require "json"
|
2
3
|
require "logger"
|
4
|
+
require "active_support/core_ext/hash"
|
3
5
|
|
4
6
|
require "simplejob/client"
|
5
7
|
require "simplejob/worker"
|
@@ -9,15 +11,9 @@ module SimpleJob
|
|
9
11
|
# Send a work request
|
10
12
|
def self.send(topic, props = {})
|
11
13
|
Client.start do
|
12
|
-
|
13
|
-
log.info "[simplejob] New job: #{topic}, #{props.inspect}"
|
14
|
-
publish(topic, props.to_json)
|
14
|
+
send(topic, props)
|
15
15
|
stop
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
end
|
20
|
-
|
21
|
-
def log
|
22
|
-
Logger.new(STDOUT)
|
23
|
-
end
|
data/simplejob.gemspec
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{simplejob}
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = [%q{Ken Pratt}]
|
12
|
+
s.date = %q{2011-05-06}
|
13
|
+
s.description = %q{A simple AMQP-backed job queuing system.}
|
14
|
+
s.email = %q{ken@kenpratt.net}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"LICENSE.txt",
|
22
|
+
"README.md",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"examples/basic/README.md",
|
26
|
+
"examples/basic/consumer.rb",
|
27
|
+
"examples/basic/producer.rb",
|
28
|
+
"examples/chained_jobs/README.md",
|
29
|
+
"examples/chained_jobs/consumer.rb",
|
30
|
+
"examples/chained_jobs/producer.rb",
|
31
|
+
"examples/multiple_jobs/README.md",
|
32
|
+
"examples/multiple_jobs/drink_consumer.rb",
|
33
|
+
"examples/multiple_jobs/food_consumer.rb",
|
34
|
+
"examples/multiple_jobs/producer.rb",
|
35
|
+
"examples/wildcard_handlers/README.md",
|
36
|
+
"examples/wildcard_handlers/consumer.rb",
|
37
|
+
"examples/wildcard_handlers/producer.rb",
|
38
|
+
"examples/wildcard_handlers/wiretap.rb",
|
39
|
+
"lib/simplejob.rb",
|
40
|
+
"lib/simplejob/client.rb",
|
41
|
+
"lib/simplejob/worker.rb",
|
42
|
+
"simplejob.gemspec",
|
43
|
+
"test/helper.rb",
|
44
|
+
"test/test_simplejob.rb"
|
45
|
+
]
|
46
|
+
s.homepage = %q{http://github.com/kenpratt/simplejob}
|
47
|
+
s.licenses = [%q{MIT}]
|
48
|
+
s.require_paths = [%q{lib}]
|
49
|
+
s.rubygems_version = %q{1.8.1}
|
50
|
+
s.summary = %q{Painless job queueing.}
|
51
|
+
|
52
|
+
if s.respond_to? :specification_version then
|
53
|
+
s.specification_version = 3
|
54
|
+
|
55
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
56
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 3.0.1"])
|
57
|
+
s.add_runtime_dependency(%q<json>, [">= 1.5.1"])
|
58
|
+
s.add_runtime_dependency(%q<amqp>, ["= 0.7.1"])
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<activesupport>, [">= 3.0.1"])
|
61
|
+
s.add_dependency(%q<json>, [">= 1.5.1"])
|
62
|
+
s.add_dependency(%q<amqp>, ["= 0.7.1"])
|
63
|
+
end
|
64
|
+
else
|
65
|
+
s.add_dependency(%q<activesupport>, [">= 3.0.1"])
|
66
|
+
s.add_dependency(%q<json>, [">= 1.5.1"])
|
67
|
+
s.add_dependency(%q<amqp>, ["= 0.7.1"])
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simplejob
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ken Pratt
|
@@ -83,6 +83,9 @@ files:
|
|
83
83
|
- examples/basic/README.md
|
84
84
|
- examples/basic/consumer.rb
|
85
85
|
- examples/basic/producer.rb
|
86
|
+
- examples/chained_jobs/README.md
|
87
|
+
- examples/chained_jobs/consumer.rb
|
88
|
+
- examples/chained_jobs/producer.rb
|
86
89
|
- examples/multiple_jobs/README.md
|
87
90
|
- examples/multiple_jobs/drink_consumer.rb
|
88
91
|
- examples/multiple_jobs/food_consumer.rb
|
@@ -94,6 +97,7 @@ files:
|
|
94
97
|
- lib/simplejob.rb
|
95
98
|
- lib/simplejob/client.rb
|
96
99
|
- lib/simplejob/worker.rb
|
100
|
+
- simplejob.gemspec
|
97
101
|
- test/helper.rb
|
98
102
|
- test/test_simplejob.rb
|
99
103
|
homepage: http://github.com/kenpratt/simplejob
|