rabbit_jobs 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ gemspec
5
5
  group :development do
6
6
  gem 'actionmailer', '>= 3.0'
7
7
 
8
+ gem 'timecop'
8
9
  gem 'rspec', '~> 2.8'
9
10
  gem 'rr'
10
11
  gem 'autotest'
@@ -7,18 +7,27 @@ module RabbitJobs
7
7
  def amqp_with_exchange(&block)
8
8
  raise ArgumentError unless block
9
9
 
10
- AMQP.start(host: RJ.config.host) do |connection|
11
- channel = AMQP::Channel.new(connection)
12
-
13
- channel.on_error do |ch, channel_close|
14
- puts "Channel-level error: #{channel_close.reply_text}, shutting down..."
15
- connection.close { EM.stop }
16
- end
17
-
10
+ connection = AMQP.connection
11
+ if connection && connection.open?
12
+ channel = AMQP::Channel.new(connection)
18
13
  exchange = channel.direct(RJ.config[:exchange], RJ.config[:exchange_params])
19
-
20
14
  # go work
21
15
  block.call(connection, exchange)
16
+
17
+ else
18
+ AMQP.start(host: RJ.config.host) do |connection|
19
+
20
+ channel = AMQP::Channel.new(connection)
21
+
22
+ channel.on_error do |ch, channel_close|
23
+ puts "Channel-level error: #{channel_close.reply_text}, shutting down..."
24
+ connection.close { EM.stop }
25
+ end
26
+
27
+ exchange = channel.direct(RJ.config[:exchange], RJ.config[:exchange_params])
28
+ # go work
29
+ block.call(connection, exchange)
30
+ end
22
31
  end
23
32
  end
24
33
 
@@ -10,7 +10,7 @@ module RabbitJobs::Job
10
10
  base.extend (ClassMethods)
11
11
 
12
12
  def initialize(*perform_params)
13
- self.params = *perform_params
13
+ self.params = perform_params
14
14
  self.opts = {}
15
15
  end
16
16
 
@@ -35,10 +35,10 @@ module RabbitJobs::Job
35
35
  self.class.perform(*params)
36
36
  # RJ.logger.debug 'after perform'
37
37
  rescue
38
- RJ.logger.warn($!.inspect)
39
- RabbitJobs::ErrorMailer.send(self, $!)
40
-
38
+ RJ.logger.warn(self.inspect)
39
+ RJ.logger.warn([$!.inspect, $!.backtrace.to_a].join("\n"))
41
40
  run_on_error_hooks($!)
41
+ RabbitJobs::ErrorMailer.send(self, $!)
42
42
  end
43
43
  exit!
44
44
  end
@@ -47,17 +47,18 @@ module RabbitJobs::Job
47
47
  def run_on_error_hooks(error)
48
48
  if self.class.rj_on_error_hooks
49
49
  self.class.rj_on_error_hooks.each do |proc_or_symbol|
50
+ proc = proc_or_symbol
50
51
  if proc_or_symbol.is_a?(Symbol)
51
- self.send(proc_or_symbol, error, *params)
52
+ proc = self.method(proc_or_symbol)
53
+ end
54
+
55
+ case proc.arity
56
+ when 0
57
+ proc.call()
58
+ when 1
59
+ proc.call(error)
52
60
  else
53
- case proc_or_symbol.arity
54
- when 0
55
- proc_or_symbol.call()
56
- when 1
57
- proc_or_symbol.call(error)
58
- else
59
- proc_or_symbol.call(error, *params)
60
- end
61
+ proc.call(error, *params)
61
62
  end
62
63
  end
63
64
  end
@@ -65,7 +66,6 @@ module RabbitJobs::Job
65
66
 
66
67
  def payload
67
68
  {'class' => self.class.to_s, 'opts' => (self.opts || {}), 'params' => params}.to_json
68
- # ([self.class.to_s] + params).to_json
69
69
  end
70
70
 
71
71
  def expires_in
@@ -108,7 +108,7 @@ module RabbitJobs::Job
108
108
  begin
109
109
  encoded = JSON.parse(payload)
110
110
  job_klass = constantize(encoded['class'])
111
- job = job_klass.new(*encoded['params'])
111
+ job = encoded['params'].count > 0 ? job_klass.new(*encoded['params']) : job_klass.new
112
112
  job.opts = encoded['opts']
113
113
  job
114
114
  rescue
@@ -3,23 +3,56 @@
3
3
  require 'json'
4
4
  require 'amqp'
5
5
  require 'eventmachine'
6
+ require 'bunny'
6
7
 
7
8
  module RabbitJobs
8
9
  module Publisher
9
10
  extend self
