rabbit_jobs 0.1.15 → 0.2.0.pre1
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.
- data/examples/configuration.rb +0 -2
- data/lib/rabbit_jobs/amqp_helper.rb +57 -0
- data/lib/rabbit_jobs/configuration.rb +24 -37
- data/lib/rabbit_jobs/error_mailer.rb +5 -1
- data/lib/rabbit_jobs/job.rb +13 -22
- data/lib/rabbit_jobs/publisher.rb +27 -23
- data/lib/rabbit_jobs/scheduler.rb +8 -15
- data/lib/rabbit_jobs/version.rb +1 -1
- data/lib/rabbit_jobs/worker.rb +16 -21
- data/lib/rabbit_jobs.rb +1 -12
- data/rabbit_jobs.gemspec +0 -1
- data/spec/fixtures/config.yml +1 -4
- data/spec/integration/publisher_spec.rb +1 -2
- data/spec/integration/worker_spec.rb +2 -2
- data/spec/unit/configuration_spec.rb +6 -18
- data/spec/unit/mailer_spec.rb +3 -2
- data/spec/unit/worker_spec.rb +0 -18
- metadata +7 -26
- data/lib/rabbit_jobs/amqp_helpers.rb +0 -52
data/examples/configuration.rb
CHANGED
@@ -6,8 +6,6 @@ require 'json'
|
|
6
6
|
RabbitJobs.configure do |c|
|
7
7
|
c.url "amqp://localhost/"
|
8
8
|
|
9
|
-
c.exchange 'test_exchange', durable: true, auto_delete: false
|
10
|
-
|
11
9
|
c.queue 'rabbit_jobs_test1', durable: true, auto_delete: false, ack: true, arguments: {'x-ha-policy' => 'all'}
|
12
10
|
c.queue 'rabbit_jobs_test2', durable: true, auto_delete: false, ack: true, arguments: {'x-ha-policy' => 'all'}
|
13
11
|
c.queue 'rabbit_jobs_test3', durable: true, auto_delete: false, ack: true, arguments: {'x-ha-policy' => 'all'}
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'amqp'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
module RabbitJobs
|
6
|
+
class AmqpHelper
|
7
|
+
|
8
|
+
class << self
|
9
|
+
# Calls given block with initialized amqp connection.
|
10
|
+
def with_amqp
|
11
|
+
raise ArgumentError unless block_given?
|
12
|
+
|
13
|
+
if EM.reactor_running?
|
14
|
+
yield false
|
15
|
+
else
|
16
|
+
AMQP.start(RJ.config.url) {
|
17
|
+
yield true
|
18
|
+
}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def prepare_channel
|
23
|
+
unless AMQP.channel
|
24
|
+
create_channel
|
25
|
+
else
|
26
|
+
create_channel unless AMQP.channel.open?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def url_from_opts(opts = {})
|
33
|
+
s = ""
|
34
|
+
s << opts[:scheme]
|
35
|
+
s << "://"
|
36
|
+
s << "#{opts[:user]}@" if opts[:user] && opts[:user] != 'guest'
|
37
|
+
s << opts[:host]
|
38
|
+
s << ":#{opts[:port]}" unless (opts[:scheme] == 'amqp' && opts[:port] == 5672) || (opts[:scheme] == 'amqps' && opts[:port] == 5673)
|
39
|
+
s << opts[:vhost]
|
40
|
+
end
|
41
|
+
|
42
|
+
def create_channel
|
43
|
+
AMQP.channel = AMQP::Channel.new(AMQP.connection, auto_recovery: true)
|
44
|
+
|
45
|
+
AMQP.connection.on_recovery do |conn, opts|
|
46
|
+
url = url_from_opts opts
|
47
|
+
RJ.logger.warn "[network failure] Connection to #{url} established."
|
48
|
+
end
|
49
|
+
AMQP.connection.on_tcp_connection_loss do |conn, opts|
|
50
|
+
url = url_from_opts opts
|
51
|
+
RJ.logger.warn "[network failure] Trying to reconnect to #{url}..."
|
52
|
+
conn.reconnect(false, 2)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -1,13 +1,14 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
require 'yaml'
|
3
|
+
require 'uri'
|
3
4
|
|
4
5
|
module RabbitJobs
|
5
6
|
|
6
7
|
extend self
|
7
8
|
|
8
|
-
def configure
|
9
|
+
def configure
|
9
10
|
@@configuration ||= Configuration.new
|
10
|
-
|
11
|
+
yield @@configuration if block_given?
|
11
12
|
end
|
12
13
|
|
13
14
|
def config
|
@@ -27,8 +28,8 @@ module RabbitJobs
|
|
27
28
|
|
28
29
|
unless @@configuration
|
29
30
|
self.configure do |c|
|
30
|
-
c.url 'amqp://localhost
|
31
|
-
c.
|
31
|
+
c.url 'amqp://localhost'
|
32
|
+
c.prefix 'rabbit_jobs'
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
@@ -44,14 +45,9 @@ module RabbitJobs
|
|
44
45
|
ack: true
|
45
46
|
}
|
46
47
|
|
47
|
-
DEFAULT_EXCHANGE_PARAMS = {
|
48
|
-
auto_delete: false,
|
49
|
-
durable: true
|
50
|
-
}
|
51
|
-
|
52
48
|
DEFAULT_MESSAGE_PARAMS = {
|
53
49
|
persistent: true,
|
54
|
-
|
50
|
+
mandatory: true,
|
55
51
|
immediate: false
|
56
52
|
}
|
57
53
|
|
@@ -62,9 +58,8 @@ module RabbitJobs
|
|
62
58
|
def initialize
|
63
59
|
@data = {
|
64
60
|
error_log: true,
|
65
|
-
url: 'amqp://localhost
|
66
|
-
|
67
|
-
exchange_params: DEFAULT_EXCHANGE_PARAMS,
|
61
|
+
url: 'amqp://localhost',
|
62
|
+
prefix: 'rabbit_jobs',
|
68
63
|
queues: {}
|
69
64
|
}
|
70
65
|
end
|
@@ -76,18 +71,10 @@ module RabbitJobs
|
|
76
71
|
def connection_options
|
77
72
|
return @data[:connection_options] if @data[:connection_options]
|
78
73
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
@data[:connection_options] = {
|
85
|
-
host: uri.host || 'localhost',
|
86
|
-
port: uri.port || ports[uri.scheme.downcase],
|
87
|
-
vhost: uri.path || '/',
|
88
|
-
user: uri.user || 'guest',
|
89
|
-
password: uri.password || 'guest'
|
90
|
-
}
|
74
|
+
@data[:connection_options] = AMQP::Client.parse_connection_uri(url)
|
75
|
+
@data[:vhost] ||= "/"
|
76
|
+
@data[:vhost] = "/" if @data[:vhost].empty?
|
77
|
+
@data[:connection_options]
|
91
78
|
end
|
92
79
|
|
93
80
|
def mail_errors_to(email = nil)
|
@@ -118,19 +105,18 @@ module RabbitJobs
|
|
118
105
|
if value
|
119
106
|
raise ArgumentError unless value.is_a?(String) && value != ""
|
120
107
|
@data[:url] = value.to_s
|
121
|
-
@data
|
108
|
+
@data.delete :connection_options
|
122
109
|
else
|
123
|
-
@data[:url] || 'amqp://localhost
|
110
|
+
@data[:url] || 'amqp://localhost'
|
124
111
|
end
|
125
112
|
end
|
126
113
|
|
127
|
-
def
|
114
|
+
def prefix(value = nil)
|
128
115
|
if value
|
129
116
|
raise ArgumentError unless value.is_a?(String) && value != ""
|
130
|
-
@data[:
|
131
|
-
@data[:exchange_params] = DEFAULT_EXCHANGE_PARAMS.merge(params)
|
117
|
+
@data[:prefix] = value.downcase
|
132
118
|
else
|
133
|
-
@data[:
|
119
|
+
@data[:prefix]
|
134
120
|
end
|
135
121
|
end
|
136
122
|
|
@@ -153,13 +139,15 @@ module RabbitJobs
|
|
153
139
|
|
154
140
|
def init_default_queue
|
155
141
|
queue('default', DEFAULT_QUEUE_PARAMS) if @data[:queues].empty?
|
156
|
-
key = @data[:queues].keys.first
|
157
142
|
end
|
158
143
|
|
159
|
-
|
144
|
+
def default_queue
|
145
|
+
queue('default', DEFAULT_QUEUE_PARAMS) if @data[:queues].empty?
|
146
|
+
key = @data[:queues].keys.first
|
147
|
+
end
|
160
148
|
|
161
149
|
def queue_name(routing_key)
|
162
|
-
[@data[:
|
150
|
+
[@data[:prefix], routing_key].join('#')
|
163
151
|
end
|
164
152
|
|
165
153
|
def load_file(filename)
|
@@ -176,11 +164,10 @@ module RabbitJobs
|
|
176
164
|
elsif defined?(Rails) && yaml[Rails.env.to_s]
|
177
165
|
convert_yaml_config(yaml[Rails.env.to_s])
|
178
166
|
else
|
179
|
-
@data = {url: nil,
|
180
|
-
%w(url
|
167
|
+
@data = {url: nil, prefix: nil, queues: {}}
|
168
|
+
%w(url prefix mail_errors_to mail_errors_from).each do |m|
|
181
169
|
self.send(m, yaml[m])
|
182
170
|
end
|
183
|
-
exchange yaml['exchange'], symbolize_keys!(yaml['exchange_params'])
|
184
171
|
yaml['queues'].each do |name, params|
|
185
172
|
queue name, symbolize_keys!(params) || {}
|
186
173
|
end
|
@@ -2,7 +2,11 @@
|
|
2
2
|
module RabbitJobs
|
3
3
|
class ErrorMailer
|
4
4
|
def self.enabled?
|
5
|
-
|
5
|
+
!!(
|
6
|
+
defined?(ActionMailer) && \
|
7
|
+
RabbitJobs.config.mail_errors_from && !RabbitJobs.config.mail_errors_from.empty? && \
|
8
|
+
RabbitJobs.config.mail_errors_to && !RabbitJobs.config.mail_errors_to.empty?
|
9
|
+
)
|
6
10
|
end
|
7
11
|
|
8
12
|
def self.report_error(job, error = $!)
|
data/lib/rabbit_jobs/job.rb
CHANGED
@@ -14,29 +14,16 @@ module RabbitJobs::Job
|
|
14
14
|
self.opts = {}
|
15
15
|
end
|
16
16
|
|
17
|
-
attr_accessor :params, :opts
|
17
|
+
attr_accessor :params, :opts
|
18
18
|
|
19
19
|
def run_perform
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
begin
|
28
|
-
RJ.__after_fork_callbacks.each do |callback|
|
29
|
-
callback.call()
|
30
|
-
end
|
31
|
-
|
32
|
-
self.class.perform(*params)
|
33
|
-
rescue
|
34
|
-
RJ.logger.warn(self.inspect)
|
35
|
-
RJ.logger.warn([$!.inspect, $!.backtrace.to_a].join("\n"))
|
36
|
-
run_on_error_hooks($!)
|
37
|
-
RabbitJobs::ErrorMailer.report_error(self, $!)
|
38
|
-
end
|
39
|
-
exit!
|
20
|
+
begin
|
21
|
+
self.class.perform(*params)
|
22
|
+
rescue
|
23
|
+
RJ.logger.warn(self.inspect)
|
24
|
+
RJ.logger.warn([$!.inspect, $!.backtrace.to_a].join("\n"))
|
25
|
+
run_on_error_hooks($!)
|
26
|
+
RabbitJobs::ErrorMailer.report_error(self, $!)
|
40
27
|
end
|
41
28
|
end
|
42
29
|
|
@@ -104,14 +91,18 @@ module RabbitJobs::Job
|
|
104
91
|
begin
|
105
92
|
encoded = JSON.parse(payload)
|
106
93
|
job_klass = constantize(encoded['class'])
|
107
|
-
job =
|
94
|
+
job = job_klass.new(*encoded['params'])
|
108
95
|
job.opts = encoded['opts']
|
109
96
|
job
|
97
|
+
rescue NameError
|
98
|
+
RJ.logger.error "Cannot find job class '#{encoded['class']}'"
|
99
|
+
:not_found
|
110
100
|
rescue
|
111
101
|
RJ.logger.error "JOB INIT ERROR at #{Time.now.to_s}:"
|
112
102
|
RJ.logger.error $!.inspect
|
113
103
|
RJ.logger.error $!.backtrace
|
114
104
|
RJ.logger.error "message: #{payload.inspect}"
|
105
|
+
nil
|
115
106
|
end
|
116
107
|
end
|
117
108
|
end
|
@@ -3,27 +3,23 @@
|
|
3
3
|
require 'json'
|
4
4
|
require 'amqp'
|
5
5
|
require 'eventmachine'
|
6
|
-
require 'bunny'
|
7
6
|
require 'uri'
|
8
7
|
|
9
8
|
module RabbitJobs
|
10
9
|
module Publisher
|
11
10
|
extend self
|
12
|
-
extend AmqpHelpers
|
13
11
|
|
14
12
|
def publish(klass, *params)
|
15
13
|
publish_to(RJ.config.default_queue, klass, *params)
|
16
14
|
end
|
17
15
|
|
18
16
|
def publish_to(routing_key, klass, *params)
|
19
|
-
raise ArgumentError unless klass && (klass.is_a?(Class) || klass.is_a?(String))
|
20
|
-
raise ArgumentError unless routing_key && (routing_key.is_a?(Symbol) || routing_key.is_a?(String))
|
17
|
+
raise ArgumentError.new("klass=#{klass.inspect}") unless klass && (klass.is_a?(Class) || klass.is_a?(String))
|
18
|
+
raise ArgumentError.new("routing_key=#{routing_key}") unless routing_key && (routing_key.is_a?(Symbol) || routing_key.is_a?(String)) && !!RJ.config[:queues][routing_key.to_s]
|
21
19
|
|
22
20
|
begin
|
23
|
-
|
24
|
-
|
25
|
-
queue = bunny.queue(RJ.config.queue_name(routing_key), RJ.config[:queues][routing_key.to_s])
|
26
|
-
queue.bind(exchange, :routing_key => routing_key.to_s)
|
21
|
+
AmqpHelper.with_amqp do |stop_em|
|
22
|
+
AmqpHelper.prepare_channel
|
27
23
|
|
28
24
|
payload = {
|
29
25
|
'class' => klass.to_s,
|
@@ -31,9 +27,14 @@ module RabbitJobs
|
|
31
27
|
'params' => params
|
32
28
|
}.to_json
|
33
29
|
|
34
|
-
|
30
|
+
AMQP::Exchange.default.publish(payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({key: RJ.config.queue_name(routing_key.to_s)})) do
|
31
|
+
if stop_em
|
32
|
+
AMQP.connection.disconnect { EM.stop }
|
33
|
+
end
|
34
|
+
end
|
35
35
|
end
|
36
36
|
rescue
|
37
|
+
raise $!
|
37
38
|
RJ.logger.warn $!.inspect
|
38
39
|
RJ.logger.warn $!.backtrace.join("\n")
|
39
40
|
end
|
@@ -45,24 +46,27 @@ module RabbitJobs
|
|
45
46
|
raise ArgumentError unless routing_keys && routing_keys.count > 0
|
46
47
|
|
47
48
|
messages_count = 0
|
48
|
-
|
49
|
+
|
50
|
+
count = routing_keys.count
|
51
|
+
|
52
|
+
AmqpHelper.with_amqp do |stop_em|
|
49
53
|
routing_keys.each do |routing_key|
|
50
|
-
queue =
|
51
|
-
|
52
|
-
|
54
|
+
queue = AMQP.channel.queue(RJ.config.queue_name(routing_key), RJ.config[:queues][routing_key.to_s])
|
55
|
+
queue.status do |messages, consumers|
|
56
|
+
# messages_count += messages
|
57
|
+
queue.purge do |ret|
|
58
|
+
raise "Cannot purge queue #{routing_key.to_s}." unless ret.is_a?(AMQ::Protocol::Queue::PurgeOk)
|
59
|
+
messages_count += ret.message_count
|
60
|
+
count -= 1
|
61
|
+
if count == 0 && stop_em
|
62
|
+
AMQP.connection.disconnect { EM.stop }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
53
66
|
end
|
54
67
|
end
|
55
|
-
return messages_count
|
56
|
-
end
|
57
68
|
|
58
|
-
|
59
|
-
def self.with_bunny(&block)
|
60
|
-
raise ArgumentError unless block
|
61
|
-
|
62
|
-
# raise RJ.config.connection_options.inspect
|
63
|
-
Bunny.run(RJ.config.connection_options.merge({logging: false})) do |bunny|
|
64
|
-
block.call(bunny)
|
65
|
-
end
|
69
|
+
return messages_count
|
66
70
|
end
|
67
71
|
end
|
68
72
|
end
|
@@ -6,7 +6,6 @@ require 'yaml'
|
|
6
6
|
|
7
7
|
module RabbitJobs
|
8
8
|
class Scheduler
|
9
|
-
include AmqpHelpers
|
10
9
|
|
11
10
|
attr_accessor :pidfile, :background, :schedule, :process_name
|
12
11
|
|
@@ -88,7 +87,14 @@ module RabbitJobs
|
|
88
87
|
$0 = self.process_name || "rj_scheduler"
|
89
88
|
|
90
89
|
processed_count = 0
|
91
|
-
|
90
|
+
AmqpHelper.with_amqp do |connection, stop_em|
|
91
|
+
channel = AMQP::Channel.new(connection)
|
92
|
+
|
93
|
+
channel.on_error do |ch, channel_close|
|
94
|
+
puts "Channel-level error: #{channel_close.reply_text}, shutting down..."
|
95
|
+
connection.disconnect { EM.stop }
|
96
|
+
end
|
97
|
+
|
92
98
|
load_schedule!
|
93
99
|
|
94
100
|
check_shutdown = Proc.new {
|
@@ -154,19 +160,6 @@ module RabbitJobs
|
|
154
160
|
|
155
161
|
def shutdown!
|
156
162
|
shutdown
|
157
|
-
kill_child
|
158
|
-
end
|
159
|
-
|
160
|
-
def kill_child
|
161
|
-
if @job && @job.child_pid
|
162
|
-
# RJ.logger.warn "Killing child at #{@child}"
|
163
|
-
if Kernel.system("ps -o pid,state -p #{@job.child_pid}")
|
164
|
-
Process.kill("KILL", @job.child_pid) rescue nil
|
165
|
-
else
|
166
|
-
# RJ.logger.warn "Child #{@child} not found, restarting."
|
167
|
-
# shutdown
|
168
|
-
end
|
169
|
-
end
|
170
163
|
end
|
171
164
|
end
|
172
165
|
end
|
data/lib/rabbit_jobs/version.rb
CHANGED
data/lib/rabbit_jobs/worker.rb
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
module RabbitJobs
|
4
4
|
class Worker
|
5
|
-
include AmqpHelpers
|
6
|
-
|
7
5
|
attr_accessor :pidfile, :background, :process_name, :worker_pid
|
8
6
|
|
9
7
|
# Workers should be initialized with an array of string queue
|
@@ -39,15 +37,16 @@ module RabbitJobs
|
|
39
37
|
RJ.logger.info("Connecting to amqp...")
|
40
38
|
|
41
39
|
begin
|
42
|
-
|
43
|
-
|
40
|
+
AmqpHelper.with_amqp do |stop_em|
|
41
|
+
AmqpHelper.prepare_channel
|
42
|
+
AMQP.channel.prefetch(1)
|
44
43
|
|
45
44
|
check_shutdown = Proc.new {
|
46
45
|
if @shutdown
|
47
46
|
RJ.logger.info "Processed jobs: #{processed_count}"
|
48
47
|
RJ.logger.info "Stopping worker ##{Process.pid}..."
|
49
48
|
|
50
|
-
connection.
|
49
|
+
AMQP.connection.disconnect {
|
51
50
|
File.delete(self.pidfile) if self.pidfile && File.exists?(self.pidfile)
|
52
51
|
RJ.logger.info "##{Process.pid} stopped."
|
53
52
|
RJ.logger.close
|
@@ -60,24 +59,28 @@ module RabbitJobs
|
|
60
59
|
}
|
61
60
|
|
62
61
|
queues.each do |routing_key|
|
63
|
-
queue =
|
62
|
+
queue = AMQP.channel.queue(RJ.config.queue_name(routing_key), RJ.config[:queues][routing_key])
|
64
63
|
|
65
|
-
RJ.logger.info "Worker ##{Process.pid} <= #{
|
64
|
+
RJ.logger.info "Worker ##{Process.pid} <= #{RJ.config.queue_name(routing_key)}"
|
66
65
|
|
67
66
|
explicit_ack = !!RJ.config[:queues][routing_key][:ack]
|
68
67
|
|
69
68
|
queue.subscribe(ack: explicit_ack) do |metadata, payload|
|
70
69
|
@job = RJ::Job.parse(payload)
|
71
70
|
|
72
|
-
|
73
|
-
|
74
|
-
processed_count += 1
|
71
|
+
if @job == :not_found
|
72
|
+
metadata.ack if explicit_ack
|
75
73
|
else
|
76
|
-
|
74
|
+
if @job.expired?
|
75
|
+
RJ.logger.info "Job expired: #{@job.inspect}"
|
76
|
+
else
|
77
|
+
@job.run_perform
|
78
|
+
processed_count += 1
|
79
|
+
end
|
80
|
+
|
81
|
+
metadata.ack if explicit_ack
|
77
82
|
end
|
78
83
|
|
79
|
-
metadata.ack if explicit_ack
|
80
|
-
|
81
84
|
check_shutdown.call
|
82
85
|
end
|
83
86
|
end
|
@@ -140,14 +143,6 @@ module RabbitJobs
|
|
140
143
|
|
141
144
|
def shutdown!
|
142
145
|
shutdown
|
143
|
-
kill_child
|
144
|
-
end
|
145
|
-
|
146
|
-
def kill_child
|
147
|
-
if @job && @job.child_pid
|
148
|
-
RJ.logger.info "Killing child #{@job.child_pid} at #{Time.now}"
|
149
|
-
Process.kill("KILL", @job.child_pid) rescue nil
|
150
|
-
end
|
151
146
|
end
|
152
147
|
end
|
153
148
|
end
|
data/lib/rabbit_jobs.rb
CHANGED
@@ -4,7 +4,7 @@ require 'rabbit_jobs/version'
|
|
4
4
|
|
5
5
|
require 'rabbit_jobs/util'
|
6
6
|
require 'rabbit_jobs/helpers'
|
7
|
-
require 'rabbit_jobs/
|
7
|
+
require 'rabbit_jobs/amqp_helper'
|
8
8
|
require 'rabbit_jobs/configuration'
|
9
9
|
require 'rabbit_jobs/error_mailer'
|
10
10
|
|
@@ -30,17 +30,6 @@ module RabbitJobs
|
|
30
30
|
def logger
|
31
31
|
@logger ||= Logger.new $stdout
|
32
32
|
end
|
33
|
-
|
34
|
-
def after_fork(&block)
|
35
|
-
raise ArgumentError.new("No block passed to after_fork") unless block_given?
|
36
|
-
@@after_fork_callbacks ||= []
|
37
|
-
|
38
|
-
@@after_fork_callbacks << block
|
39
|
-
end
|
40
|
-
|
41
|
-
def __after_fork_callbacks
|
42
|
-
@@after_fork_callbacks ||= []
|
43
|
-
end
|
44
33
|
end
|
45
34
|
|
46
35
|
RJ = RabbitJobs
|
data/rabbit_jobs.gemspec
CHANGED
data/spec/fixtures/config.yml
CHANGED
@@ -6,7 +6,7 @@ require 'eventmachine'
|
|
6
6
|
describe RabbitJobs::Worker do
|
7
7
|
it 'should listen for messages' do
|
8
8
|
RabbitJobs.configure do |c|
|
9
|
-
c.
|
9
|
+
c.prefix 'test_durable'
|
10
10
|
c.queue 'rspec_durable_queue', auto_delete: false, durable: true, ack: true
|
11
11
|
end
|
12
12
|
|
@@ -23,7 +23,7 @@ describe RabbitJobs::Worker do
|
|
23
23
|
|
24
24
|
it 'should allow to publish jobs from worker' do
|
25
25
|
RabbitJobs.configure do |c|
|
26
|
-
c.
|
26
|
+
c.prefix 'test_durable'
|
27
27
|
c.queue 'rspec_durable_queue', auto_delete: false, durable: true, ack: true
|
28
28
|
end
|
29
29
|
|
@@ -8,7 +8,7 @@ describe RabbitJobs::Configuration do
|
|
8
8
|
|
9
9
|
c.url "amqp://somehost.lan"
|
10
10
|
|
11
|
-
c.
|
11
|
+
c.prefix 'my_prefix'
|
12
12
|
|
13
13
|
c.queue 'durable_queue', durable: true, auto_delete: false, ack: true, arguments: {'x-ha-policy' => 'all'}
|
14
14
|
c.queue 'fast_queue', durable: false, auto_delete: true, ack: false
|
@@ -17,11 +17,7 @@ describe RabbitJobs::Configuration do
|
|
17
17
|
RabbitJobs.config.to_hash.should == {
|
18
18
|
error_log: false,
|
19
19
|
url: "amqp://somehost.lan",
|
20
|
-
|
21
|
-
exchange_params: {
|
22
|
-
durable: true,
|
23
|
-
auto_delete: false
|
24
|
-
},
|
20
|
+
prefix: "my_prefix",
|
25
21
|
queues: {
|
26
22
|
"durable_queue" => {
|
27
23
|
durable: true,
|
@@ -43,11 +39,7 @@ describe RabbitJobs::Configuration do
|
|
43
39
|
|
44
40
|
RabbitJobs.config.to_hash.should == {
|
45
41
|
url: "amqp://example.com/vhost",
|
46
|
-
|
47
|
-
exchange_params: {
|
48
|
-
durable: true,
|
49
|
-
auto_delete: false
|
50
|
-
},
|
42
|
+
prefix: "my_prefix",
|
51
43
|
queues: {
|
52
44
|
"durable_queue" => {
|
53
45
|
durable: true,
|
@@ -68,11 +60,7 @@ describe RabbitJobs::Configuration do
|
|
68
60
|
RabbitJobs.config.to_hash.should == {
|
69
61
|
error_log: true,
|
70
62
|
url: "amqp://localhost",
|
71
|
-
|
72
|
-
exchange_params: {
|
73
|
-
auto_delete: false,
|
74
|
-
durable: true
|
75
|
-
},
|
63
|
+
prefix: "rabbit_jobs",
|
76
64
|
queues: {
|
77
65
|
"default" => {
|
78
66
|
auto_delete: false,
|
@@ -86,8 +74,8 @@ describe RabbitJobs::Configuration do
|
|
86
74
|
it 'returns settings on some methods' do
|
87
75
|
RabbitJobs.config.error_log == true
|
88
76
|
RabbitJobs.config.url.should == 'amqp://localhost'
|
89
|
-
RabbitJobs.config.routing_keys.should == [
|
90
|
-
RabbitJobs.config.
|
77
|
+
RabbitJobs.config.routing_keys.should == []
|
78
|
+
RabbitJobs.config.prefix.should == 'rabbit_jobs'
|
91
79
|
RabbitJobs.config.queue_name('default').should == 'rabbit_jobs#default'
|
92
80
|
end
|
93
81
|
end
|
data/spec/unit/mailer_spec.rb
CHANGED
@@ -7,9 +7,10 @@ describe RabbitJobs::ErrorMailer do
|
|
7
7
|
it 'should be enabled when use setup email' do
|
8
8
|
RabbitJobs::ErrorMailer.enabled?.should == false
|
9
9
|
RabbitJobs.configure do |c|
|
10
|
-
c.mail_errors_to 'dev@example.com'
|
10
|
+
c.mail_errors_to 'dev@example.com'
|
11
|
+
c.mail_errors_from 'app@example.com'
|
11
12
|
end
|
12
|
-
|
13
|
+
|
13
14
|
RabbitJobs::ErrorMailer.enabled?.should == true
|
14
15
|
end
|
15
16
|
end
|
data/spec/unit/worker_spec.rb
CHANGED
@@ -38,23 +38,5 @@ describe RabbitJobs::Worker do
|
|
38
38
|
@worker.shutdown
|
39
39
|
@worker.instance_variable_get('@shutdown').should == true
|
40
40
|
end
|
41
|
-
|
42
|
-
it '#shutdown! should kill child process' do
|
43
|
-
mock(@worker.kill_child)
|
44
|
-
mock(@worker.shutdown)
|
45
|
-
|
46
|
-
@worker.shutdown!
|
47
|
-
end
|
48
|
-
|
49
|
-
it '#kill_child' do
|
50
|
-
job = TestJob.new()
|
51
|
-
job.instance_variable_set '@child_pid', 123123
|
52
|
-
@worker.instance_variable_set('@job', job)
|
53
|
-
|
54
|
-
mock(Kernel).system("ps -o pid,state -p #{123123}") { true }
|
55
|
-
mock(Process).kill("KILL", 123123)
|
56
|
-
|
57
|
-
@worker.kill_child
|
58
|
-
end
|
59
41
|
end
|
60
42
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rabbit_jobs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.0.pre1
|
5
|
+
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Pavel Lazureykis
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08
|
12
|
+
date: 2012-11-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: amqp
|
@@ -27,22 +27,6 @@ dependencies:
|
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0.9'
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: bunny
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
|
-
requirements:
|
35
|
-
- - ~>
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
version: '0.7'
|
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.7'
|
46
30
|
- !ruby/object:Gem::Dependency
|
47
31
|
name: rake
|
48
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -90,7 +74,7 @@ files:
|
|
90
74
|
- Rakefile
|
91
75
|
- examples/configuration.rb
|
92
76
|
- lib/rabbit_jobs.rb
|
93
|
-
- lib/rabbit_jobs/
|
77
|
+
- lib/rabbit_jobs/amqp_helper.rb
|
94
78
|
- lib/rabbit_jobs/configuration.rb
|
95
79
|
- lib/rabbit_jobs/error_mailer.rb
|
96
80
|
- lib/rabbit_jobs/helpers.rb
|
@@ -129,16 +113,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
129
113
|
version: '0'
|
130
114
|
segments:
|
131
115
|
- 0
|
132
|
-
hash:
|
116
|
+
hash: -1227131973983864781
|
133
117
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
118
|
none: false
|
135
119
|
requirements:
|
136
|
-
- - ! '
|
120
|
+
- - ! '>'
|
137
121
|
- !ruby/object:Gem::Version
|
138
|
-
version:
|
139
|
-
segments:
|
140
|
-
- 0
|
141
|
-
hash: 4442654216517974901
|
122
|
+
version: 1.3.1
|
142
123
|
requirements: []
|
143
124
|
rubyforge_project:
|
144
125
|
rubygems_version: 1.8.24
|
@@ -1,52 +0,0 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
require 'uri'
|
3
|
-
|
4
|
-
module RabbitJobs
|
5
|
-
module AmqpHelpers
|
6
|
-
|
7
|
-
# Calls given block with initialized amqp
|
8
|
-
def amqp_with_exchange(&block)
|
9
|
-
raise ArgumentError unless block
|
10
|
-
|
11
|
-
connection = AMQP.connection
|
12
|
-
if connection && connection.open?
|
13
|
-
channel = AMQP::Channel.new(connection)
|
14
|
-
exchange = channel.direct(RJ.config[:exchange], RJ.config[:exchange_params])
|
15
|
-
# go work
|
16
|
-
block.call(connection, exchange)
|
17
|
-
|
18
|
-
else
|
19
|
-
AMQP.start(RJ.config.connection_options) do |connection|
|
20
|
-
|
21
|
-
channel = AMQP::Channel.new(connection)
|
22
|
-
|
23
|
-
channel.on_error do |ch, channel_close|
|
24
|
-
puts "Channel-level error: #{channel_close.reply_text}, shutting down..."
|
25
|
-
connection.close { EM.stop }
|
26
|
-
end
|
27
|
-
|
28
|
-
exchange = channel.direct(RJ.config[:exchange], RJ.config[:exchange_params])
|
29
|
-
# go work
|
30
|
-
block.call(connection, exchange)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def amqp_with_queue(routing_key, &block)
|
36
|
-
raise ArgumentError unless routing_key && block
|
37
|
-
|
38
|
-
amqp_with_exchange do |connection, exchange|
|
39
|
-
queue = make_queue(exchange, routing_key.to_s)
|
40
|
-
|
41
|
-
# go work
|
42
|
-
block.call(connection, queue)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def make_queue(exchange, routing_key)
|
47
|
-
queue = exchange.channel.queue(RJ.config.queue_name(routing_key), RJ.config[:queues][routing_key])
|
48
|
-
queue.bind(exchange, :routing_key => routing_key)
|
49
|
-
queue
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|