rabbit_jobs 0.3 → 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rabbit_jobs/configuration.rb +25 -1
- data/lib/rabbit_jobs/job.rb +4 -1
- data/lib/rabbit_jobs/scheduler.rb +11 -5
- data/lib/rabbit_jobs/tasks.rb +20 -27
- data/lib/rabbit_jobs/version.rb +1 -1
- data/lib/rabbit_jobs/worker.rb +3 -3
- data/lib/rabbit_jobs.rb +3 -2
- metadata +12 -12
@@ -58,9 +58,10 @@ module RabbitJobs
|
|
58
58
|
def initialize
|
59
59
|
@data = {
|
60
60
|
error_log: true,
|
61
|
+
workers: HashWithIndifferentAccess.new,
|
61
62
|
servers: [],
|
62
63
|
prefix: 'rabbit_jobs',
|
63
|
-
queues:
|
64
|
+
queues: HashWithIndifferentAccess.new
|
64
65
|
}
|
65
66
|
end
|
66
67
|
|
@@ -130,6 +131,25 @@ module RabbitJobs
|
|
130
131
|
end
|
131
132
|
end
|
132
133
|
|
134
|
+
def workers
|
135
|
+
@data[:workers] ||= HashWithIndifferentAccess.new
|
136
|
+
end
|
137
|
+
|
138
|
+
def worker(name, params = {})
|
139
|
+
raise ArgumentError.new("name is #{name.inspect}") unless name && name.is_a?(String) && name != ""
|
140
|
+
raise ArgumentError.new("params is #{params.inspect}") unless params && params.is_a?(Hash)
|
141
|
+
raise ArgumentError.new("params should have :instances and :queues keys.") unless params[:instances] && params[:queues]
|
142
|
+
|
143
|
+
name = name.downcase
|
144
|
+
|
145
|
+
@data[:workers] ||= HashWithIndifferentAccess.new
|
146
|
+
if @data[:workers][name]
|
147
|
+
@data[:workers][name].merge!(params)
|
148
|
+
else
|
149
|
+
@data[:workers][name] = params
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
133
153
|
def routing_keys
|
134
154
|
@data[:queues].keys
|
135
155
|
end
|
@@ -171,6 +191,10 @@ module RabbitJobs
|
|
171
191
|
yaml['queues'].each do |name, params|
|
172
192
|
queue name, symbolize_keys!(params) || {}
|
173
193
|
end
|
194
|
+
|
195
|
+
yaml['workers'].each do |name, params|
|
196
|
+
worker name, symbolize_keys!(params) || {}
|
197
|
+
end
|
174
198
|
end
|
175
199
|
end
|
176
200
|
|
data/lib/rabbit_jobs/job.rb
CHANGED
@@ -18,8 +18,11 @@ module RabbitJobs::Job
|
|
18
18
|
|
19
19
|
def run_perform
|
20
20
|
begin
|
21
|
-
|
21
|
+
start_time = Time.now
|
22
|
+
RJ.logger.info "rj_worker[##{Process.pid}] performing #{self.to_ruby_string} at #{start_time.utc}"
|
22
23
|
self.class.perform(*params)
|
24
|
+
execution_time = Time.now - start_time
|
25
|
+
RJ.logger.info "rj_worker[##{Process.pid}] processed #{self.to_ruby_string} in #{execution_time} seconds."
|
23
26
|
rescue
|
24
27
|
RJ.logger.warn $!.message
|
25
28
|
RJ.logger.warn(self.to_ruby_string)
|
@@ -97,10 +97,10 @@ module RabbitJobs
|
|
97
97
|
|
98
98
|
check_shutdown = Proc.new {
|
99
99
|
if @shutdown
|
100
|
-
RJ.stop
|
101
|
-
File.delete(self.pidfile) if self.pidfile
|
102
|
-
}
|
100
|
+
RJ.stop
|
103
101
|
RJ.logger.info "rj_scheduler[##{Process.pid}] stopped."
|
102
|
+
|
103
|
+
File.delete(self.pidfile) if self.pidfile
|
104
104
|
end
|
105
105
|
}
|
106
106
|
|
@@ -117,8 +117,14 @@ module RabbitJobs
|
|
117
117
|
RJ.logger.info "rj_scheduler[##{Process.pid}] started."
|
118
118
|
end
|
119
119
|
rescue => e
|
120
|
-
|
121
|
-
RJ.logger
|
120
|
+
error = $!
|
121
|
+
if RJ.logger
|
122
|
+
begin
|
123
|
+
RJ.logger.error [error.message, error.backtrace].flatten.join("\n")
|
124
|
+
ensure
|
125
|
+
abort(error.message)
|
126
|
+
end
|
127
|
+
end
|
122
128
|
end
|
123
129
|
|
124
130
|
true
|
data/lib/rabbit_jobs/tasks.rb
CHANGED
@@ -31,7 +31,7 @@ namespace :rj do
|
|
31
31
|
desc "Start a Rabbit Jobs workers from config/rj_workers.yml"
|
32
32
|
task :start => [:environment, :load_config] do
|
33
33
|
make_dirs
|
34
|
-
|
34
|
+
RJ.config.workers.each do |worker_name, worker_props|
|
35
35
|
worker_num = 1
|
36
36
|
worker_props['instances'].to_i.times do
|
37
37
|
unless @do_only.count > 0 && !@do_only.include?("#{worker_name}-#{worker_num}")
|
@@ -58,7 +58,7 @@ namespace :rj do
|
|
58
58
|
pids = {}
|
59
59
|
errors = []
|
60
60
|
|
61
|
-
|
61
|
+
RJ.config.workers.each do |worker_name, worker_props|
|
62
62
|
worker_num = 1
|
63
63
|
worker_props['instances'].to_i.times do
|
64
64
|
unless (@do_only.count > 0) && !@do_only.include?("#{worker_name}-#{worker_num}")
|
@@ -107,50 +107,46 @@ namespace :rj do
|
|
107
107
|
sleep 1
|
108
108
|
end
|
109
109
|
|
110
|
-
|
110
|
+
fail(errors.join("\r\n")) if not errors.empty?
|
111
111
|
|
112
112
|
puts "\nrj_worker stopped."
|
113
|
-
exit(0)
|
114
113
|
end
|
115
114
|
|
116
115
|
task :status => :load_config do
|
116
|
+
errors = []
|
117
117
|
|
118
|
-
|
119
|
-
|
120
|
-
@rj_config.each do |worker_name, worker_props|
|
118
|
+
RJ.config.workers.each do |worker_name, worker_props|
|
121
119
|
worker_num = 1
|
122
120
|
worker_props['instances'].to_i.times do
|
123
121
|
pidfile = app_root.join("tmp/pids/rj_worker_#{rails_env}_#{worker_name}_#{worker_num}.pid")
|
124
122
|
|
125
123
|
unless File.exists?(pidfile)
|
126
|
-
|
127
|
-
errors += 1
|
124
|
+
errors << "Pidfile not found: #{pidfile}"
|
128
125
|
else
|
129
126
|
pid = open(pidfile).read.to_i
|
130
127
|
begin
|
131
128
|
raise "must return 1" unless Process.kill(0, pid) == 1
|
132
129
|
rescue
|
133
|
-
|
130
|
+
errors << "Pidfile found but process not respond: #{pidfile}\r\nRemoving pidfile."
|
134
131
|
File.delete(pidfile) if File.exist?(pidfile)
|
135
|
-
errors += 1
|
136
132
|
end
|
137
133
|
end
|
138
134
|
worker_num += 1
|
139
135
|
end
|
140
136
|
end
|
141
137
|
|
142
|
-
|
143
|
-
|
138
|
+
if errors.empty?
|
139
|
+
puts "ok"
|
140
|
+
else
|
141
|
+
fail errors.join("\r\n")
|
142
|
+
end
|
144
143
|
end
|
145
144
|
|
146
145
|
task :load_config do
|
147
|
-
@rj_config = YAML.load(open app_root.join("config/rj_workers.yml"))
|
148
|
-
@rj_config = @rj_config[rails_env]
|
149
|
-
|
150
146
|
@do_only = ENV["WORKERS"] ? ENV["WORKERS"].strip.split : []
|
151
147
|
@do_only.each do |worker|
|
152
148
|
worker_name, worker_num = worker.split('-')
|
153
|
-
unless
|
149
|
+
unless RJ.config.workers.keys.include?(worker_name) && RJ.config.workers[worker_name][:instances].to_i >= worker_num.to_i
|
154
150
|
raise "Worker #{worker} not found."
|
155
151
|
end
|
156
152
|
end
|
@@ -168,7 +164,7 @@ namespace :rj do
|
|
168
164
|
RJ.logger = ::Logger.new(app_root.join('log/rj_scheduler.log'), 'daily')
|
169
165
|
# RJ.logger.level = ENV['VERBOSE'] ? Logger::INFO : Logger::WARN
|
170
166
|
|
171
|
-
scheduler.work
|
167
|
+
exit! if scheduler.work
|
172
168
|
puts "rj_scheduler started."
|
173
169
|
end
|
174
170
|
|
@@ -176,9 +172,7 @@ namespace :rj do
|
|
176
172
|
pidfile = app_root.join('tmp/pids/rj_scheduler.pid')
|
177
173
|
|
178
174
|
unless File.exists?(pidfile)
|
179
|
-
|
180
|
-
$stderr.puts msg
|
181
|
-
exit(1)
|
175
|
+
fail "Pidfile not found: #{pidfile}"
|
182
176
|
else
|
183
177
|
pid = open(pidfile).read.to_i
|
184
178
|
begin
|
@@ -187,7 +181,7 @@ namespace :rj do
|
|
187
181
|
$stderr.puts "Not found process: #{pid} from #{pidfile}"
|
188
182
|
$stderr.puts "Removing pidfile ..."
|
189
183
|
File.delete(pidfile) if File.exist?(pidfile)
|
190
|
-
|
184
|
+
fail "Not found process: #{pid} from #{pidfile}"
|
191
185
|
end
|
192
186
|
|
193
187
|
while true
|
@@ -196,7 +190,7 @@ namespace :rj do
|
|
196
190
|
sleep 0.1
|
197
191
|
rescue
|
198
192
|
puts "rj_scheduler[##{pid}] stopped."
|
199
|
-
|
193
|
+
break
|
200
194
|
end
|
201
195
|
end
|
202
196
|
end
|
@@ -206,8 +200,8 @@ namespace :rj do
|
|
206
200
|
pidfile = app_root.join('tmp/pids/rj_scheduler.pid')
|
207
201
|
|
208
202
|
unless File.exists?(pidfile)
|
209
|
-
puts "Pidfile not found: #{pidfile}"
|
210
|
-
|
203
|
+
# puts "Pidfile not found: #{pidfile}"
|
204
|
+
fail "Pidfile not found: #{pidfile}"
|
211
205
|
else
|
212
206
|
pid = open(pidfile).read.to_i
|
213
207
|
begin
|
@@ -217,10 +211,9 @@ namespace :rj do
|
|
217
211
|
puts "Pidfile found but process not respond: #{pidfile}"
|
218
212
|
puts "Removing pidfile."
|
219
213
|
File.delete(pidfile) if File.exist?(pidfile)
|
220
|
-
|
214
|
+
fail "Pidfile found but process not respond: #{pidfile}"
|
221
215
|
end
|
222
216
|
end
|
223
|
-
exit(0)
|
224
217
|
end
|
225
218
|
end
|
226
219
|
|
data/lib/rabbit_jobs/version.rb
CHANGED
data/lib/rabbit_jobs/worker.rb
CHANGED
@@ -39,11 +39,11 @@ module RabbitJobs
|
|
39
39
|
RJ.run do
|
40
40
|
check_shutdown = Proc.new {
|
41
41
|
if @shutdown
|
42
|
-
RJ.stop
|
43
|
-
File.delete(self.pidfile) if self.pidfile && File.exists?(self.pidfile)
|
44
|
-
}
|
42
|
+
RJ.stop
|
45
43
|
RJ.logger.info "rj_worker[##{Process.pid}] processed jobs: #{processed_count}."
|
46
44
|
RJ.logger.info "rj_worker[##{Process.pid}] stopped."
|
45
|
+
|
46
|
+
File.delete(self.pidfile) if self.pidfile && File.exists?(self.pidfile)
|
47
47
|
# RJ.logger.close
|
48
48
|
# exit!
|
49
49
|
end
|
data/lib/rabbit_jobs.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
|
+
require 'logger'
|
3
|
+
require 'rake'
|
2
4
|
|
3
5
|
require 'rabbit_jobs/version'
|
4
6
|
|
@@ -12,8 +14,7 @@ require 'rabbit_jobs/job'
|
|
12
14
|
require 'rabbit_jobs/publisher'
|
13
15
|
require 'rabbit_jobs/worker'
|
14
16
|
require 'rabbit_jobs/scheduler'
|
15
|
-
|
16
|
-
require 'logger'
|
17
|
+
require 'rabbit_jobs/tasks'
|
17
18
|
|
18
19
|
module RabbitJobs
|
19
20
|
extend self
|
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:
|
4
|
+
version: 0.4.4
|
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-
|
12
|
+
date: 2013-02-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: amqp
|
16
|
-
requirement: &
|
16
|
+
requirement: &6815860 !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: *
|
24
|
+
version_requirements: *6815860
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &6814800 !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: *
|
35
|
+
version_requirements: *6814800
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rufus-scheduler
|
38
|
-
requirement: &
|
38
|
+
requirement: &6814000 !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: *
|
46
|
+
version_requirements: *6814000
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rails
|
49
|
-
requirement: &
|
49
|
+
requirement: &6812800 !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: *
|
57
|
+
version_requirements: *6812800
|
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:
|
118
|
+
hash: 4510837792608330592
|
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:
|
127
|
+
hash: 4510837792608330592
|
128
128
|
requirements: []
|
129
129
|
rubyforge_project:
|
130
130
|
rubygems_version: 1.8.11
|