rabbit_jobs 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module RabbitJobs
3
+ module Consumer
4
+ class JobConsumer
5
+ def process_message(metadata, payload)
6
+ job = RJ::Job.parse(payload)
7
+
8
+ if job.is_a?(Symbol)
9
+ # case @job
10
+ # when :not_found
11
+ # when :parsing_error
12
+ # when :error
13
+ # end
14
+ else
15
+ if job.expired?
16
+ RJ.logger.warn "Job expired: #{job.to_ruby_string}"
17
+ false
18
+ else
19
+ job.run_perform
20
+ end
21
+ end
22
+
23
+ true
24
+ end
25
+ end
26
+ end
27
+ end
@@ -7,7 +7,7 @@ require 'yaml'
7
7
  module RabbitJobs
8
8
  class Scheduler
9
9
 
10
- attr_accessor :pidfile, :background, :schedule, :process_name
10
+ attr_accessor :schedule, :process_name
11
11
 
12
12
  def load_default_schedule
13
13
  if defined?(Rails)
@@ -106,7 +106,6 @@ module RabbitJobs
106
106
  RJ.logger.info "Processed jobs: #{processed_count}."
107
107
  RJ.logger.info "Stopped."
108
108
 
109
- File.delete(self.pidfile) if self.pidfile && File.exists?(self.pidfile)
110
109
  return true
111
110
  end
112
111
  end
@@ -130,27 +129,10 @@ module RabbitJobs
130
129
  end
131
130
 
132
131
  def startup
133
- # prune_dead_workers
134
- RabbitJobs::Util.check_pidfile(self.pidfile) if self.pidfile
135
-
136
- if self.background
137
- child_pid = fork
138
- if child_pid
139
- return false
140
- else
141
- # daemonize child process
142
- Process.daemon(true)
143
- end
144
- end
145
-
146
132
  # Fix buffering so we can `rake rj:work > resque.log` and
147
133
  # get output from the child in there.
148
134
  $stdout.sync = true
149
135
 
150
- if self.pidfile
151
- File.open(self.pidfile, 'w') { |f| f << Process.pid }
152
- end
153
-
154
136
  @shutdown = false
155
137
 
156
138
  Signal.trap('TERM') { shutdown }
@@ -24,9 +24,10 @@ namespace :rj do
24
24
 
25
25
  desc "Start a Rabbit Jobs worker"
26
26
  task :worker => :environment do
27
- queues = (ENV['QUEUES'] || ENV['QUEUES'] || "").split(',')
27
+ queues = (ENV['QUEUES'] || ENV['QUEUE'] || "").split(',')
28
28
  make_dirs
29
29
  worker = RJ::Worker.new(*queues)
30
+ worker.consumer = RJ::Consumer.const_get(ENV['CONSUMER'].classify).new if ENV['CONSUMER']
30
31
  worker.process_name = "rj_worker #{rails_env} [#{queues.join(',')}]"
31
32
  exit(worker.work)
32
33
  end
@@ -4,24 +4,6 @@ module RabbitJobs
4
4
  class Util
5
5
  class << self
6
6
 
7
- # clean old worker process
8
- def check_pidfile(pidfile)
9
- if File.exists?(pidfile)
10
- pid = File.read(pidfile).to_i
11
- begin
12
- Process.kill(0, pid)
13
- RJ.logger.info "Killing stale rj_worker[##{pid}]"
14
- Process.kill("TERM", pid)
15
-
16
- while Process.kill(0, pid)
17
- sleep(0.5)
18
- end
19
- rescue
20
- File.delete(pidfile) if File.exists?(pidfile)
21
- end
22
- end
23
- end
24
-
25
7
  def cleanup_backtrace(trace_lines)
26
8
  if defined?(Rails) && Rails.respond_to?(:root)
27
9
  rails_root_path = Rails.root.to_s
@@ -1,5 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
3
  module RabbitJobs
