rabbit_jobs 0.4.14 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rabbit_jobs.rb CHANGED
@@ -24,7 +24,6 @@ module RabbitJobs
24
24
  raise if EM.reactor_running?
25
25
 
26
26
  EM.run {
27
- AmqpHelper.prepare_connection
28
27
  yield
29
28
  }
30
29
  end
@@ -13,62 +13,61 @@ module RabbitJobs
13
13
 
14
14
  class << self
15
15
 
16
- def prepare_connection
17
- if !AMQP.connection || AMQP.connection.closed?
16
+ def prepare_connection(conn)
17
+ if !conn
18
18
  RJ.logger.info("Connecting to #{RJ.config.servers.first.to_s}...")
19
- AMQP.connection = AMQP.connect(RJ.config.servers.first, auto_recovery: AUTO_RECOVERY_ENABLED)
20
- init_auto_recovery if AUTO_RECOVERY_ENABLED
19
+ conn = AMQP.connect(RJ.config.servers.first, auto_recovery: AUTO_RECOVERY_ENABLED)
20
+ init_auto_recovery(conn) if AUTO_RECOVERY_ENABLED
21
+ elsif conn.closed?
22
+ conn.reconnect
21
23
  end
24
+ conn
22
25
  end
23
26
 
24
- def prepare_channel
25
- AMQP.channel ||= AMQP::Channel.new(AMQP.connection, auto_recovery: AUTO_RECOVERY_ENABLED)
27
+ def create_channel(connection)
28
+ AMQP::Channel.new(connection, auto_recovery: AUTO_RECOVERY_ENABLED)
26
29
  end
27
30
 
28
- def init_auto_recovery
29
- unless $auto_recovery_initiated
30
- $auto_recovery_initiated = true
31
-
32
- AMQP.connection.on_recovery do |conn, opts|
33
- HOSTS_DEAD.clear
34
- HOSTS_FAILED.clear
35
- url = url_from_opts opts
36
- RJ.logger.warn "Connection to #{url} recovered."
37
- end
31
+ def init_auto_recovery(connection)
32
+ connection.on_recovery do |conn, opts|
33
+ HOSTS_DEAD.clear
34
+ HOSTS_FAILED.clear
35
+ url = url_from_opts opts
36
+ RJ.logger.warn "Connection to #{url} recovered."
37
+ end
38
38
 
39
- AMQP.connection.on_open do |conn, opts|
40
- RJ.logger.info "Connected."
41
- end
39
+ connection.on_open do |conn, opts|
40
+ RJ.logger.info "Connected."
41
+ end
42
42
 
43
- AMQP.connection.on_tcp_connection_loss do |conn, opts|
44
- sleep 2
45
- restore_from_connection_failure(opts)
46
- end
43
+ connection.on_tcp_connection_loss do |conn, opts|
44
+ sleep 2
45
+ restore_from_connection_failure(conn, opts)
46
+ end
47
47
 
48
- AMQP.connection.on_tcp_connection_failure do |opts|
49
- sleep 2
50
- restore_from_connection_failure(opts)
51
- end
48
+ connection.on_tcp_connection_failure do |opts|
49
+ sleep 2
50
+ restore_from_connection_failure(connection, opts)
51
+ end
52
52
 
53
53
 
54
- # AMQP.connection.before_recovery do |conn, opts|
55
- # RJ.logger.info "before_recovery"
56
- # end
54
+ # connection.before_recovery do |conn, opts|
55
+ # RJ.logger.info "before_recovery"
56
+ # end
57
57
 
58
- # AMQP.connection.on_possible_authentication_failure do |conn, opts|
59
- # puts opts.inspect
60
- # # restore_from_connection_failure(opts)
61
- # end
58
+ # connection.on_possible_authentication_failure do |conn, opts|
59
+ # puts opts.inspect
60
+ # # restore_from_connection_failure(conn, opts)
61
+ # end
62
62
 
