rabbit_jobs 0.7.3 → 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
data/examples/bunny ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding : utf-8 -*-
3
+
4
+ require 'bunny'
5
+
6
+
7
+ b = Bunny.new "amqp://localhost/bunny", heartbeat: 2
8
+ b.start
9
+ channel = b.create_channel
10
+ # channel.confirm_select
11
+ exchange = channel.default_exchange
12
+
13
+ i = 0
14
+ loop do
15
+ puts "publishing hello #{i}"
16
+ begin
17
+ exchange.publish("hello #{i}", routing_key: "hello")
18
+ # channel.wait_for_confirms
19
+ rescue Exception => e
20
+ puts e.message
21
+ puts e.backtrace.join("\r\n")
22
+ end
23
+ i += 1
24
+ sleep 1
25
+ end
26
+
27
+ b.stop
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding : utf-8 -*-
3
+
4
+ require 'bunny'
5
+
6
+ # RJ.configure { |c|
7
+ # c.queue "failover_test"
8
+ # c.server "amqp://localhost/bunny"
9
+ # }
10
+
11
+
12
+ b = Bunny.new "amqp://localhost/bunny"
13
+ b.start
14
+ channel = b.create_channel
15
+ queue = channel.queue("hello", durable: true)
16
+
17
+ queue.subscribe do |delivery_info, properties, payload|
18
+ puts "got #{payload}"
19
+ end
20
+
21
+ loop do
22
+ sleep 1
23
+ end
data/examples/client CHANGED
@@ -19,7 +19,13 @@ RJ.configure { |c|
19
19
  i = 0
20
20
  loop do
21
21
  puts "publishing job #{i}"
22
- RJ.publish_to(:failover_test, MyCurrentJob, i)
22
+ begin
23
+ RJ.publish_to(:failover_test, MyCurrentJob, i)
24
+ rescue Exception => e
25
+ RJ::Publisher.amqp_connection.stop
26
+ puts e.message
27
+ puts e.backtrace.join("\r\n")
28
+ end
23
29
  i += 1
24
30
  sleep 1
25
31
  end
@@ -8,7 +8,7 @@ module RabbitJobs
8
8
  class << self
9
9
 
10
10
  def prepare_connection
11
- conn = Bunny.new(RJ.config.server)
11
+ conn = Bunny.new(RJ.config.server, :heartbeat_interval => 5)
12
12
  conn.start unless conn.connected? || conn.connecting?
13
13
  conn
14
14
  end
@@ -63,7 +63,6 @@ module RabbitJobs
63
63
  def initialize
64
64
  @data = {
65
65
  error_log: true,
66
- workers: {},
67
66
  server: 'amqp://localhost/',
68
67
  prefix: 'rabbit_jobs',
69
68
  queues: {}
@@ -125,25 +124,6 @@ module RabbitJobs
125
124
  end
126
125
  end
127
126
 
128
- def workers
129
- @data[:workers] ||= {}
130
- end
131
-
132
- def worker(name, params = {})
133
- raise ArgumentError.new("name is #{name.inspect}") unless name && name.is_a?(String) && name != ""
134
- raise ArgumentError.new("params is #{params.inspect}") unless params && params.is_a?(Hash)
135
- raise ArgumentError.new("params should have :instances and :queues keys.") unless params[:instances] && params[:queues]
136
-
137
- name = name.downcase.to_sym
138
-
139
- @data[:workers] ||= {}
140
- if @data[:workers][name]
141
- @data[:workers][name].merge!(params)
142
- else
143
- @data[:workers][name] = params
144
- end
145
- end
146
-
147
127
  def routing_keys
148
128
  @data[:queues].keys
149
129
  end
@@ -2,6 +2,15 @@
2
2
 
3
3
  module RabbitJobs
4
4
  module Helpers
5
+ def _cleanup_backtrace(trace_lines)
6
+ if defined?(Rails) && Rails.respond_to?(:root)
7
+ rails_root_path = Rails.root.to_s
8
+ trace_lines.dup.keep_if { |l| l[rails_root_path] }
9
+ else
10
+ trace_lines
11
+ end
12
+ end
13
+
5
14
  def symbolize_keys!(hash)
6
15
  hash.inject({}) do |options, (key, value)|
7
16
  options[(key.to_sym rescue key) || key] = value
@@ -26,7 +26,7 @@ module RabbitJobs::Job
26
26
  rescue
27
27
  RJ.logger.warn $!.message
28
28
  RJ.logger.warn(self.to_ruby_string)
29
- RJ.logger.warn RJ::Util.cleanup_backtrace($!.backtrace).join("\n")
29
+ RJ.logger.warn _cleanup_backtrace($!.backtrace).join("\n")
30
30
  run_on_error_hooks($!)
31
31
  RabbitJobs::ErrorMailer.report_error(self, $!)
32
32
  end
@@ -122,7 +122,7 @@ module RabbitJobs::Job
122
122
  rescue
123
123
  RJ.logger.warn "Cannot initialize job."
124
124
  RJ.logger.warn $!.message
125
- RJ.logger.warn RJ::Util.cleanup_backtrace($!.backtrace).join("\n")
125
+ RJ.logger.warn _cleanup_backtrace($!.backtrace).join("\n")
126
126
  RJ.logger.warn "Data received: #{payload.inspect}"
127
127
  :error
128
128
  end
@@ -14,13 +14,17 @@ module RabbitJobs
14
14
  Thread.current[:rj_publisher_connection] ||= AmqpHelper.prepare_connection
15
15
  end
16
16
 
17
+ def amqp_channel
18
+ Thread.current[:rj_publisher_channel] ||= amqp_connection.create_channel
19
+ end
20
+
17
21
  def cleanup
18
22
  conn = Thread.current[:rj_publisher_connection]
19
23
  conn.close if conn && conn.status != :not_connected
20
24
  Thread.current[:rj_publisher_connection] = nil
21
25
  end
22
26
 
23
- def publish_to(routing_key, klass, *params, &block)
27
+ def publish_to(routing_key, klass, *params)
24
28
  raise ArgumentError.new("klass=#{klass.inspect}") unless klass && (klass.is_a?(Class) || klass.is_a?(String))
25
29
  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_sym]
26
30
 
@@ -30,19 +34,19 @@ module RabbitJobs
30
34
  'params' => params
31
35
  }.to_json
