rabbit_jobs 0.1.14 → 0.1.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,15 +5,13 @@ module RabbitJobs
5
5
  defined?(ActionMailer) && !!RabbitJobs.config.mail_errors_from && !RabbitJobs.config.mail_errors_from.empty?
6
6
  end
7
7
 
8
- def self.send(job, error = $!)
8
+ def self.report_error(job, error = $!)
9
9
  return unless enabled?
10
10
 
11
- raise "You must require action_mailer or turn of error reporting in config." unless defined?(ActionMailer)
12
-
13
11
  params ||= []
14
12
 
15
13
  params_str = job.params == [] ? '' : job.params.map { |p| p.inspect }.join(', ')
16
- subject = "RJ:Worker: #{error.class} on #{job.class}.perform(#{params_str}) "
14
+ subject = "RJ:Worker: #{error.class} on #{job.class}"
17
15
  text = "\n#{job.class}.perform(#{params_str})\n"
18
16
  text += "\n#{error.inspect}\n"
19
17
  text += "\nBacktrace:\n#{error.backtrace.join("\n")}" if error.backtrace
@@ -27,8 +25,8 @@ module RabbitJobs
27
25
  begin
28
26
  letter.deliver
29
27
  rescue
30
- RJ.logger.error $!.inspect
28
+ RJ.logger.error [$!.message, $!.backtrace].flatten.join("\n")
31
29
  end
32
30
  end
33
31
  end
34
- end
32
+ end
@@ -34,7 +34,7 @@ module RabbitJobs::Job
34
34
  RJ.logger.warn(self.inspect)
35
35
  RJ.logger.warn([$!.inspect, $!.backtrace.to_a].join("\n"))
36
36
  run_on_error_hooks($!)
37
- RabbitJobs::ErrorMailer.send(self, $!)
37
+ RabbitJobs::ErrorMailer.report_error(self, $!)
38
38
  end
39
39
  exit!
40
40
  end
@@ -112,8 +112,6 @@ module RabbitJobs::Job
112
112
  RJ.logger.error $!.inspect
113
113
  RJ.logger.error $!.backtrace
114
114
  RJ.logger.error "message: #{payload.inspect}"
115
- # Mailer.send(klass_name, params, $!)
116
- # raise $!
117
115
  end
118
116
  end
119
117
  end
@@ -1,3 +1,3 @@
1
1
  module RabbitJobs
2
- VERSION = "0.1.14"
2
+ VERSION = "0.1.15"
3
3
  end
@@ -4,7 +4,7 @@ module RabbitJobs
4
4
  class Worker
5
5
  include AmqpHelpers
6
6
 
7
- attr_accessor :pidfile, :background, :process_name
7
+ attr_accessor :pidfile, :background, :process_name, :worker_pid
8
8
 
9
9
  # Workers should be initialized with an array of string queue
10
10
  # names. The order is important: a Worker will check the first
@@ -36,59 +36,72 @@ module RabbitJobs
36
36
  $0 = self.process_name || "rj_worker (#{queues.join(',')})"
37
37
 
38
38
  processed_count = 0
39
- amqp_with_exchange do |connection, exchange|
40
- exchange.channel.prefetch(1)
39
+ RJ.logger.info("Connecting to amqp...")
41
40
 