4
- VERSION = "0.7.1"
4
+ VERSION = "0.7.2"
5
5
  end
@@ -2,7 +2,13 @@
2
2
 
3
3
  module RabbitJobs
4
4
  class Worker
5
- attr_accessor :pidfile, :background, :process_name, :worker_pid
5
+ attr_accessor :process_name
6
+ attr_reader :consumer
7
+
8
+ def consumer=(consumer)
9
+ raise ArgumentError.new("consumer=#{consumer.inspect}") unless consumer.respond_to?(:process_message)
10
+ @consumer = consumer
11
+ end
6
12
 
7
13
  def amqp_connection
8
14
  Thread.current[:rj_worker_connection] ||= AmqpHelper.prepare_connection
@@ -14,27 +20,6 @@ module RabbitJobs
14
20
  Thread.current[:rj_worker_connection] = nil
15
21
  end
16
22
 
17
- def process_message(metadata, payload)
18
- job = RJ::Job.parse(payload)
19
-
20
- if job.is_a?(Symbol)
21
- # case @job
22
- # when :not_found
23
- # when :parsing_error
24
- # when :error
25
- # end
26
- else
27
- if job.expired?
28
- RJ.logger.warn "Job expired: #{job.to_ruby_string}"
29
- false
30
- else
31
- job.run_perform
32
- end
33
- end
34
-
35
- true
36
- end
37
-
38
23
  def queue_name(routing_key)
39
24
  RJ.config.queue_name(routing_key)
40
25
  end
@@ -69,6 +54,7 @@ module RabbitJobs
69
54
  # Subscribes to queue and working on jobs
70
55
  def work(time = -1)
71
56
  return false unless startup
57
+ @consumer ||= RJ::Consumer::JobConsumer.new
72
58
 
73
59
  $0 = self.process_name || "rj_worker (#{queues.join(', ')})"
74
60
 
@@ -91,7 +77,8 @@ module RabbitJobs
91
77
  queue.subscribe(ack: explicit_ack) do |delivery_info, properties, payload|
92
78
  if RJ.run_before_process_message_callbacks
93
79
  begin
94
- processed_count += 1 if process_message(properties, payload)
80
+ @consumer.process_message(properties, payload)
81
+ processed_count += 1
95
82
  rescue
96
83
  RJ.logger.warn "process_message failed. payload: #{payload.inspect}"
97
84
  RJ.logger.warn $!.inspect
@@ -123,8 +110,6 @@ module RabbitJobs
123
110
  if @shutdown
124
111
  RJ.logger.info "Processed jobs: #{processed_count}."
125
112
  RJ.logger.info "Stopped."
126
-
127
- File.delete(self.pidfile) if self.pidfile && File.exists?(self.pidfile)
128
113
  return true
129
114
  end
130
115
  end
@@ -147,24 +132,8 @@ module RabbitJobs
147
132
  end
148
133
 
149
134
  def startup
150
- # prune_dead_workers
151
- RabbitJobs::Util.check_pidfile(self.pidfile) if self.pidfile
152
-
153
- if self.background
154
- return false if self.worker_pid = fork
155
-
156
- # daemonize child process
157
- Process.daemon(true)
158
- end
159
-
160
135
  count = RJ._run_after_fork_callbacks
161
136
 
162
- self.worker_pid ||= Process.pid
163
-
164
- if self.pidfile
165
- File.open(self.pidfile, 'w') { |f| f << Process.pid }
166
- end
167
-
168
137
  $stdout.sync = true
169
138
 
170
139
  @shutdown = false
data/lib/rabbit_jobs.rb CHANGED
@@ -10,6 +10,7 @@ require 'rabbit_jobs/amqp_helper'
10
10
  require 'rabbit_jobs/configuration'
11
11
  require 'rabbit_jobs/error_mailer'
12
12
 
13
+ require 'rabbit_jobs/consumer/job_consumer'
13
14
  require 'rabbit_jobs/job'
