rabbit_jobs 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rabbit_jobs/error_mailer.rb +1 -3
- data/lib/rabbit_jobs/job.rb +8 -10
- data/lib/rabbit_jobs/scheduler.rb +9 -10
- data/lib/rabbit_jobs/tasks.rb +12 -18
- data/lib/rabbit_jobs/version.rb +1 -1
- data/lib/rabbit_jobs/worker.rb +6 -7
- data/lib/rabbit_jobs.rb +7 -1
- metadata +7 -10
- data/lib/rabbit_jobs/logger.rb +0 -18
- data/spec/unit/logger_spec.rb +0 -23
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
module RabbitJobs
|
4
4
|
class ErrorMailer
|
5
|
-
include RabbitJobs::Logger
|
6
|
-
|
7
5
|
def self.enabled?
|
8
6
|
!!RabbitJobs.config.mail_errors_from && !RabbitJobs.config.mail_errors_from.empty?
|
9
7
|
end
|
@@ -30,7 +28,7 @@ module RabbitJobs
|
|
30
28
|
begin
|
31
29
|
letter.deliver
|
32
30
|
rescue
|
33
|
-
|
31
|
+
RJ.logger.error $!.inspect
|
34
32
|
end
|
35
33
|
end
|
36
34
|
end
|
data/lib/rabbit_jobs/job.rb
CHANGED
@@ -4,11 +4,9 @@ require 'digest/md5'
|
|
4
4
|
|
5
5
|
module RabbitJobs::Job
|
6
6
|
extend RabbitJobs::Helpers
|
7
|
-
extend RabbitJobs::Logger
|
8
7
|
extend self
|
9
8
|
|
10
9
|
def self.included(base)
|
11
|
-
include RabbitJobs::Logger
|
12
10
|
base.extend (ClassMethods)
|
13
11
|
|
14
12
|
def initialize(*perform_params)
|
@@ -21,7 +19,7 @@ module RabbitJobs::Job
|
|
21
19
|
def run_perform
|
22
20
|
if @child_pid = fork
|
23
21
|
srand # Reseeding
|
24
|
-
|
22
|
+
RJ.logger.info "Forked #{@child_pid} at #{Time.now} to process #{self.class}.perform(#{ params.map(&:inspect).join(', ') })"
|
25
23
|
Process.wait(@child_pid)
|
26
24
|
yield if block_given?
|
27
25
|
else
|
@@ -32,11 +30,11 @@ module RabbitJobs::Job
|
|
32
30
|
if defined?(MongoMapper)
|
33
31
|
MongoMapper.database.connection.connect_to_master
|
34
32
|
end
|
35
|
-
#
|
33
|
+
# RJ.logger.debug 'before perform'
|
36
34
|
self.class.perform(*params)
|
37
|
-
#
|
35
|
+
# RJ.logger.debug 'after perform'
|
38
36
|
rescue
|
39
|
-
|
37
|
+
RJ.logger.warn($!.inspect)
|
40
38
|
RabbitJobs::ErrorMailer.send(self, $!)
|
41
39
|
|
42
40
|
run_on_error_hooks($!)
|
@@ -113,10 +111,10 @@ module RabbitJobs::Job
|
|
113
111
|
job.opts = encoded['opts']
|
114
112
|
job
|
115
113
|
rescue
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
114
|
+
RJ.logger.error "JOB INIT ERROR at #{Time.now.to_s}:"
|
115
|
+
RJ.logger.error $!.inspect
|
116
|
+
RJ.logger.error $!.backtrace
|
117
|
+
RJ.logger.error "message: #{payload.inspect}"
|
120
118
|
# Mailer.send(klass_name, params, $!)
|
121
119
|
# raise $!
|
122
120
|
end
|
@@ -7,7 +7,6 @@ require 'yaml'
|
|
7
7
|
module RabbitJobs
|
8
8
|
class Scheduler
|
9
9
|
include AmqpHelpers
|
10
|
-
include Logger
|
11
10
|
|
12
11
|
attr_accessor :pidfile, :background, :schedule
|
13
12
|
|
@@ -33,12 +32,12 @@ module RabbitJobs
|
|
33
32
|
# job should be scheduled regardless of what ENV['RAILS_ENV'] is set
|
34
33
|
# to.
|
35
34
|
if config['rails_env'].nil? || rails_env_matches?(config)
|
36
|
-
|
35
|
+
RJ.logger.info "Scheduling #{name} "
|
37
36
|
interval_defined = false
|
38
37
|
interval_types = %w{cron every}
|
39
38
|
interval_types.each do |interval_type|
|
40
39
|
if !config[interval_type].nil? && config[interval_type].length > 0
|
41
|
-
|
40
|
+
RJ.logger.info "queueing #{config['class']} (#{name})"
|
42
41
|
rufus_scheduler.send(interval_type, config[interval_type]) do
|
43
42
|
publish_from_config(config)
|
44
43
|
end
|
@@ -47,7 +46,7 @@ module RabbitJobs
|
|
47
46
|
end
|
48
47
|
end
|
49
48
|
unless interval_defined
|
50
|
-
|
49
|
+
RJ.logger.warn "no #{interval_types.join(' / ')} found for #{config['class']} (#{name}) - skipping"
|
51
50
|
end
|
52
51
|
end
|
53
52
|
end
|
@@ -65,10 +64,10 @@ module RabbitJobs
|
|
65
64
|
params = args.is_a?(Hash) ? [args] : Array(args)
|
66
65
|
queue = config['queue'] || config[:queue] || RabbitJobs.config.routing_keys.first
|
67
66
|
|
68
|
-
|
67
|
+
RJ.logger.info "publishing #{config}"
|
69
68
|
RabbitJobs.publish_to(queue, klass_name.constantize, nil, *params)
|
70
69
|
rescue
|
71
|
-
|
70
|
+
RJ.logger.warn "Failed to publish #{klass_name}:\n #{$!}\n params = #{params.inspect}"
|
72
71
|
end
|
73
72
|
|
74
73
|
def rufus_scheduler
|
@@ -107,7 +106,7 @@ module RabbitJobs
|
|
107
106
|
end
|
108
107
|
end
|
109
108
|
|
110
|
-
|
109
|
+
RJ.logger.info "Scheduler started."
|
111
110
|
|
112
111
|
EM.add_periodic_timer(1) do
|
113
112
|
check_shutdown.call
|
@@ -116,7 +115,7 @@ module RabbitJobs
|
|
116
115
|
end
|
117
116
|
|
118
117
|
def shutdown
|
119
|
-
|
118
|
+
RJ.logger.warn "Stopping scheduler..."
|
120
119
|
@shutdown = true
|
121
120
|
end
|
122
121
|
|
@@ -146,11 +145,11 @@ module RabbitJobs
|
|
146
145
|
|
147
146
|
def kill_child
|
148
147
|
if @job && @job.child_pid
|
149
|
-
#
|
148
|
+
# RJ.logger.warn "Killing child at #{@child}"
|
150
149
|
if Kernel.system("ps -o pid,state -p #{@job.child_pid}")
|
151
150
|
Process.kill("KILL", @job.child_pid) rescue nil
|
152
151
|
else
|
153
|
-
#
|
152
|
+
# RJ.logger.warn "Child #{@child} not found, restarting."
|
154
153
|
# shutdown
|
155
154
|
end
|
156
155
|
end
|
data/lib/rabbit_jobs/tasks.rb
CHANGED
@@ -2,8 +2,18 @@
|
|
2
2
|
# will give you the resque tasks
|
3
3
|
|
4
4
|
require 'rabbit_jobs'
|
5
|
+
require 'logger'
|
5
6
|
|
6
7
|
namespace :rj do
|
8
|
+
def initialize_rj_daemon(daemon)
|
9
|
+
daemon.pidfile = ENV['PIDFILE']
|
10
|
+
daemon.background = %w(yes true).include? ENV['BACKGROUND']
|
11
|
+
RJ.logger = ::Logger.new(ENV['LOGFILE']) if ENV['LOGFILE']
|
12
|
+
RJ.logger.level = ENV['VERBOSE'] ? Logger::INFO : Logger::WARN
|
13
|
+
|
14
|
+
worker
|
15
|
+
end
|
16
|
+
|
7
17
|
task :setup
|
8
18
|
|
9
19
|
desc "Start a Rabbit Jobs worker"
|
@@ -11,30 +21,14 @@ namespace :rj do
|
|
11
21
|
require 'rabbit_jobs'
|
12
22
|
|
13
23
|
queues = (ENV['QUEUES'] || ENV['QUEUE']).to_s.split(',')
|
14
|
-
|
15
|
-
begin
|
16
|
-
worker = RabbitJobs::Worker.new(*queues)
|
17
|
-
worker.pidfile = ENV['PIDFILE']
|
18
|
-
worker.background = %w(yes true).include? ENV['BACKGROUND']
|
19
|
-
RabbitJobs::Logger.verbose = true if ENV['VERBOSE']
|
20
|
-
# worker.very_verbose = ENV['VVERBOSE']
|
21
|
-
end
|
24
|
+
worker = initialize_rj_service(RJ::Worker.new(*queues))
|
22
25
|
|
23
26
|
worker.work
|
24
27
|
end
|
25
28
|
|
26
29
|
desc "Start a Rabbit Jobs scheduler"
|
27
30
|
task :scheduler => [ :preload, :setup ] do
|
28
|
-
|
29
|
-
queues = (ENV['SCHEDULE']).to_s.split(',')
|
30
|
-
|
31
|
-
begin
|
32
|
-
scheduler = RabbitJobs::Scheduler.new(*queues)
|
33
|
-
scheduler.pidfile = ENV['PIDFILE']
|
34
|
-
scheduler.background = %w(yes true).include? ENV['BACKGROUND']
|
35
|
-
RabbitJobs::Logger.verbose = true if ENV['VERBOSE']
|
36
|
-
# worker.very_verbose = ENV['VVERBOSE']
|
37
|
-
end
|
31
|
+
scheduler = initialize_rj_service(RabbitJobs::Scheduler.new)
|
38
32
|
|
39
33
|
scheduler.work
|
40
34
|
end
|
data/lib/rabbit_jobs/version.rb
CHANGED
data/lib/rabbit_jobs/worker.rb
CHANGED
@@ -3,7 +3,6 @@
|
|
3
3
|
module RabbitJobs
|
4
4
|
class Worker
|
5
5
|
include AmqpHelpers
|
6
|
-
include Logger
|
7
6
|
|
8
7
|
attr_accessor :pidfile, :background
|
9
8
|
|
@@ -39,8 +38,8 @@ module RabbitJobs
|
|
39
38
|
|
40
39
|
check_shutdown = Proc.new {
|
41
40
|
if @shutdown
|
42
|
-
|
43
|
-
|
41
|
+
RJ.logger.info "Processed jobs: #{processed_count}"
|
42
|
+
RJ.logger.info "Stopping worker..."
|
44
43
|
|
45
44
|
connection.close {
|
46
45
|
File.delete(self.pidfile) if self.pidfile
|
@@ -52,7 +51,7 @@ module RabbitJobs
|
|
52
51
|
queues.each do |routing_key|
|
53
52
|
queue = make_queue(exchange, routing_key)
|
54
53
|
|
55
|
-
|
54
|
+
RJ.logger.info "Worker ##{Process.pid} <= #{exchange.name}##{routing_key}"
|
56
55
|
|
57
56
|
explicit_ack = !!RJ.config[:queues][routing_key][:ack]
|
58
57
|
|
@@ -63,7 +62,7 @@ module RabbitJobs
|
|
63
62
|
@job.run_perform
|
64
63
|
processed_count += 1
|
65
64
|
else
|
66
|
-
|
65
|
+
RJ.logger.info "Job expired: #{@job.inspect}"
|
67
66
|
end
|
68
67
|
|
69
68
|
metadata.ack if explicit_ack
|
@@ -115,11 +114,11 @@ module RabbitJobs
|
|
115
114
|
|
116
115
|
def kill_child
|
117
116
|
if @job && @job.child_pid
|
118
|
-
#
|
117
|
+
# RJ.logger.info "Killing child at #{@child}"
|
119
118
|
if Kernel.system("ps -o pid,state -p #{@job.child_pid}")
|
120
119
|
Process.kill("KILL", @job.child_pid) rescue nil
|
121
120
|
else
|
122
|
-
#
|
121
|
+
# RJ.logger.info "Child #{@child} not found, restarting."
|
123
122
|
# shutdown
|
124
123
|
end
|
125
124
|
end
|
data/lib/rabbit_jobs.rb
CHANGED
@@ -5,7 +5,6 @@ require 'rabbit_jobs/version'
|
|
5
5
|
require 'rabbit_jobs/helpers'
|
6
6
|
require 'rabbit_jobs/amqp_helpers'
|
7
7
|
require 'rabbit_jobs/configuration'
|
8
|
-
require 'rabbit_jobs/logger'
|
9
8
|
require 'rabbit_jobs/error_mailer'
|
10
9
|
|
11
10
|
require 'rabbit_jobs/job'
|
@@ -13,6 +12,8 @@ require 'rabbit_jobs/publisher'
|
|
13
12
|
require 'rabbit_jobs/worker'
|
14
13
|
require 'rabbit_jobs/scheduler'
|
15
14
|
|
15
|
+
require 'logger'
|
16
|
+
|
16
17
|
module RabbitJobs
|
17
18
|
extend self
|
18
19
|
|
@@ -23,6 +24,11 @@ module RabbitJobs
|
|
23
24
|
def publish_to(routing_key, klass, opts = {}, *params)
|
24
25
|
RabbitJobs::Publisher.publish_to(routing_key, klass, opts, *params)
|
25
26
|
end
|
27
|
+
|
28
|
+
attr_writer :logger
|
29
|
+
def logger
|
30
|
+
@logger ||= Logger.new $stdout
|
31
|
+
end
|
26
32
|
end
|
27
33
|
|
28
34
|
RJ = RabbitJobs
|
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.
|
4
|
+
version: 0.0.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-02-21 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: amqp
|
16
|
-
requirement: &
|
16
|
+
requirement: &70110815973060 !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: *70110815973060
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &70110815972640 !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: *70110815972640
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rufus-scheduler
|
38
|
-
requirement: &
|
38
|
+
requirement: &70110815972100 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '2.0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70110815972100
|
47
47
|
description: Background jobs on RabbitMQ
|
48
48
|
email:
|
49
49
|
- lazureykis@gmail.com
|
@@ -64,7 +64,6 @@ files:
|
|
64
64
|
- lib/rabbit_jobs/error_mailer.rb
|
65
65
|
- lib/rabbit_jobs/helpers.rb
|
66
66
|
- lib/rabbit_jobs/job.rb
|
67
|
-
- lib/rabbit_jobs/logger.rb
|
68
67
|
- lib/rabbit_jobs/publisher.rb
|
69
68
|
- lib/rabbit_jobs/scheduler.rb
|
70
69
|
- lib/rabbit_jobs/tasks.rb
|
@@ -81,7 +80,6 @@ files:
|
|
81
80
|
- spec/spec_helper.rb
|
82
81
|
- spec/unit/configuration_spec.rb
|
83
82
|
- spec/unit/job_spec.rb
|
84
|
-
- spec/unit/logger_spec.rb
|
85
83
|
- spec/unit/mailer_spec.rb
|
86
84
|
- spec/unit/rabbit_jobs_spec.rb
|
87
85
|
- spec/unit/worker_spec.rb
|
@@ -119,7 +117,6 @@ test_files:
|
|
119
117
|
- spec/spec_helper.rb
|
120
118
|
- spec/unit/configuration_spec.rb
|
121
119
|
- spec/unit/job_spec.rb
|
122
|
-
- spec/unit/logger_spec.rb
|
123
120
|
- spec/unit/mailer_spec.rb
|
124
121
|
- spec/unit/rabbit_jobs_spec.rb
|
125
122
|
- spec/unit/worker_spec.rb
|
data/lib/rabbit_jobs/logger.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
module RabbitJobs
|
2
|
-
module Logger
|
3
|
-
extend self
|
4
|
-
|
5
|
-
def log(string)
|
6
|
-
puts string
|
7
|
-
end
|
8
|
-
|
9
|
-
def log!(string)
|
10
|
-
@@verbose ||= false
|
11
|
-
log(string) if RabbitJobs::Logger.verbose
|
12
|
-
end
|
13
|
-
|
14
|
-
class << self
|
15
|
-
attr_accessor :verbose
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
data/spec/unit/logger_spec.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
require 'spec_helper'
|
3
|
-
|
4
|
-
describe RabbitJobs::Logger do
|
5
|
-
it '#log should write messages to stdout' do
|
6
|
-
mock($stdout).puts "hello"
|
7
|
-
RabbitJobs::Logger.log("hello")
|
8
|
-
end
|
9
|
-
|
10
|
-
it '#log! should not write messages to stdout in normal mode' do
|
11
|
-
RabbitJobs::Logger.verbose = false
|
12
|
-
|
13
|
-
dont_allow(RabbitJobs::Logger).log("hello")
|
14
|
-
RabbitJobs::Logger.log!("hello")
|
15
|
-
end
|
16
|
-
|
17
|
-
it '#log! should write messages to stdout in verbose mode' do
|
18
|
-
RabbitJobs::Logger.verbose = true
|
19
|
-
|
20
|
-
mock(RabbitJobs::Logger).log("hello")
|
21
|
-
RabbitJobs::Logger.log!("hello")
|
22
|
-
end
|
23
|
-
end
|