63
- # AMQP.connection.on_connection_interruption do |conn|
64
- # # restore_from_connection_failure(opts)
65
- # end
66
- end
63
+ # connection.on_connection_interruption do |conn|
64
+ # # restore_from_connection_failure(conn, opts)
65
+ # end
67
66
  end
68
67
 
69
68
  private
70
69
 
71
- def restore_from_connection_failure(opts)
70
+ def restore_from_connection_failure(connection, opts)
72
71
  url = opts.empty? ? RJ.config.servers.first : url_from_opts(opts)
73
72
  HOSTS_FAILED[url] ||= Time.now
74
73
 
@@ -77,24 +76,24 @@ module RabbitJobs
77
76
  HOSTS_DEAD.push(url) unless HOSTS_DEAD.include?(url)
78
77
  new_url = (RJ.config.servers.dup - HOSTS_DEAD.dup).first
79
78
  if new_url
80
- reconnect_to(new_url)
79
+ reconnect_to(connection, new_url)
81
80
  else
82
81
  # all hosts is dead
83
82
  end
84
83
  else
85
84
  # reconnect to the same host
86
- reconnect_to(url)
85
+ reconnect_to(connection, url)
87
86
  end
88
87
  end
89
88
 
90
- def reconnect_to(url)
91
- if AMQP.connection
89
+ def reconnect_to(connection, url)
90
+ if connection
92
91
  RJ.logger.warn "Trying to reconnect to #{url}..."
93
- AMQP.connection.reconnect_to(url, 2)
92
+ connection.reconnect_to(url, 2)
94
93
  else
95
94
  RJ.logger.warn "Trying to connect to #{url}..."
96
- AMQP.connection = AMQP.connect(url, auto_recovery: true)
97
- init_auto_recovery
95
+ connection = AMQP.connect(url, auto_recovery: true)
96
+ init_auto_recovery(connection)
98
97
  end
99
98
  end
100
99
 
@@ -9,6 +9,17 @@ module RabbitJobs
9
9
  module Publisher
10
10
  extend self
11
11
 
12
+ mattr_accessor :_connection
13
+ mattr_accessor :_channel
14
+
15
+ def amqp_connection
16
+ self._connection ||= AmqpHelper.prepare_connection(self._connection)
17
+ end
18
+
19
+ def amqp_channel
20
+ self._channel ||= AmqpHelper.create_channel(self._connection)
21
+ end
22
+
12
23
  def publish(klass, *params, &block)
13
24
  publish_to(RJ.config.default_queue, klass, *params, &block)
14
25
  end
@@ -31,16 +42,16 @@ module RabbitJobs
31
42
  raise ArgumentError.new("Need to pass exchange name") if ex.size > 0 && ex[:name].to_s.empty?
32
43
 
33
44
  begin
34
- AmqpHelper.prepare_channel
45
+ amqp_connection
35
46
 
36
47
  if ex.size > 0
37
- AMQP::Exchange.new(AMQP.channel, :direct, ex[:name].to_s, Configuration::DEFAULT_EXCHANGE_PARAMS.merge(ex[:params] || {})) do |exchange|
48
+ AMQP::Exchange.new(self.amqp_channel, :direct, ex[:name].to_s, Configuration::DEFAULT_EXCHANGE_PARAMS.merge(ex[:params] || {})) do |exchange|
38
49
  exchange.publish(payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({key: routing_key.to_sym})) do
39
50
  yield if block_given?
40
51
  end
41
52
  end
42
53
  else
43
- AMQP.channel.default_exchange.publish(payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({key: routing_key.to_sym})) do
54
+ self.amqp_channel.default_exchange.publish(payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({key: routing_key.to_sym})) do
44
55
  yield if block_given?
45
56
  end
46
57
  end
@@ -59,11 +70,9 @@ module RabbitJobs
59
70
  messages_count = 0
60
71
  count = routing_keys.count
61
72
 
62
- AmqpHelper.prepare_channel
63
-
64
73
  routing_keys.map(&:to_sym).each do |routing_key|
65
74
  queue_name = RJ.config.queue_name(routing_key)