32
36
 
33
- direct_publish_to(RJ.config.queue_name(routing_key.to_sym), payload, &block)
37
+ direct_publish_to(RJ.config.queue_name(routing_key.to_sym), payload)
34
38
  end
35
39
 
36
- def direct_publish_to(routing_key, payload, ex = {}, &block)
40
+ def direct_publish_to(routing_key, payload, ex = {})
37
41
  ex = {name: ex} if ex.is_a?(String)
38
42
  raise ArgumentError.new("Need to pass exchange name") if ex.size > 0 && ex[:name].to_s.empty?
39
43
 
40
44
  begin
41
45
  exchange = if ex.size > 0
42
46
  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)
47
+ amqp_channel.exchange(ex[:name].to_s, exchange_opts)
44
48
  else
45
- amqp_connection.channel.default_exchange
49
+ amqp_channel.default_exchange
46
50
  end
47
51
 
48
52
  exchange.publish(payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({key: routing_key.to_sym}))
@@ -52,11 +56,10 @@ module RabbitJobs
52
56
  raise $!
53
57
  end
54
58
 
55
- yield if block_given?
56
59
  true
57
60
  end
58
61
 
59
- def purge_queue(*routing_keys, &block)
62
+ def purge_queue(*routing_keys)
60
63
  raise ArgumentError unless routing_keys && routing_keys.count > 0
61
64
 
62
65
  messages_count = 0
@@ -69,8 +72,6 @@ module RabbitJobs
69
72
  queue.delete
70
73
  end
71
74
 
72
- yield(messages_count) if block_given?
73
-
74
75
  messages_count
75
76
  end
76
77
  end
@@ -1,5 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
3
  module RabbitJobs
4
- VERSION = "0.7.3"
4
+ VERSION = "0.7.4"
5
5
  end
data/lib/rabbit_jobs.rb CHANGED
@@ -4,7 +4,6 @@ require 'rake'
4
4
 
5
5
  require 'rabbit_jobs/version'
6
6
 
7
- require 'rabbit_jobs/util'
8
7
  require 'rabbit_jobs/helpers'
9
8
  require 'rabbit_jobs/amqp_helper'
10
9
  require 'rabbit_jobs/configuration'
@@ -24,13 +23,12 @@ module RabbitJobs
24
23
  RJ::Publisher.publish_to(routing_key, klass, *params)
25
24
  end
26
25
 
27
- def direct_publish_to(routing_key, payload, ex = {}, &block)
28
- RJ::Publisher.direct_publish_to(routing_key, payload, ex, &block)
29
- yield if block_given?
26
+ def direct_publish_to(routing_key, payload, ex = {})
27
+ RJ::Publisher.direct_publish_to(routing_key, payload, ex)
30
28
  end