10
11
  extend AmqpHelpers
11
12
 
12
- def publish(klass, opts = {}, *params)
13
+ def publish(klass, *params)
13
14
  key = RJ.config.routing_keys.first
14
- publish_to(key, klass, opts, *params)
15
+ publish_to(key, klass, *params)
15
16
  end
16
17
 
17
- def publish_to(routing_key, klass, opts = {}, *params)
18
- raise ArgumentError unless klass && routing_key && klass.is_a?(Class)# && (routing_key.is_a?(Hash) || routing_key.is_a?(String))
19
- opts ||= {}
18
+ def publish_to(routing_key, klass, *params)
19
+ raise ArgumentError unless klass && routing_key && klass.is_a?(Class) && (routing_key.is_a?(Symbol) || routing_key.is_a?(String))
20
20
 
21
21
  job = klass.new(*params)
22
- job.opts = opts
22
+ job.opts = {'created_at' => Time.now.to_i}
23
+
24
+ b = Bunny.new(host: RJ.config.host, logging: false)
25
+ b.start
26
+
27
+ exchange = b.exchange(RJ.config[:exchange], RJ.config[:exchange_params])
28
+ queue = b.queue(RJ.config.queue_name(routing_key), RJ.config[:queues][routing_key.to_s])
29
+ queue.bind(exchange)
30
+
31
+ exchange.publish(job.payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({key: routing_key.to_s}))
32
+ b.stop
33
+ end
34
+
35
+ def purge_queue(*routing_keys)
36
+ raise ArgumentError unless routing_keys && routing_keys.count > 0
37
+
38
+ b = Bunny.new(host: RJ.config.host, logging: false)
39
+ b.start
40
+
41
+ messages_count = 0
42
+ routing_keys.each do |routing_key|
43
+ queue = b.queue(RJ.config.queue_name(routing_key), RJ.config[:queues][routing_key.to_s])
44
+ messages_count += queue.status[:message_count]
45
+ queue.purge
46
+ end
47
+ b.stop
48
+ return messages_count
49
+ end
50
+
51
+ def publish_to2(routing_key, klass, *params)
52
+ raise ArgumentError unless klass && routing_key && klass.is_a?(Class) && (routing_key.is_a?(Hash) || routing_key.is_a?(String))
53
+
54
+ job = klass.new(*params)
55
+ job.opts = {}
23
56
 
24
57
  dont_stop_em = defined?(EM) && EM.reactor_running?
25
58
  dont_close_connection = AMQP.connection && AMQP.connection.open?
@@ -41,7 +74,7 @@ module RabbitJobs
41
74
  end
42
75
  end
43
76
 
44
- def purge_queue(*routing_keys)
77
+ def purge_queue2(*routing_keys)
45
78
  raise ArgumentError unless routing_keys && routing_keys.count > 0
46
79
 
47
80
  dont_stop_em = defined?(EM) && EM.reactor_running?
@@ -55,7 +88,6 @@ module RabbitJobs
55
88
  routing_key = routing_key.to_s
56
89
  queue = exchange.channel.queue(RabbitJobs.config.queue_name(routing_key), RabbitJobs.config[:queues][routing_key])
57
90
  queue.bind(exchange, :routing_key => routing_key)
58
-
59
91
  queue.status do |number_of_messages, number_of_consumers|
60
92
  messages_count += number_of_messages