42
- check_shutdown = Proc.new {
43
- if @shutdown
44
- RJ.logger.info "Processed jobs: #{processed_count}"
45
- RJ.logger.info "Stopping worker ##{Process.pid}..."
41
+ begin
42
+ amqp_with_exchange do |connection, exchange|
43
+ exchange.channel.prefetch(1)
46
44
 
47
- connection.close {
48
- File.delete(self.pidfile) if self.pidfile && File.exists?(self.pidfile)
49
- RJ.logger.info "##{Process.pid} stopped."
50
- RJ.logger.close
45
+ check_shutdown = Proc.new {
46
+ if @shutdown
47
+ RJ.logger.info "Processed jobs: #{processed_count}"
48
+ RJ.logger.info "Stopping worker ##{Process.pid}..."
51
49
 
52
- EM.stop {
53
- exit!
50
+ connection.close {
51
+ File.delete(self.pidfile) if self.pidfile && File.exists?(self.pidfile)
52
+ RJ.logger.info "##{Process.pid} stopped."
53
+ RJ.logger.close
54
+
55
+ EM.stop {
56
+ exit!
57
+ }
54
58
  }
55
- }
56
- end
57
- }
59
+ end
60
+ }
61
+
62
+ queues.each do |routing_key|
63
+ queue = make_queue(exchange, routing_key)
58
64
 
59
- queues.each do |routing_key|
60
- queue = make_queue(exchange, routing_key)
65
+ RJ.logger.info "Worker ##{Process.pid} <= #{exchange.name}##{routing_key}"
61
66
 
62
- RJ.logger.info "Worker ##{Process.pid} <= #{exchange.name}##{routing_key}"
67
+ explicit_ack = !!RJ.config[:queues][routing_key][:ack]
63
68
 
64
- explicit_ack = !!RJ.config[:queues][routing_key][:ack]
69
+ queue.subscribe(ack: explicit_ack) do |metadata, payload|
70
+ @job = RJ::Job.parse(payload)
65
71
 
66
- queue.subscribe(ack: explicit_ack) do |metadata, payload|
67
- @job = RJ::Job.parse(payload)
72
+ unless @job.expired?
73
+ @job.run_perform
74
+ processed_count += 1
75
+ else
76
+ RJ.logger.info "Job expired: #{@job.inspect}"
77
+ end
68
78
 
69
- unless @job.expired?
70
- @job.run_perform
71
- processed_count += 1
72
- else
73
- RJ.logger.info "Job expired: #{@job.inspect}"
79
+ metadata.ack if explicit_ack
80
+
81
+ check_shutdown.call
74
82
  end
83
+ end
75
84
 
76
- metadata.ack if explicit_ack
85
+ if time > 0
86
+ # for debugging
87
+ EM.add_timer(time) do
88
+ self.shutdown
89
+ end
90
+ end
77
91
 
92
+ EM.add_periodic_timer(1) do
78
93
  check_shutdown.call
79
94
  end
80
95
  end
81
-
82
- if time > 0
83
- # for debugging
84
- EM.add_timer(time) do
85
- self.shutdown
96
+ rescue
97
+ error = $!
98
+ if RJ.logger
99
+ begin
100
+ RJ.logger.error [error.message, error.backtrace].flatten.join("\n")
101
+ ensure
102
+ abort(error.message)
86
103
  end
87
104
  end
88
-
89
- EM.add_periodic_timer(1) do
90
- check_shutdown.call
91
- end
92
105
  end
93
106
 
94
107
  true
@@ -103,21 +116,18 @@ module RabbitJobs
103
116
  RabbitJobs::Util.check_pidfile(self.pidfile) if self.pidfile
104
117
 
105
118
  if self.background
106
- child_pid = fork
107
- if child_pid
108
- return false
109
- else
110
- # daemonize child process
111
- Process.daemon(true)
112
- end
119
+ return false if self.worker_pid = fork
120
+
121
+ # daemonize child process
122
+ Process.daemon(true)
113
123
  end
114
124
 
125
+ self.worker_pid ||= Process.pid
126
+
115
127
  if self.pidfile
116
128
  File.open(self.pidfile, 'w') { |f| f << Process.pid }
117
129
  end
118
130
 
119
- # Fix buffering so we can `rake rj:work > resque.log` and
120
- # get output from the child in there.
121
131
  $stdout.sync = true
122
132
 
123
133
  @shutdown = false
@@ -135,13 +145,8 @@ module RabbitJobs
135
145
 
136
146
  def kill_child
137
147
  if @job && @job.child_pid
138
- # RJ.logger.info "Killing child at #{@child}"
139
- if Kernel.system("ps -o pid,state -p #{@job.child_pid}")
140
- Process.kill("KILL", @job.child_pid) rescue nil
141
- else
142
- # RJ.logger.info "Child #{@child} not found, restarting."
143
- # shutdown
144
- end
148
+ RJ.logger.info "Killing child #{@job.child_pid} at #{Time.now}"
149
+ Process.kill("KILL", @job.child_pid) rescue nil
145
150
  end
146
151
  end
147
152
  end
@@ -21,7 +21,7 @@ describe RabbitJobs::ErrorMailer do
21
21
  mock(email).deliver { }
22
22
  mock(RabbitJobs::ErrorMailer).enabled? { true }
23
23
 
24
- RabbitJobs::ErrorMailer.send(TestJob.new, RuntimeError.new('error text'))
24
+ RabbitJobs::ErrorMailer.report_error(TestJob.new, RuntimeError.new('error text'))
25
25
  end
26
26
  end
27
27
  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.1.14
4
+ version: 0.1.15
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-21 00:00:00.000000000 Z
12
+ date: 2012-08-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: amqp
@@ -129,7 +129,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
129
  version: '0'
130
130
  segments:
131
131
  - 0
132
- hash: -224029628042723631
132
+ hash: 4442654216517974901
133
133
  required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  none: false
135
135
  requirements:
@@ -138,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
138
  version: '0'
139
139
  segments:
140
140
  - 0
141
- hash: -224029628042723631
141
+ hash: 4442654216517974901
142
142
  requirements: []
143
143
  rubyforge_project:
144
144
  rubygems_version: 1.8.24