66
- AMQP.channel.queue(queue_name, RJ.config[:queues][routing_key]) do |queue, declare_ok|
75
+ self.amqp_channel.queue(queue_name, RJ.config[:queues][routing_key]) do |queue, declare_ok|
67
76
  queue.status do |messages, consumers|
68
77
  queue.purge do |ret|
69
78
  RJ.logger.error "Cannot purge queue #{queue_name}." unless ret.is_a?(AMQ::Protocol::Queue::PurgeOk)
@@ -1,5 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
3
  module RabbitJobs
4
- VERSION = "0.4.14"
4
+ VERSION = "0.5.0"
5
5
  end
@@ -4,6 +4,16 @@ 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
+ def amqp_connection
10
+ self._connection ||= AmqpHelper.prepare_connection(self._connection)
11
+ end
12
+
13
+ def amqp_channel
14
+ self._channel ||= AmqpHelper.create_channel(self._connection)
15
+ end
16
+
7
17
  def process_message(metadata, payload)
8
18
  job = RJ::Job.parse(payload)
9
19
 
@@ -73,18 +83,16 @@ module RabbitJobs
73
83
  RJ.logger.info "Stopped."
74
84
 
75
85
  File.delete(self.pidfile) if self.pidfile && File.exists?(self.pidfile)
76
- # RJ.logger.close
77
- # exit!
78
86
  end
79
87
  }
80
88
 
81
- AmqpHelper.prepare_channel
82
- AMQP.channel.prefetch(1)
89
+ amqp_connection
90
+ amqp_channel.prefetch(1)
83
91
 
84
92
  queues.each do |routing_key|
85
93
  routing_key = routing_key.to_sym
86
94
 
87
- AMQP.channel.queue(queue_name(routing_key), queue_params(routing_key)) { |queue, declare_ok|
95
+ amqp_channel.queue(queue_name(routing_key), queue_params(routing_key)) { |queue, declare_ok|
88
96
  explicit_ack = !!queue_params(routing_key)[:ack]
89
97
 
90
98
  RJ.logger.info "Subscribing to #{queue_name(routing_key)}"
@@ -93,6 +101,8 @@ module RabbitJobs
93
101
  processed_count += 1 if process_message(metadata, payload)
94
102
  rescue
95
103
  RJ.logger.warn "process_message failed. payload: #{payload.inspect}"
104
+ RJ.logger.warn $!.inspect
105
+ $!.backtrace.each {|l| RJ.logger.warn l}
96
106
  end
97
107
 
98
108
  metadata.ack if explicit_ack
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.14
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-14 00:00:00.000000000 Z
12
+ date: 2013-02-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: amqp
16
- requirement: &10943520 !ruby/object:Gem::Requirement
16
+ requirement: &13074180 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0.9'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *10943520
24
+ version_requirements: *13074180
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &10942260 !ruby/object:Gem::Requirement
27
+ requirement: &13073280 !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: *10942260
35
+ version_requirements: *13073280
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rufus-scheduler
38
- requirement: &10940980 !ruby/object:Gem::Requirement
38
+ requirement: &13072060 !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: *10940980
46
+ version_requirements: *13072060
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rails
49
- requirement: &10940040 !ruby/object:Gem::Requirement
49
+ requirement: &13466020 !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: *10940040
57
+ version_requirements: *13466020
58
58
  description: Background jobs on RabbitMQ
59
59
  email:
60
60
  - lazureykis@gmail.com
@@ -115,7 +115,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
115
  version: '0'
116
116
  segments:
117
117
  - 0
118
- hash: -2117076599747944409
118
+ hash: -1416154192839369945
119
119
  required_rubygems_version: !ruby/object:Gem::Requirement
120
120
  none: false
121
121
  requirements:
@@ -124,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
124
  version: '0'
125
125
  segments:
126
126
  - 0
127
- hash: -2117076599747944409
127
+ hash: -1416154192839369945
128
128
  requirements: []
129
129
  rubyforge_project:
130
130
  rubygems_version: 1.8.11