rabbit_jobs 0.6.2 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/examples/client +8 -31
- data/examples/worker +2 -8
- data/lib/rabbit_jobs/amqp_helper.rb +4 -105
- data/lib/rabbit_jobs/configuration.rb +4 -26
- data/lib/rabbit_jobs/publisher.rb +17 -35
- data/lib/rabbit_jobs/scheduler.rb +14 -18
- data/lib/rabbit_jobs/version.rb +1 -1
- data/lib/rabbit_jobs/worker.rb +49 -54
- data/lib/rabbit_jobs.rb +5 -70
- data/rabbit_jobs.gemspec +1 -1
- data/spec/integration/publisher_spec.rb +4 -12
- data/spec/integration/worker_spec.rb +13 -34
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/configuration_spec.rb +1 -1
- metadata +14 -14
data/Gemfile
CHANGED
data/examples/client
CHANGED
@@ -13,36 +13,13 @@ end
|
|
13
13
|
|
14
14
|
RJ.configure { |c|
|
15
15
|
c.queue "failover_test"
|
16
|
-
c.server "amqp://localhost/"
|
16
|
+
c.server "amqp://localhost/bunny"
|
17
17
|
}
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
RJ.
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
# EM.add_timer(5) {
|
28
|
-
# RJ.publish(MyCurrentJob) {
|
29
|
-
# puts 'published second message'
|
30
|
-
# RJ.stop
|
31
|
-
# }
|
32
|
-
# }
|
33
|
-
}
|
34
|
-
|
35
|
-
|
36
|
-
1000.times {
|
37
|
-
RJ.run do
|
38
|
-
count = 10000
|
39
|
-
published = 0
|
40
|
-
count.times {
|
41
|
-
RJ.publish_to(:default, MyCurrentJob) {
|
42
|
-
published += 1
|
43
|
-
RJ.stop if published >= count
|
44
|
-
}
|
45
|
-
}
|
46
|
-
end
|
47
|
-
puts 'returned from em'
|
48
|
-
}
|
19
|
+
i = 0
|
20
|
+
loop do
|
21
|
+
puts "publishing job #{i}"
|
22
|
+
RJ.publish_to(:failover_test, MyCurrentJob, i)
|
23
|
+
i += 1
|
24
|
+
sleep 1
|
25
|
+
end
|
data/examples/worker
CHANGED
@@ -8,21 +8,15 @@ require File.expand_path('../../lib/rabbit_jobs', __FILE__)
|
|
8
8
|
class MyCurrentJob
|
9
9
|
include RJ::Job
|
10
10
|
def self.perform(count = 0)
|
11
|
-
|
11
|
+
puts count
|
12
12
|
# RJ.publish_to(:default, MyCurrentJob, count - 1) if count > 0
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
RJ.configure { |c|
|
17
17
|
c.queue "failover_test"
|
18
|
-
c.server "amqp://localhost/"
|
19
|
-
c.server "amqp://dev.lan/"
|
18
|
+
c.server "amqp://localhost/bunny"
|
20
19
|
}
|
21
20
|
|
22
|
-
# AMQ::Client.logging = false
|
23
|
-
AMQP.logging = false
|
24
|
-
|
25
|
-
RJ.logger = Logger.new($stdout)
|
26
|
-
|
27
21
|
worker = RJ::Worker.new
|
28
22
|
worker.work
|
@@ -1,119 +1,18 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
|
-
require '
|
2
|
+
require 'bunny'
|
3
3
|
require 'uri'
|
4
4
|
|
5
5
|
module RabbitJobs
|
6
6
|
class AmqpHelper
|
7
7
|
|
8
|
-
# Timeout to recover connection.
|
9
|
-
RECOVERY_TIMEOUT = 3
|
10
|
-
HOSTS_DEAD = []
|
11
|
-
HOSTS_FAILED = {}
|
12
|
-
AUTO_RECOVERY_ENABLED = true
|
13
|
-
|
14
8
|
class << self
|
15
9
|
|
16
|
-
def prepare_connection
|
17
|
-
|
18
|
-
|
19
|
-
conn = AMQP.connect(RJ.config.servers.first, auto_recovery: AUTO_RECOVERY_ENABLED)
|
20
|
-
init_auto_recovery(conn) if AUTO_RECOVERY_ENABLED
|
21
|
-
end
|
10
|
+
def prepare_connection
|
11
|
+
conn = Bunny.new(RJ.config.server)
|
12
|
+
conn.start unless conn.connected? || conn.connecting?
|
22
13
|
conn
|
23
14
|
end
|
24
15
|
|
25
|
-
def create_channel(connection)
|
26
|
-
AMQP::Channel.new(connection, auto_recovery: AUTO_RECOVERY_ENABLED)
|
27
|
-
end
|
28
|
-
|
29
|
-
def init_auto_recovery(connection)
|
30
|
-
connection.on_recovery do |conn, opts|
|
31
|
-
HOSTS_DEAD.clear
|
32
|
-
HOSTS_FAILED.clear
|
33
|
-
url = url_from_opts opts
|
34
|
-
RJ.logger.warn "Connection to #{url} recovered."
|
35
|
-
end
|
36
|
-
|
37
|
-
connection.on_open do |conn, opts|
|
38
|
-
RJ.logger.info "Connected."
|
39
|
-
end
|
40
|
-
|
41
|
-
# connection.on_tcp_connection_loss do |conn, opts|
|
42
|
-
# sleep 1
|
43
|
-
# restore_from_connection_failure(conn, opts) unless conn.closed?
|
44
|
-
# end
|
45
|
-
|
46
|
-
connection.on_tcp_connection_failure do |opts|
|
47
|
-
sleep 1
|
48
|
-
restore_from_connection_failure(connection, opts)
|
49
|
-
end
|
50
|
-
|
51
|
-
|
52
|
-
# connection.before_recovery do |conn, opts|
|
53
|
-
# RJ.logger.info "before_recovery"
|
54
|
-
# end
|
55
|
-
|
56
|
-
# connection.on_possible_authentication_failure do |conn, opts|
|
57
|
-
# puts opts.inspect
|
58
|
-
# # restore_from_connection_failure(conn, opts)
|
59
|
-
# end
|
60
|
-
|
61
|
-
# connection.on_connection_interruption do |conn|
|
62
|
-
# # restore_from_connection_failure(conn, opts)
|
63
|
-
# end
|
64
|
-
end
|
65
|
-
|
66
|
-
private
|
67
|
-
|
68
|
-
def restore_from_connection_failure(connection, opts)
|
69
|
-
url = opts.empty? ? RJ.config.servers.first : url_from_opts(opts)
|
70
|
-
HOSTS_FAILED[url] ||= Time.now
|
71
|
-
|
72
|
-
if HOSTS_FAILED[url] + RECOVERY_TIMEOUT < Time.now
|
73
|
-
# reconnect to another host
|
74
|
-
HOSTS_DEAD.push(url) unless HOSTS_DEAD.include?(url)
|
75
|
-
new_url = (RJ.config.servers.dup - HOSTS_DEAD.dup).first
|
76
|
-
if new_url
|
77
|
-
reconnect_to(connection, new_url)
|
78
|
-
else
|
79
|
-
# all hosts is dead
|
80
|
-
end
|
81
|
-
else
|
82
|
-
# reconnect to the same host
|
83
|
-
reconnect_to(connection, url)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def reconnect_to(connection, url)
|
88
|
-
if connection
|
89
|
-
RJ.logger.warn "Trying to reconnect to #{url}..."
|
90
|
-
connection.reconnect_to(url, 2)
|
91
|
-
else
|
92
|
-
RJ.logger.warn "Trying to connect to #{url}..."
|
93
|
-
connection = AMQP.connect(url, auto_recovery: true)
|
94
|
-
init_auto_recovery(connection)
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
def url_from_opts(opts = {})
|
99
|
-
return "" unless opts
|
100
|
-
return "" if opts.empty?
|
101
|
-
|
102
|
-
scheme = opts[:scheme] || "amqp"
|
103
|
-
vhost = opts[:vhost] || "/"
|
104
|
-
vhost = "/#{vhost}" unless vhost[0] == '/'
|
105
|
-
use_default_port = (scheme == 'amqp' && opts[:port] == 5672) || (scheme == 'amqps' && opts[:port] == 5673)
|
106
|
-
use_default_credentials = opts[:user] == 'guest' && opts[:pass] == 'guest'
|
107
|
-
|
108
|
-
s = ""
|
109
|
-
s << scheme
|
110
|
-
s << "://"
|
111
|
-
s << "#{opts[:user]}:#{opts[:pass]}@" unless use_default_credentials
|
112
|
-
s << opts[:host]
|
113
|
-
s << ":#{opts[:port]}" unless use_default_port
|
114
|
-
s << vhost
|
115
|
-
s
|
116
|
-
end
|
117
16
|
end
|
118
17
|
end
|
119
18
|
end
|
@@ -64,7 +64,7 @@ module RabbitJobs
|
|
64
64
|
@data = {
|
65
65
|
error_log: true,
|
66
66
|
workers: {},
|
67
|
-
|
67
|
+
server: 'amqp://localhost/',
|
68
68
|
prefix: 'rabbit_jobs',
|
69
69
|
queues: {}
|
70
70
|
}
|
@@ -98,20 +98,9 @@ module RabbitJobs
|
|
98
98
|
@data[:error_log] = false
|
99
99
|
end
|
100
100
|
|
101
|
-
def servers(*value)
|
102
|
-
unless value.empty?
|
103
|
-
@data[:servers] = value.map(&:to_s).map(&:strip).keep_if{|url|!url.empty?}.map {|url|
|
104
|
-
normalize_url(url)
|
105
|
-
}
|
106
|
-
end
|
107
|
-
@data[:servers]
|
108
|
-
end
|
109
|
-
|
110
101
|
def server(value = nil)
|
111
|
-
|
112
|
-
|
113
|
-
@data[:servers] ||= []
|
114
|
-
@data[:servers] << value unless @data[:servers].include?(value)
|
102
|
+
@data[:server] = value.to_s.strip if value && value.length > 0
|
103
|
+
@data[:server]
|
115
104
|
end
|
116
105
|
|
117
106
|
def prefix(value = nil)
|
@@ -179,24 +168,13 @@ module RabbitJobs
|
|
179
168
|
convert_yaml_config(yaml[Rails.env.to_s])
|
180
169
|
else
|
181
170
|
@data = {prefix: nil, queues: {}}
|
182
|
-
%w(prefix mail_errors_to mail_errors_from).each do |m|
|
171
|
+
%w(server prefix mail_errors_to mail_errors_from).each do |m|
|
183
172
|
self.send(m, yaml[m])
|
184
173
|
end
|
185
|
-
yaml['servers'].split(",").each do |value|
|
186
|
-
server normalize_url(value)
|
187
|
-
end
|
188
174
|
yaml['queues'].each do |name, params|
|
189
175
|
queue name, symbolize_keys!(params) || {}
|
190
176
|
end
|
191
177
|
end
|
192
178
|
end
|
193
|
-
|
194
|
-
private
|
195
|
-
|
196
|
-
def normalize_url(url_string)
|
197
|
-
uri = URI.parse(url_string)
|
198
|
-
uri.path = "" if uri.path.to_s == "/"
|
199
|
-
uri.to_s
|
200
|
-
end
|
201
179
|
end
|
202
180
|
end
|
@@ -1,8 +1,7 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
|
3
3
|
require 'json'
|
4
|
-
require '
|
5
|
-
require 'eventmachine'
|
4
|
+
require 'bunny'
|
6
5
|
require 'uri'
|
7
6
|
require 'active_support'
|
8
7
|
require 'active_support/core_ext/module'
|
@@ -11,20 +10,14 @@ module RabbitJobs
|
|
11
10
|
module Publisher
|
12
11
|
extend self
|
13
12
|
|
14
|
-
mattr_accessor :_connection
|
15
|
-
mattr_accessor :_channel
|
16
|
-
|
17
13
|
def amqp_connection
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
def amqp_channel
|
22
|
-
self._connection ||= AmqpHelper.prepare_connection(self._connection)
|
23
|
-
self._channel ||= AmqpHelper.create_channel(self._connection)
|
14
|
+
Thread.current[:rj_publisher_connection] ||= AmqpHelper.prepare_connection
|
24
15
|
end
|
25
16
|
|
26
17
|
def cleanup
|
27
|
-
|
18
|
+
conn = Thread.current[:rj_publisher_connection]
|
19
|
+
conn.close if conn && conn.status != :not_connected
|
20
|
+
Thread.current[:rj_publisher_connection] = nil
|
28
21
|
end
|
29
22
|
|
30
23
|
def publish_to(routing_key, klass, *params, &block)
|
@@ -45,25 +38,21 @@ module RabbitJobs
|
|
45
38
|
raise ArgumentError.new("Need to pass exchange name") if ex.size > 0 && ex[:name].to_s.empty?
|
46
39
|
|
47
40
|
begin
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
AMQP::Exchange.new(self.amqp_channel, ex[:type] || :direct, ex[:name].to_s, Configuration::DEFAULT_EXCHANGE_PARAMS.merge(ex[:params] || {})) do |exchange|
|
52
|
-
exchange.publish(payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({key: routing_key.to_sym})) do
|
53
|
-
yield if block_given?
|
54
|
-
end
|
55
|
-
end
|
41
|
+
exchange = if ex.size > 0
|
42
|
+
exchange_opts = Configuration::DEFAULT_EXCHANGE_PARAMS.merge(ex[:params] || {}).merge({type: (ex[:type] || :direct)})
|
43
|
+
amqp_connection.channel.exchange(ex[:name].to_s, exchange_opts)
|
56
44
|
else
|
57
|
-
|
58
|
-
yield if block_given?
|
59
|
-
end
|
45
|
+
amqp_connection.channel.default_exchange
|
60
46
|
end
|
47
|
+
|
48
|
+
exchange.publish(payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({key: routing_key.to_sym}))
|
61
49
|
rescue
|
62
50
|
RJ.logger.warn $!.message
|
63
51
|
RJ.logger.warn $!.backtrace.join("\n")
|
64
52
|
raise $!
|
65
53
|
end
|
66
54
|
|
55
|
+
yield if block_given?
|
67
56
|
true
|
68
57
|
end
|
69
58
|
|
@@ -75,20 +64,13 @@ module RabbitJobs
|
|
75
64
|
|
76
65
|
routing_keys.map(&:to_sym).each do |routing_key|
|
77
66
|
queue_name = RJ.config.queue_name(routing_key)
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
RJ.logger.error "Cannot purge queue #{queue_name}." unless ret.is_a?(AMQ::Protocol::Queue::PurgeOk)
|
82
|
-
messages_count += ret.message_count
|
83
|
-
count -= 1
|
84
|
-
if count == 0
|
85
|
-
yield(messages_count) if block_given?
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
67
|
+
queue = amqp_connection.queue(queue_name, RJ.config[:queues][routing_key])
|
68
|
+
messages_count += queue.status[:message_count]
|
69
|
+
queue.delete
|
90
70
|
end
|
91
71
|
|
72
|
+
yield(messages_count) if block_given?
|
73
|
+
|
92
74
|
messages_count
|
93
75
|
end
|
94
76
|
end
|
@@ -70,7 +70,6 @@ module RabbitJobs
|
|
70
70
|
end
|
71
71
|
|
72
72
|
def rufus_scheduler
|
73
|
-
raise "Cannot start without eventmachine running." unless EM.reactor_running?
|
74
73
|
@rufus_scheduler ||= Rufus::Scheduler.start_new
|
75
74
|
end
|
76
75
|
|
@@ -89,30 +88,27 @@ module RabbitJobs
|
|
89
88
|
|
90
89
|
$0 = self.process_name || "rj_scheduler"
|
91
90
|
|
92
|
-
|
93
|
-
RJ.run do
|
94
|
-
load_schedule!
|
95
|
-
|
96
|
-
check_shutdown = Proc.new {
|
97
|
-
if @shutdown
|
98
|
-
RJ.stop
|
99
|
-
RJ.logger.info "Stopped."
|
91
|
+
RJ.logger.info "Started."
|
100
92
|
|
101
|
-
|
102
|
-
|
103
|
-
}
|
93
|
+
processed_count = 0
|
94
|
+
load_schedule!
|
104
95
|
|
96
|
+
while true
|
97
|
+
sleep 1
|
105
98
|
if time > 0
|
106
|
-
|
107
|
-
|
99
|
+
time -= 1
|
100
|
+
if time == 0
|
101
|
+
shutdown
|
108
102
|
end
|
109
103
|
end
|
110
104
|
|
111
|
-
|
112
|
-
|
113
|
-
|
105
|
+
if @shutdown
|
106
|
+
RJ.logger.info "Processed jobs: #{processed_count}."
|
107
|
+
RJ.logger.info "Stopped."
|
114
108
|
|
115
|
-
|
109
|
+
File.delete(self.pidfile) if self.pidfile && File.exists?(self.pidfile)
|
110
|
+
return true
|
111
|
+
end
|
116
112
|
end
|
117
113
|
rescue => e
|
118
114
|
error = $!
|
data/lib/rabbit_jobs/version.rb
CHANGED
data/lib/rabbit_jobs/worker.rb
CHANGED
@@ -4,18 +4,14 @@ module RabbitJobs
|
|
4
4
|
class Worker
|
5
5
|
attr_accessor :pidfile, :background, :process_name, :worker_pid
|
6
6
|
|
7
|
-
attr_accessor :_connection, :_channel
|
8
|
-
|
9
7
|
def amqp_connection
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
def amqp_channel
|
14
|
-
self._channel ||= AmqpHelper.create_channel(self._connection)
|
8
|
+
Thread.current[:rj_worker_connection] ||= AmqpHelper.prepare_connection
|
15
9
|
end
|
16
10
|
|
17
|
-
def cleanup
|
18
|
-
|
11
|
+
def self.cleanup
|
12
|
+
conn = Thread.current[:rj_worker_connection]
|
13
|
+
conn.close if conn && conn.status != :not_connected
|
14
|
+
Thread.current[:rj_worker_connection] = nil
|
19
15
|
end
|
20
16
|
|
21
17
|
def process_message(metadata, payload)
|
@@ -71,7 +67,7 @@ module RabbitJobs
|
|
71
67
|
end
|
72
68
|
|
73
69
|
# Subscribes to queue and working on jobs
|
74
|
-
def work(time =
|
70
|
+
def work(time = -1)
|
75
71
|
return false unless startup
|
76
72
|
|
77
73
|
$0 = self.process_name || "rj_worker (#{queues.join(', ')})"
|
@@ -79,59 +75,58 @@ module RabbitJobs
|
|
79
75
|
processed_count = 0
|
80
76
|
|
81
77
|
begin
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
78
|
+
# amqp_channel.prefetch(1)
|
79
|
+
|
80
|
+
amqp_channel = amqp_connection.create_channel
|
81
|
+
|
82
|
+
queue_objects = []
|
83
|
+
queues.each do |routing_key|
|
84
|
+
RJ.logger.info "Subscribing to #{queue_name(routing_key)}"
|
85
|
+
|
86
|
+
routing_key = routing_key.to_sym
|
87
|
+
queue = amqp_channel.queue(queue_name(routing_key), queue_params(routing_key))
|
88
|
+
queue_objects << queue
|
89
|
+
explicit_ack = !!queue_params(routing_key)[:ack]
|
90
|
+
|
91
|
+
queue.subscribe(ack: explicit_ack) do |delivery_info, properties, payload|
|
92
|
+
if RJ.run_before_process_message_callbacks
|
93
|
+
begin
|
94
|
+
processed_count += 1 if process_message(properties, payload)
|
95
|
+
rescue
|
96
|
+
RJ.logger.warn "process_message failed. payload: #{payload.inspect}"
|
97
|
+
RJ.logger.warn $!.inspect
|
98
|
+
$!.backtrace.each {|l| RJ.logger.warn l}
|
99
|
+
end
|
100
|
+
amqp_channel.ack(delivery_info.delivery_tag, false) if explicit_ack
|
101
|
+
else
|
102
|
+
RJ.logger.warn "before_process_message hook failed, requeuing payload: #{payload.inspect}"
|
103
|
+
amqp_channel.nack(delivery_info.delivery_tag, true) if explicit_ack
|
104
|
+
end
|
88
105
|
|
89
|
-
|
106
|
+
if @shutdown
|
107
|
+
queue_objects.each {|q| q.unsubscribe}
|
90
108
|
end
|
91
|
-
}
|
92
|
-
|
93
|
-
amqp_connection
|
94
|
-
amqp_channel.prefetch(1)
|
95
|
-
|
96
|
-
queues.each do |routing_key|
|
97
|
-
routing_key = routing_key.to_sym
|
98
|
-
|
99
|
-
amqp_channel.queue(queue_name(routing_key), queue_params(routing_key)) { |queue, declare_ok|
|
100
|
-
explicit_ack = !!queue_params(routing_key)[:ack]
|
101
|
-
|
102
|
-
RJ.logger.info "Subscribing to #{queue_name(routing_key)}"
|
103
|
-
queue.subscribe(ack: explicit_ack) do |metadata, payload|
|
104
|
-
if RJ.run_before_process_message_callbacks
|
105
|
-
begin
|
106
|
-
processed_count += 1 if process_message(metadata, payload)
|
107
|
-
rescue
|
108
|
-
RJ.logger.warn "process_message failed. payload: #{payload.inspect}"
|
109
|
-
RJ.logger.warn $!.inspect
|
110
|
-
$!.backtrace.each {|l| RJ.logger.warn l}
|
111
|
-
end
|
112
|
-
metadata.ack if explicit_ack
|
113
|
-
else
|
114
|
-
RJ.logger.warn "before_process_message hook failed, requeuing payload: #{payload.inspect}"
|
115
|
-
metadata.reject(requeue: true) if explicit_ack
|
116
|
-
end
|
117
|
-
|
118
|
-
check_shutdown.call
|
119
|
-
end
|
120
|
-
}
|
121
109
|
end
|
110
|
+
end
|
122
111
|
|
112
|
+
RJ.logger.info "Started."
|
113
|
+
|
114
|
+
while true
|
115
|
+
sleep 1
|
123
116
|
if time > 0
|
124
|
-
|
125
|
-
|
126
|
-
|
117
|
+
time -= 1
|
118
|
+
if time == 0
|
119
|
+
shutdown
|
127
120
|
end
|
128
121
|
end
|
129
122
|
|
130
|
-
|
131
|
-
|
132
|
-
|
123
|
+
if @shutdown
|
124
|
+
RJ.logger.info "Processed jobs: #{processed_count}."
|
125
|
+
RJ.logger.info "Stopped."
|
133
126
|
|
134
|
-
|
127
|
+
File.delete(self.pidfile) if self.pidfile && File.exists?(self.pidfile)
|
128
|
+
return true
|
129
|
+
end
|
135
130
|
end
|
136
131
|
rescue
|
137
132
|
error = $!
|
data/lib/rabbit_jobs.rb
CHANGED
@@ -19,82 +19,17 @@ require 'rabbit_jobs/tasks'
|
|
19
19
|
module RabbitJobs
|
20
20
|
extend self
|
21
21
|
|
22
|
-
def
|
23
|
-
|
24
|
-
raise if EM.reactor_running?
|
25
|
-
|
26
|
-
EM.run {
|
27
|
-
yield
|
28
|
-
}
|
29
|
-
end
|
30
|
-
|
31
|
-
alias_method :run, :start
|
32
|
-
|
33
|
-
def stop
|
34
|
-
RJ::Publisher.cleanup
|
35
|
-
|
36
|
-
if AMQP.connection
|
37
|
-
AMQP.connection.disconnect {
|
38
|
-
AMQP.connection = nil
|
39
|
-
AMQP.channel = nil
|
40
|
-
EM.stop {
|
41
|
-
yield if block_given?
|
42
|
-
}
|
43
|
-
}
|
44
|
-
else
|
45
|
-
EM.stop {
|
46
|
-
yield if block_given?
|
47
|
-
}
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def running?
|
52
|
-
EM.reactor_running?
|
53
|
-
end
|
54
|
-
|
55
|
-
def publish_to(routing_key, klass, *params, &block)
|
56
|
-
if RJ.running?
|
57
|
-
RJ::Publisher.publish_to(routing_key, klass, *params, &block)
|
58
|
-
else
|
59
|
-
RJ.run {
|
60
|
-
RJ::Publisher.publish_to(routing_key, klass, *params) {
|
61
|
-
RJ.stop {
|
62
|
-
yield if block_given?
|
63
|
-
}
|
64
|
-
}
|
65
|
-
}
|
66
|
-
end
|
22
|
+
def publish_to(routing_key, klass, *params)
|
23
|
+
RJ::Publisher.publish_to(routing_key, klass, *params)
|
67
24
|
end
|
68
25
|
|
69
26
|
def direct_publish_to(routing_key, payload, ex = {}, &block)
|
70
|
-
|
71
|
-
|
72
|
-
else
|
73
|
-
RJ.run {
|
74
|
-
RJ::Publisher.direct_publish_to(routing_key, payload, ex) {
|
75
|
-
RJ.stop {
|
76
|
-
yield if block_given?
|
77
|
-
}
|
78
|
-
}
|
79
|
-
}
|
80
|
-
end
|
27
|
+
RJ::Publisher.direct_publish_to(routing_key, payload, ex, &block)
|
28
|
+
yield if block_given?
|
81
29
|
end
|
82
30
|
|
83
31
|
def purge_queue(*routing_keys, &block)
|
84
|
-
|
85
|
-
RJ::Publisher.purge_queue(*routing_keys, &block)
|
86
|
-
else
|
87
|
-
messages_count = 0
|
88
|
-
RJ.run {
|
89
|
-
RJ::Publisher.purge_queue(*routing_keys) { |count|
|
90
|
-
messages_count = count
|
91
|
-
RJ.stop {
|
92
|
-
yield(count) if block_given?
|
93
|
-
}
|
94
|
-
}
|
95
|
-
}
|
96
|
-
messages_count
|
97
|
-
end
|
32
|
+
RJ::Publisher.purge_queue(*routing_keys, &block)
|
98
33
|
end
|
99
34
|
|
100
35
|
attr_writer :logger
|
data/rabbit_jobs.gemspec
CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.require_paths = ["lib"]
|
17
17
|
gem.version = RabbitJobs::VERSION
|
18
18
|
|
19
|
-
gem.add_dependency "
|
19
|
+
gem.add_dependency "bunny", "0.9.0.pre7"
|
20
20
|
gem.add_dependency "rake"
|
21
21
|
gem.add_dependency "rufus-scheduler", "~> 2.0"
|
22
22
|
gem.add_dependency "rails", ">= 3.0"
|
@@ -42,19 +42,11 @@ describe RabbitJobs::Publisher do
|
|
42
42
|
count = 1000
|
43
43
|
published = 0
|
44
44
|
time = Benchmark.measure {
|
45
|
-
|
46
|
-
|
47
|
-
RJ.publish_to(:rspec_queue, TestJob) {
|
48
|
-
published += 1
|
49
|
-
if published == count
|
50
|
-
RJ.purge_queue(:rspec_queue, :rspec_queue2, :rspec_queue3) { |removed|
|
51
|
-
removed.should == 1000
|
52
|
-
RJ.stop
|
53
|
-
}
|
54
|
-
end
|
55
|
-
}
|
56
|
-
}
|
45
|
+
count.times {
|
46
|
+
RJ.publish_to(:rspec_queue, TestJob)
|
57
47
|
}
|
48
|
+
removed = RJ.purge_queue(:rspec_queue, :rspec_queue2, :rspec_queue3)
|
49
|
+
removed.should == 1000
|
58
50
|
}
|
59
51
|
puts time
|
60
52
|
end
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
require 'spec_helper'
|
3
|
-
require 'timecop'
|
4
|
-
require 'eventmachine'
|
5
3
|
|
6
4
|
describe RabbitJobs::Worker do
|
7
5
|
it 'should listen for messages' do
|
@@ -10,36 +8,22 @@ describe RabbitJobs::Worker do
|
|
10
8
|
c.queue 'rspec_durable_queue', auto_delete: false, durable: true, ack: true
|
11
9
|
end
|
12
10
|
|
13
|
-
RJ.
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
RabbitJobs.publish(PrintTimeJob, Time.now) {
|
18
|
-
count -= 1
|
19
|
-
if count <= 0
|
20
|
-
Timecop.freeze(Time.now - 4600) {
|
21
|
-
count = 5
|
22
|
-
5.times {
|
23
|
-
RabbitJobs.publish(JobWithExpire) {
|
24
|
-
count -= 1
|
25
|
-
if count <= 0
|
26
|
-
RabbitJobs.publish(JobWithErrorHook) {
|
27
|
-
RJ.stop
|
28
|
-
}
|
29
|
-
end
|
30
|
-
}
|
31
|
-
}
|
32
|
-
}
|
33
|
-
end
|
34
|
-
}
|
35
|
-
}
|
36
|
-
}
|
11
|
+
RJ::Publisher.purge_queue('rspec_durable_queue')
|
12
|
+
count = 5
|
13
|
+
5.times {
|
14
|
+
RabbitJobs.publish(PrintTimeJob, Time.now)
|
37
15
|
}
|
38
16
|
|
17
|
+
Timecop.freeze(Time.now - 4600) {
|
18
|
+
5.times { RabbitJobs.publish(JobWithExpire) }
|
19
|
+
}
|
20
|
+
|
21
|
+
RabbitJobs.publish(JobWithErrorHook)
|
22
|
+
|
39
23
|
worker = RabbitJobs::Worker.new
|
40
24
|
|
41
25
|
worker.work(1) # work for 1 second
|
42
|
-
RJ
|
26
|
+
RJ::Publisher.purge_queue('rspec_durable_queue')
|
43
27
|
end
|
44
28
|
|
45
29
|
it 'should allow to publish jobs from worker' do
|
@@ -48,13 +32,8 @@ describe RabbitJobs::Worker do
|
|
48
32
|
c.queue 'rspec_durable_queue', auto_delete: false, durable: true, ack: true
|
49
33
|
end
|
50
34
|
|
51
|
-
RJ.
|
52
|
-
|
53
|
-
RabbitJobs.publish(JobWithPublish) {
|
54
|
-
RJ.stop
|
55
|
-
}
|
56
|
-
}
|
57
|
-
}
|
35
|
+
RJ::Publisher.purge_queue('rspec_durable_queue')
|
36
|
+
RabbitJobs.publish(JobWithPublish)
|
58
37
|
|
59
38
|
worker = RabbitJobs::Worker.new
|
60
39
|
|
data/spec/spec_helper.rb
CHANGED
@@ -73,7 +73,7 @@ describe RabbitJobs::Configuration do
|
|
73
73
|
|
74
74
|
it 'returns settings on some methods' do
|
75
75
|
RabbitJobs.config.error_log == true
|
76
|
-
RabbitJobs.config.
|
76
|
+
RabbitJobs.config.server.should == 'amqp://localhost/'
|
77
77
|
RabbitJobs.config.routing_keys.should == []
|
78
78
|
RabbitJobs.config.prefix.should == 'rabbit_jobs'
|
79
79
|
RabbitJobs.config.queue_name('default').should == 'rabbit_jobs#default'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rabbit_jobs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,19 +12,19 @@ cert_chain: []
|
|
12
12
|
date: 2013-03-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
16
|
-
requirement: &
|
15
|
+
name: bunny
|
16
|
+
requirement: &21380680 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - =
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
21
|
+
version: 0.9.0.pre7
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *21380680
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &21379160 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *21379160
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rufus-scheduler
|
38
|
-
requirement: &
|
38
|
+
requirement: &21374960 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '2.0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *21374960
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rails
|
49
|
-
requirement: &
|
49
|
+
requirement: &21399100 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '3.0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *21399100
|
58
58
|
description: Background jobs on RabbitMQ
|
59
59
|
email:
|
60
60
|
- lazureykis@gmail.com
|
@@ -111,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
111
111
|
version: '0'
|
112
112
|
segments:
|
113
113
|
- 0
|
114
|
-
hash:
|
114
|
+
hash: 2351435873662003643
|
115
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
116
|
none: false
|
117
117
|
requirements:
|
@@ -120,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
120
|
version: '0'
|
121
121
|
segments:
|
122
122
|
- 0
|
123
|
-
hash:
|
123
|
+
hash: 2351435873662003643
|
124
124
|
requirements: []
|
125
125
|
rubyforge_project:
|
126
126
|
rubygems_version: 1.8.11
|