14
15
  require 'rabbit_jobs/publisher'
15
16
  require 'rabbit_jobs/worker'
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 "bunny", "0.9.0.pre7"
19
+ gem.add_dependency "bunny", "0.9.0.pre8"
20
20
  gem.add_dependency "rake"
21
21
  gem.add_dependency "rufus-scheduler", "~> 2.0"
22
22
  gem.add_dependency "rails", ">= 3.0"
@@ -6,9 +6,6 @@ describe RabbitJobs::Scheduler do
6
6
  scheduler = RabbitJobs::Scheduler.new
7
7
  scheduler.schedule = YAML.load_file(File.expand_path('../../fixtures/schedule.yml', __FILE__))
8
8
 
9
- scheduler.pidfile = '/tmp/rj_scheduler.pid'
10
- scheduler.background = false
11
-
12
9
  scheduler.work(10) # work for 1 second
13
10
 
14
11
  RJ.config.queue 'default', RJ::Configuration::DEFAULT_QUEUE_PARAMS
@@ -22,17 +22,6 @@ describe RabbitJobs::Worker do
22
22
  @worker.instance_variable_get('@shutdown').should_not == true
23
23
  end
24
24
 
25
- it '#startup should write process id to file' do
26
- mock(Signal).trap('TERM')
27
- mock(Signal).trap('INT')
28
-
29
- filename = 'test_worker.pid'
30
- mock(File).open(filename, 'w') {}
31
- @worker.pidfile = filename
32
- @worker.startup
33
- @worker.pidfile.should == filename
34
- end
35
-
36
25
  it '#shutdown should set @shutdown to true' do
37
26
  @worker.instance_variable_get('@shutdown').should_not == true
38
27
  @worker.shutdown
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.1
4
+ version: 0.7.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-20 00:00:00.000000000 Z
12
+ date: 2013-03-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bunny
16
- requirement: &21380680 !ruby/object:Gem::Requirement
16
+ requirement: &10601260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - =
20
20
  - !ruby/object:Gem::Version
21
- version: 0.9.0.pre7
21
+ version: 0.9.0.pre8
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *21380680
24
+ version_requirements: *10601260
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &21379160 !ruby/object:Gem::Requirement
27
+ requirement: &10622000 !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: *21379160
35
+ version_requirements: *10622000
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rufus-scheduler
38
- requirement: &21374960 !ruby/object:Gem::Requirement
38
+ requirement: &10619420 !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: *21374960
46
+ version_requirements: *10619420
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rails
49
- requirement: &21399100 !ruby/object:Gem::Requirement
49
+ requirement: &10617700 !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: *21399100
57
+ version_requirements: *10617700
58
58
  description: Background jobs on RabbitMQ
59
59
  email:
60
60
  - lazureykis@gmail.com
@@ -74,6 +74,7 @@ files:
74
74
  - lib/rabbit_jobs.rb
75
75
  - lib/rabbit_jobs/amqp_helper.rb
76
76
  - lib/rabbit_jobs/configuration.rb
77
+ - lib/rabbit_jobs/consumer/job_consumer.rb
77
78
  - lib/rabbit_jobs/error_mailer.rb
78
79
  - lib/rabbit_jobs/helpers.rb
79
80
  - lib/rabbit_jobs/job.rb
@@ -111,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
111
112
  version: '0'
112
113
  segments:
113
114
  - 0
114
- hash: 2351435873662003643
115
+ hash: 2228103642299556651
115
116
  required_rubygems_version: !ruby/object:Gem::Requirement
116
117
  none: false
117
118
  requirements:
@@ -120,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
121
  version: '0'
121
122
  segments:
122
123
  - 0
123
- hash: 2351435873662003643
124
+ hash: 2228103642299556651
124
125
  requirements: []
125
126
  rubyforge_project:
126
127
  rubygems_version: 1.8.11