background_bunnies 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/background_bunnies.gemspec +1 -0
- data/lib/background_bunnies/bunny.rb +30 -15
- data/lib/background_bunnies/job.rb +1 -0
- data/lib/background_bunnies/producer.rb +2 -6
- data/lib/background_bunnies/version.rb +1 -1
- data/lib/background_bunnies.rb +35 -12
- data/test/background_bunnies_test.rb +7 -0
- metadata +18 -2
data/background_bunnies.gemspec
CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
gem.add_dependency "bunny", ">= 0.9.0.pre7"
|
20
|
+
gem.add_dependency "amqp"
|
20
21
|
gem.add_development_dependency "rake"
|
21
22
|
gem.add_development_dependency "minitest"
|
22
23
|
end
|
@@ -1,11 +1,13 @@
|
|
1
1
|
require 'thread'
|
2
|
+
require 'bunny'
|
3
|
+
require 'amqp'
|
2
4
|
require_relative 'job'
|
3
5
|
|
4
6
|
module BackgroundBunnies
|
5
7
|
module Bunny
|
6
8
|
|
7
9
|
module BunnyConfigurators
|
8
|
-
|
10
|
+
DEFAULT_CONNECTION_OPTIONS = {:threaded=>true}
|
9
11
|
def group(group_name)
|
10
12
|
@group_name = group_name
|
11
13
|
end
|
@@ -22,6 +24,14 @@ module BackgroundBunnies
|
|
22
24
|
@queue_name || demodulized_class_name
|
23
25
|
end
|
24
26
|
|
27
|
+
def connection_options
|
28
|
+
@connection_options || DEFAULT_CONNECTION_OPTIONS.dup
|
29
|
+
end
|
30
|
+
|
31
|
+
def connection_options=(options)
|
32
|
+
@connection_options
|
33
|
+
end
|
34
|
+
|
25
35
|
def demodulized_class_name
|
26
36
|
path = name
|
27
37
|
if i = path.rindex('::')
|
@@ -48,36 +58,41 @@ module BackgroundBunnies
|
|
48
58
|
self.class.queue_name
|
49
59
|
end
|
50
60
|
|
61
|
+
def connection_options
|
62
|
+
self.class.connection_options
|
63
|
+
end
|
64
|
+
|
51
65
|
attr_reader :channel
|
52
66
|
attr_reader :queue
|
53
67
|
attr_reader :consumer
|
68
|
+
attr_reader :exchange
|
69
|
+
attr_reader :thread
|
54
70
|
|
55
71
|
#
|
56
|
-
# Starts the Worker
|
72
|
+
# Starts the Worker with the given connection or group name
|
57
73
|
#
|
58
|
-
def start(
|
59
|
-
@
|
74
|
+
def start(connection_or_group)
|
75
|
+
@connection = connection_or_group
|
76
|
+
@channel = AMQP::Channel.new(@connection)
|
60
77
|
@queue = @channel.queue(queue_name)
|
61
|
-
@consumer = @queue.subscribe(
|
62
|
-
|
63
|
-
|
78
|
+
@consumer = @queue.subscribe(:ack=>true) do |metadata, payload|
|
79
|
+
info = metadata
|
80
|
+
properties = nil
|
64
81
|
begin
|
82
|
+
job = Job.new(JSON.parse!(payload), info, properties)
|
83
|
+
err = nil
|
65
84
|
self.process(job)
|
66
|
-
|
67
|
-
|
68
|
-
end
|
69
|
-
unless err
|
70
|
-
@channel.ack(info.delivery_tag, false)
|
71
|
-
else
|
85
|
+
metadata.ack
|
86
|
+
rescue =>err
|
72
87
|
# processing went wrong, requeing message
|
73
|
-
@channel.reject(info.delivery_tag, true)
|
74
88
|
on_error(job, err)
|
89
|
+
metadata.reject(:requeue=>true)
|
75
90
|
end
|
76
91
|
end
|
77
92
|
end
|
78
93
|
|
79
94
|
def on_error(job, err)
|
80
|
-
|
95
|
+
log_error "Error processing #{job.info.delivery_tag}: #{err.message}, #{err.backtrace.join('\n')}"
|
81
96
|
end
|
82
97
|
|
83
98
|
def log_error(a)
|
@@ -7,12 +7,8 @@ module BackgroundBunnies
|
|
7
7
|
attr_reader :queue_name
|
8
8
|
attr_reader :connection
|
9
9
|
|
10
|
-
def initialize(
|
11
|
-
|
12
|
-
@connection = BackgroundBunnies.connections[connection]
|
13
|
-
else
|
14
|
-
@connection = connection
|
15
|
-
end
|
10
|
+
def initialize(connection_or_group, queue_name)
|
11
|
+
@connection = BackgroundBunnies.connect connection_or_group
|
16
12
|
@queue_name = queue_name.to_s
|
17
13
|
@channel = @connection.create_channel
|
18
14
|
@queue = @connection.queue(@queue_name)
|
data/lib/background_bunnies.rb
CHANGED
@@ -5,6 +5,8 @@ require "background_bunnies/producer"
|
|
5
5
|
require "background_bunnies/job"
|
6
6
|
require "background_bunnies/workers"
|
7
7
|
require "thread"
|
8
|
+
require "bunny"
|
9
|
+
require "amqp"
|
8
10
|
|
9
11
|
module BackgroundBunnies
|
10
12
|
|
@@ -17,30 +19,51 @@ module BackgroundBunnies
|
|
17
19
|
@configs || @configs = {}
|
18
20
|
end
|
19
21
|
|
20
|
-
|
22
|
+
DEFAULT_CONNECTION_OPTIONS={:threaded=>false}.freeze
|
21
23
|
|
22
24
|
#
|
23
25
|
# Configures the connection of a group
|
24
26
|
#
|
25
|
-
def configure(group,
|
26
|
-
info "Configuring #{group} connection"
|
27
|
-
configurations[group] =
|
28
|
-
|
29
|
-
|
27
|
+
def configure(group, url, options = DEFAULT_CONNECTION_OPTIONS)
|
28
|
+
info "Configuring #{group} connection with #{url} and options #{options}"
|
29
|
+
configurations[group] = {
|
30
|
+
:url=>url,
|
31
|
+
:options=>options
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# Creates a new connection based on a group configuration
|
37
|
+
#
|
38
|
+
def connect(connection_or_group, options=DEFAULT_CONNECTION_OPTIONS)
|
39
|
+
unless connection_or_group.is_a? Symbol
|
40
|
+
return connection_or_group
|
30
41
|
end
|
42
|
+
configuration = self.configurations[connection_or_group] || DEFAULT_CONNECTION_OPTIONS
|
43
|
+
configuration = configuration.dup.merge!(options)
|
44
|
+
raise "Unable to connect. Missing configuration for group #{connection_or_group}" unless configuration
|
45
|
+
conn = ::Bunny.new(configuration[:url], configuration[:options])
|
46
|
+
conn.start
|
47
|
+
conn
|
31
48
|
end
|
32
49
|
|
33
50
|
#
|
34
51
|
# Runs all the tasks in the given group and waits.
|
35
52
|
#
|
36
53
|
def run(group)
|
37
|
-
connection = configurations[group]
|
38
54
|
klasses = describe_group(group)
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
55
|
+
instances = []
|
56
|
+
EventMachine.run do
|
57
|
+
url = configurations[group][:url]
|
58
|
+
puts "Group Connection: #{url}"
|
59
|
+
connection = AMQP.connect(url)
|
60
|
+
info "Running #{group} workers: #{ klasses.join(',') }"
|
61
|
+
klasses.each do |klass|
|
62
|
+
instance = klass.new
|
63
|
+
instance.start connection
|
64
|
+
info "Started worker: #{klass.demodulized_class_name}"
|
65
|
+
instances << instance
|
66
|
+
end
|
44
67
|
end
|
45
68
|
Thread.current.join
|
46
69
|
end
|
@@ -130,6 +130,12 @@ describe BackgroundBunnies::Bunny do
|
|
130
130
|
self.delivery_tag = delivery_tag
|
131
131
|
end
|
132
132
|
|
133
|
+
attr_accessor :rejected
|
134
|
+
|
135
|
+
def reject(delivery_tag, multiple)
|
136
|
+
@rejected = true
|
137
|
+
end
|
138
|
+
|
133
139
|
end
|
134
140
|
channel_klass.new
|
135
141
|
end
|
@@ -143,6 +149,7 @@ describe BackgroundBunnies::Bunny do
|
|
143
149
|
worker = StartTest.new
|
144
150
|
worker.start connection
|
145
151
|
worker.channel.delivery_tag.must_be_nil
|
152
|
+
worker.channel.rejected.must_be :==, true
|
146
153
|
end
|
147
154
|
end
|
148
155
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: background_bunnies
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bunny
|
@@ -27,6 +27,22 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 0.9.0.pre7
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: amqp
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: rake
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|