31
29
 
32
- def purge_queue(*routing_keys, &block)
33
- RJ::Publisher.purge_queue(*routing_keys, &block)
30
+ def purge_queue(*routing_keys)
31
+ RJ::Publisher.purge_queue(*routing_keys)
34
32
  end
35
33
 
36
34
  attr_writer :logger
@@ -1,5 +1,5 @@
1
1
  rabbit_jobs:
2
- url: amqp://example.com/vhost
2
+ server: amqp://example.com/vhost
3
3
  prefix: 'rabbit_jobs'
4
4
  queues:
5
5
  durable_queue:
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,7 @@ end
6
6
 
7
7
  require 'rr'
8
8
  require 'timecop'
9
+ require 'action_mailer'
9
10
 
10
11
  require 'rabbit_jobs'
11
12
  require 'fixtures/jobs'
@@ -6,7 +6,7 @@ describe RabbitJobs::Configuration do
6
6
  RabbitJobs.configure do |c|
7
7
  c.disable_error_log
8
8
 
9
- c.url "amqp://somehost.lan"
9
+ c.server "amqp://somehost.lan"
10
10
 
11
11
  c.prefix 'my_prefix'
12
12
 
@@ -16,20 +16,22 @@ describe RabbitJobs::Configuration do
16
16
 