61
93
  queue.purge {
@@ -1,3 +1,3 @@
1
1
  module RabbitJobs
2
- VERSION = "0.0.9"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/rabbit_jobs.rb CHANGED
@@ -17,12 +17,12 @@ require 'logger'
17
17
  module RabbitJobs
18
18
  extend self
19
19
 
20
- def publish(klass, opts = {}, *params)
21
- RabbitJobs::Publisher.publish(klass, opts, *params)
20
+ def publish(klass, *params)
21
+ RabbitJobs::Publisher.publish(klass, *params)
22
22
  end
23
23
 
24
- def publish_to(routing_key, klass, opts = {}, *params)
25
- RabbitJobs::Publisher.publish_to(routing_key, klass, opts, *params)
24
+ def publish_to(routing_key, klass, *params)
25
+ RabbitJobs::Publisher.publish_to(routing_key, klass, *params)
26
26
  end
27
27
 
28
28
  attr_writer :logger
data/rabbit_jobs.gemspec CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.version = RabbitJobs::VERSION
19
19
 
20
20
  gem.add_dependency "amqp", "~> 0.9"
21
+ gem.add_dependency "bunny", "~> 0.7"
21
22
  gem.add_dependency "rake"
22
23
  gem.add_dependency "rufus-scheduler", "~> 2.0"
23
24
  end
@@ -1,13 +1,13 @@
1
1
  do_a_job:
2
2
  cron: "* * * * *" # run every minute
3
3
  queue: queue_name
4
- class: MyJobClass
4
+ class: TestJob
5
5
  args: "string to be passed to MyJobClass.perform"
6
6
  description: "Description is not supported yet"
7
7
 
8
8
  another_job:
9
9
  every: 1h # run every 1 hour
10
10
  queue: queue_name
11
- class: MyJobClass
11
+ class: TestJob
12
12
  args: "string to be passed to MyJobClass.perform"
13
13
  description: "Description is not supported yet"
@@ -16,7 +16,7 @@ describe RabbitJobs::Publisher do
16
16
  end
17
17
 
18
18
  it 'should publish message to queue' do
19
- RabbitJobs.publish(TestJob, nil, 'some', 'other', 'params')
19
+ RabbitJobs.publish(TestJob, 'some', 'other', 'params')
20
20
  RabbitJobs::Publisher.purge_queue('rspec_queue').should == 1
21
21
  end
22
22
 
@@ -33,7 +33,7 @@ describe RabbitJobs::Publisher do
33
33
  end
34
34
 
35
35
  it 'should publish job with *params' do
36
- RabbitJobs.publish_to(:rspec_queue, JobWithArgsArray, nil, 'first value', :some_symbol, 123, 'and string')
36
+ RabbitJobs.publish_to(:rspec_queue, JobWithArgsArray, 'first value', :some_symbol, 123, 'and string')
37
37
  RabbitJobs::Publisher.purge_queue(:rspec_queue).should == 1
38
38
  end
39
39
  end
@@ -1,7 +1,8 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  require 'spec_helper'
3
-
3
+ require 'timecop'
4
4
  require 'eventmachine'
5
+
5
6
  describe RabbitJobs::Worker do
6
7
  it 'should listen for messages' do
7
8
  RabbitJobs.configure do |c|
@@ -9,12 +10,15 @@ describe RabbitJobs::Worker do
9
10
  c.queue 'rspec_durable_queue', auto_delete: false, durable: true, ack: true
10
11
  end
11
12
 
12
- 5.times { RabbitJobs.publish(PrintTimeJob, nil, Time.now) }
13
- 5.times { RabbitJobs.publish(ExpiredJob, { :expires_at => Time.now - 10 }) }
13
+ RJ::Publisher.purge_queue('rspec_durable_queue')
14
+
15
+ 5.times { RabbitJobs.publish(PrintTimeJob, Time.now) }
16
+ Timecop.freeze(Time.now - 4600) { 5.times { RabbitJobs.publish(JobWithExpire) } }
14
17
  1.times { RabbitJobs.publish(JobWithErrorHook) }
15
18
  worker = RabbitJobs::Worker.new
16
19
 
17
20
  worker.work(1) # work for 1 second
21
+ RJ::Publisher.purge_queue('rspec_durable_queue')
18
22
  end
19
23
 
20
24
  it 'should allow to publish jobs from worker' do
@@ -23,6 +27,8 @@ describe RabbitJobs::Worker do
23
27
  c.queue 'rspec_durable_queue', auto_delete: false, durable: true, ack: true
24
28
  end
25
29
 
30
+ RJ::Publisher.purge_queue('rspec_durable_queue')
31
+
26
32
  RabbitJobs.publish(JobWithPublish)
27
33
  worker = RabbitJobs::Worker.new
28
34
 
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.0.9
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: amqp
16
- requirement: &70108928613220 !ruby/object:Gem::Requirement
16
+ requirement: &70362685237960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,21 @@ dependencies:
21
21
  version: '0.9'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70108928613220
24
+ version_requirements: *70362685237960
25
+ - !ruby/object:Gem::Dependency
26
+ name: bunny
27
+ requirement: &70362685237460 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '0.7'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70362685237460
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: rake
27
- requirement: &70108932737200 !ruby/object:Gem::Requirement
38
+ requirement: &70362685237080 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: '0'
33
44
  type: :runtime
34
45
  prerelease: false
35
- version_requirements: *70108932737200
46
+ version_requirements: *70362685237080
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: rufus-scheduler
38
- requirement: &70108932736660 !ruby/object:Gem::Requirement
49
+ requirement: &70362685236540 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ~>
@@ -43,7 +54,7 @@ dependencies:
43
54
  version: '2.0'
44
55
  type: :runtime
45
56
  prerelease: false
46
- version_requirements: *70108932736660
57
+ version_requirements: *70362685236540
47
58
  description: Background jobs on RabbitMQ
48
59
  email:
49
60
  - lazureykis@gmail.com