17
17
  RabbitJobs.config.to_hash.should == {
18
18
  error_log: false,
19
- url: "amqp://somehost.lan",
19
+ server: "amqp://somehost.lan",
20
20
  prefix: "my_prefix",
21
21
  queues: {
22
- "durable_queue" => {
23
- durable: true,
22
+ durable_queue: {
24
23
  auto_delete: false,
24
+ exclusive: false,
25
+ durable: true,
25
26
  ack: true,
26
27
  arguments: {"x-ha-policy"=>"all"}
27
28
  },
28
- "fast_queue" => {
29
- durable: false,
29
+ fast_queue: {
30
30
  auto_delete: true,
31
+ exclusive: false,
32
+ durable: false,
31
33
  ack: false
32
- },
34
+ }
33
35
  }
34
36
  }
35
37
  end
@@ -38,17 +40,19 @@ describe RabbitJobs::Configuration do
38
40
  RabbitJobs.config.load_file(File.expand_path('../../fixtures/config.yml', __FILE__))
39
41
 
40
42
  RabbitJobs.config.to_hash.should == {
41
- url: "amqp://example.com/vhost",
42
- prefix: "my_prefix",
43
+ prefix: "rabbit_jobs",
44
+ server: "amqp://example.com/vhost",
43
45
  queues: {
44
- "durable_queue" => {
46
+ durable_queue: {
45
47
  durable: true,
48
+ exclusive: false,
46
49
  auto_delete: false,
47
50
  ack: true,
48
51
  arguments: {"x-ha-policy"=>"all"}
49
52
  },
50
- "fast_queue" => {
53
+ fast_queue: {
51
54
  durable: false,
55
+ exclusive: false,
52
56
  auto_delete: true,
53
57
  ack: false
54
58
  }
@@ -56,18 +60,12 @@ describe RabbitJobs::Configuration do
56
60
  }
57
61
  end
58
62
 
59
- it 'use default config' do
63
+ it 'use default values for #server and #prefix' do
60
64
  RabbitJobs.config.to_hash.should == {
61
65
  error_log: true,
62
- url: "amqp://localhost/",
66
+ server: "amqp://localhost/",
63
67
  prefix: "rabbit_jobs",
64
- queues: {
65
- "default" => {
66
- auto_delete: false,
67
- ack: true,
68
- durable: true
69
- }
70
- }
68
+ queues: {}
71
69
  }
72
70
  end
73
71
 
@@ -76,6 +74,5 @@ describe RabbitJobs::Configuration do
76
74
  RabbitJobs.config.server.should == 'amqp://localhost/'
77
75
  RabbitJobs.config.routing_keys.should == []
78
76
  RabbitJobs.config.prefix.should == 'rabbit_jobs'
79
- RabbitJobs.config.queue_name('default').should == 'rabbit_jobs#default'
80
77
  end
81
78
  end
@@ -1,6 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  require 'spec_helper'
3
- require 'action_mailer'
4
3
 
5
4
  describe RabbitJobs::ErrorMailer do
6
5
  describe '#enabled?' do
@@ -1,11 +1,12 @@
1
1
  # -*- encoding : utf-8 -*-
2
+ require 'spec_helper'
2
3
 
3
4
  describe RabbitJobs do
4
5
  it 'should pass publish methods to publisher' do
5
- mock(RabbitJobs::Publisher).publish(TestJob, nil, 1, 2, "string")
6
- RabbitJobs.publish(TestJob, nil, 1, 2, "string")
6
+ mock(RJ::Publisher).publish_to('default_queue', TestJob, nil, 1, 2, "string")
7
+ RJ.publish_to('default_queue', TestJob, nil, 1, 2, "string")
7
8
 
8
- mock(RabbitJobs::Publisher).publish_to('default_queue', TestJob, nil, 1, 2, "string")
9
- RabbitJobs.publish_to('default_queue', TestJob, nil, 1, 2, "string")
9
+ mock(RJ::Publisher).direct_publish_to('default_queue', 'hello', {name: "my_exchange"})
10
+ RJ.direct_publish_to('default_queue', 'hello', {name: "my_exchange"})
10
11
  end
11
12
  end
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.7.3
4
+ version: 0.7.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,18 +13,23 @@ date: 2013-03-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bunny
16
- requirement: &7390960 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - =
19
+ - - '='
20
20
  - !ruby/object:Gem::Version
21
21
  version: 0.9.0.pre8
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *7390960
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.9.0.pre8
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: rake
27
- requirement: &7387480 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *7387480
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: rufus-scheduler
38
- requirement: &7410080 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ~>
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '2.0'
44
54
  type: :runtime
45
55
  prerelease: false
46
- version_requirements: *7410080
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: rails
49
- requirement: &7407360 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,7 +69,12 @@ dependencies:
54
69
  version: '3.0'
55
70
  type: :runtime
56
71
  prerelease: false
57
- version_requirements: *7407360
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '3.0'
58
78
  description: Background jobs on RabbitMQ
59
79
  email:
60
80
  - lazureykis@gmail.com
@@ -68,6 +88,8 @@ files:
68
88
  - LICENSE
69
89
  - README.md
70
90
  - Rakefile
91
+ - examples/bunny
92
+ - examples/bunny_server
71
93
  - examples/client
72
94
  - examples/configuration.rb
73
95
  - examples/worker
@@ -81,7 +103,6 @@ files:
81
103
  - lib/rabbit_jobs/publisher.rb
82
104
  - lib/rabbit_jobs/scheduler.rb
83
105
  - lib/rabbit_jobs/tasks.rb
84
- - lib/rabbit_jobs/util.rb
85
106
  - lib/rabbit_jobs/version.rb
86
107
  - lib/rabbit_jobs/worker.rb
87
108
  - lib/tasks/rabbit_jobs.rake
@@ -112,7 +133,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
133
  version: '0'
113
134
  segments:
114
135
  - 0
115
- hash: 3473426678828030662
136
+ hash: -1591772843040716923
116
137
  required_rubygems_version: !ruby/object:Gem::Requirement
117
138
  none: false
118
139
  requirements:
@@ -121,11 +142,23 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
142
  version: '0'
122
143
  segments:
123
144
  - 0
124
- hash: 3473426678828030662
145
+ hash: -1591772843040716923
125
146
  requirements: []
126
147
  rubyforge_project:
127
- rubygems_version: 1.8.11
148
+ rubygems_version: 1.8.25
128
149
  signing_key:
129
150
  specification_version: 3
130
151
  summary: Background jobs on RabbitMQ
131
- test_files: []
152
+ test_files:
153
+ - spec/fixtures/config.yml
154
+ - spec/fixtures/jobs.rb
155
+ - spec/fixtures/schedule.yml
156
+ - spec/integration/publisher_spec.rb
157
+ - spec/integration/scheduler_spec.rb
158
+ - spec/integration/worker_spec.rb
159
+ - spec/spec_helper.rb
160
+ - spec/unit/configuration_spec.rb
161
+ - spec/unit/job_spec.rb
162
+ - spec/unit/mailer_spec.rb
163
+ - spec/unit/rabbit_jobs_spec.rb
164
+ - spec/unit/worker_spec.rb
@@ -1,18 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
- module RabbitJobs
4
- class Util
5
- class << self
6
-
7
- def cleanup_backtrace(trace_lines)
8
- if defined?(Rails) && Rails.respond_to?(:root)
9
- rails_root_path = Rails.root.to_s
10
- trace_lines.dup.keep_if { |l| l[rails_root_path] }
11
- else
12
- trace_lines
13
- end
14
- end
15
-
16
- end
17
- end
18
- end