dj_remixes 0.0.10 → 0.0.17
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dj_remixes/attributes.rb +27 -0
- data/lib/dj_remixes/callbacks.rb +22 -0
- data/lib/dj_remixes/dj_remixes.rb +17 -0
- data/lib/dj_remixes/hoptoad.rb +12 -9
- data/lib/dj_remixes/priority.rb +34 -0
- data/lib/dj_remixes/rails3/action_mailer.rb +2 -1
- data/lib/dj_remixes/re_enqueue.rb +44 -0
- data/lib/dj_remixes/run_at.rb +19 -0
- data/lib/dj_remixes/unique.rb +36 -0
- data/lib/dj_remixes/worker.rb +7 -97
- data/lib/dj_remixes.rb +9 -3
- metadata +12 -6
- data/lib/dj_remixes/rails2/action_mailer.rb +0 -62
- data/lib/dj_remixes/rails2/unique_validator.rb +0 -14
@@ -0,0 +1,27 @@
|
|
1
|
+
module DJ
|
2
|
+
class Worker
|
3
|
+
|
4
|
+
attr_accessor :id
|
5
|
+
attr_accessor :attributes
|
6
|
+
|
7
|
+
def initialize(attributes = {})
|
8
|
+
self.attributes = attributes
|
9
|
+
self.attributes = self.attributes.stringify_keys
|
10
|
+
self.id = self.attributes['id']
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing(sym, *args, &block)
|
14
|
+
attribute = sym.to_s
|
15
|
+
case attribute
|
16
|
+
when /(.+)\=$/
|
17
|
+
self.attributes[$1] = args.first
|
18
|
+
when /(.+)\?$/
|
19
|
+
# self.attributes.has_key?($1.to_sym)
|
20
|
+
return self.attributes[$1]
|
21
|
+
else
|
22
|
+
return self.attributes[attribute]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end # Worker
|
27
|
+
end # DJ
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module DJ
|
2
|
+
class Worker
|
3
|
+
|
4
|
+
def before(job)
|
5
|
+
self.dj_object = job
|
6
|
+
job.touch(:started_at)
|
7
|
+
end
|
8
|
+
|
9
|
+
def after(job)
|
10
|
+
end
|
11
|
+
|
12
|
+
def success(job)
|
13
|
+
job.touch(:finished_at)
|
14
|
+
self.enqueue_again
|
15
|
+
end
|
16
|
+
|
17
|
+
def failure(job, error)
|
18
|
+
job.update_attributes(:started_at => nil)
|
19
|
+
end
|
20
|
+
|
21
|
+
end # Worker
|
22
|
+
end # DJ
|
@@ -2,10 +2,27 @@ module DJ
|
|
2
2
|
|
3
3
|
class << self
|
4
4
|
|
5
|
+
# Pass off calls to the backend:
|
5
6
|
def method_missing(sym, *args, &block)
|
6
7
|
Delayed::Worker.backend.send(sym, *args, &block)
|
7
8
|
end
|
8
9
|
|
9
10
|
end
|
10
11
|
|
12
|
+
end
|
13
|
+
|
14
|
+
Delayed::Worker.backend.send(:class_eval) do
|
15
|
+
def invoke_job_with_callbacks
|
16
|
+
payload_object.before(self) if payload_object.respond_to?(:before)
|
17
|
+
begin
|
18
|
+
invoke_job_without_callbacks
|
19
|
+
payload_object.success(self) if payload_object.respond_to?(:success)
|
20
|
+
rescue Exception => e
|
21
|
+
payload_object.failure(self, e) if payload_object.respond_to?(:failure)
|
22
|
+
raise e
|
23
|
+
ensure
|
24
|
+
payload_object.after(self) if payload_object.respond_to?(:after)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
alias_method_chain :invoke_job, :callbacks
|
11
28
|
end
|
data/lib/dj_remixes/hoptoad.rb
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
if Object.const_defined?(:HoptoadNotifier)
|
2
|
+
module DJ
|
3
|
+
class Worker
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
# Report Errors to Hoptoad:
|
6
|
+
def failure_with_hoptoad(job, error)
|
7
|
+
HoptoadNotifier.notify_or_ignore(error, :cgi_data => self.attributes)
|
8
|
+
failure_without_hoptoad(job, error)
|
9
|
+
end
|
8
10
|
|
9
|
-
|
11
|
+
alias_method_chain :failure, :hoptoad
|
10
12
|
|
11
|
-
|
12
|
-
end
|
13
|
+
end # Worker
|
14
|
+
end # DJ
|
15
|
+
end # defined?
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module DJ
|
2
|
+
class Worker
|
3
|
+
|
4
|
+
# A helpful list of symbols to priority levels
|
5
|
+
PRIORITY_LEVELS = {:urgent => -100000, :immediate => -10000, :high => -1000, :medium => -500, :normal => 0, :low => 100, :who_cares => 1000}
|
6
|
+
|
7
|
+
attr_accessor :priority
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
def priority(level = 0)
|
12
|
+
define_method('priority') do
|
13
|
+
if level.is_a?(Symbol)
|
14
|
+
level = DJ::Worker::PRIORITY_LEVELS[level] ||= 0
|
15
|
+
end
|
16
|
+
return @priority ||= level
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def priority
|
23
|
+
case @priority
|
24
|
+
when Symbol
|
25
|
+
DJ::Worker::PRIORITY_LEVELS[@priority] ||= 0
|
26
|
+
when Fixnum
|
27
|
+
@priority
|
28
|
+
else
|
29
|
+
0
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module DJ
|
2
|
+
class Worker
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
# Class level accessor to let DJ know whether it
|
7
|
+
# should be enqueued again after it has successfully
|
8
|
+
# completed.
|
9
|
+
attr_accessor :re_enqueuable
|
10
|
+
|
11
|
+
# A convience method to tell DJ to re-enqueue this worker
|
12
|
+
# after it has successfully completely. NOTE: This will actually
|
13
|
+
# create a new DJ object in the database, not reuse the same one.
|
14
|
+
#
|
15
|
+
# Example:
|
16
|
+
# # Run every 30 days and charge a credit card.
|
17
|
+
# class SubscriptionWorker < DJ::Worker
|
18
|
+
# re_enqueue
|
19
|
+
#
|
20
|
+
# def run_at
|
21
|
+
# 30.days.from_now
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# def perform
|
25
|
+
# # charge the credit card...
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
def re_enqueue
|
29
|
+
self.re_enqueuable = true
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
protected
|
35
|
+
# clone and re-enqueue the worker.
|
36
|
+
def enqueue_again # :nodoc:
|
37
|
+
if self.class.re_enqueuable
|
38
|
+
new_worker = self.clone()
|
39
|
+
new_worker.enqueue
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end # Worker
|
44
|
+
end # DJ
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module DJ
|
2
|
+
class Worker
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
# Tell DJ to only allow one of this worker at a given time.
|
7
|
+
#
|
8
|
+
# Example:
|
9
|
+
# # We only want to charge the card once!
|
10
|
+
# class PurchaseWorker < DJ::Worker
|
11
|
+
# is_unique
|
12
|
+
#
|
13
|
+
# def perform
|
14
|
+
# # charge the credit card...
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
def is_unique
|
18
|
+
define_method('unique?') do
|
19
|
+
return true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
# Returns true or false based on whether this should be the only
|
26
|
+
# worker that should be allowed in the system at any one time.
|
27
|
+
# If set to true, using the is_unique class method, then when a
|
28
|
+
# new job is enqueued it will be validated against the worker_class_name
|
29
|
+
# field in the database. If there is already a job with the same
|
30
|
+
# worker_class_name then the enqueing of the new job will fail.
|
31
|
+
def unique?
|
32
|
+
false
|
33
|
+
end
|
34
|
+
|
35
|
+
end # Worker
|
36
|
+
end # DJ
|
data/lib/dj_remixes/worker.rb
CHANGED
@@ -1,78 +1,17 @@
|
|
1
1
|
module DJ
|
2
2
|
class Worker
|
3
3
|
|
4
|
-
PRIORITY_LEVELS = {:immediate => 10000, :high => 1000, :medium => 500, :normal => 0, :low => -100, :who_cares => -1000}
|
5
|
-
|
6
4
|
attr_accessor :run_at
|
7
|
-
attr_accessor :priority
|
8
5
|
attr_accessor :worker_class_name
|
9
|
-
attr_accessor :re_enqueuable
|
10
|
-
attr_accessor :id
|
11
|
-
attr_accessor :attributes
|
12
|
-
attr_accessor :dj_object
|
13
6
|
|
14
7
|
class << self
|
15
8
|
|
16
|
-
def priority(level = 0)
|
17
|
-
define_method('priority') do
|
18
|
-
if level.is_a?(Symbol)
|
19
|
-
level = DJ::Worker::PRIORITY_LEVELS[level] ||= 0
|
20
|
-
end
|
21
|
-
return @priority ||= level
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def is_unique
|
26
|
-
define_method('unique?') do
|
27
|
-
return true
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
9
|
def enqueue(*args)
|
32
10
|
self.new(*args).enqueue
|
33
11
|
end
|
34
12
|
|
35
|
-
def re_enqueue(&block)
|
36
|
-
define_method('re_enqueuable') do
|
37
|
-
true
|
38
|
-
end
|
39
|
-
define_method('__re_enqueue_block') do
|
40
|
-
block
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|
45
|
-
|
46
|
-
def initialize(attributes = {})
|
47
|
-
self.attributes = attributes
|
48
|
-
self.attributes = self.attributes.stringify_keys
|
49
|
-
self.id = self.attributes['id']
|
50
13
|
end
|
51
14
|
|
52
|
-
def method_missing(sym, *args, &block)
|
53
|
-
attribute = sym.to_s
|
54
|
-
case attribute
|
55
|
-
when /(.+)\=$/
|
56
|
-
self.attributes[$1] = args.first
|
57
|
-
when /(.+)\?$/
|
58
|
-
# self.attributes.has_key?($1.to_sym)
|
59
|
-
return self.attributes[$1]
|
60
|
-
else
|
61
|
-
return self.attributes[attribute]
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def priority
|
66
|
-
case @priority
|
67
|
-
when Symbol
|
68
|
-
DJ::Worker::PRIORITY_LEVELS[@priority] ||= 0
|
69
|
-
when Fixnum
|
70
|
-
@priority
|
71
|
-
else
|
72
|
-
0
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
15
|
def run_at
|
77
16
|
return @run_at ||= Time.now
|
78
17
|
end
|
@@ -102,46 +41,17 @@ module DJ
|
|
102
41
|
|
103
42
|
alias_method :save, :enqueue
|
104
43
|
|
105
|
-
|
106
|
-
|
44
|
+
# Needs to be implemented by subclasses! It's only here
|
45
|
+
# so people can hook into it.
|
46
|
+
def perform
|
47
|
+
raise NoMethodError.new('perform')
|
107
48
|
end
|
108
49
|
|
109
|
-
def clone
|
50
|
+
def clone # :nodoc:
|
110
51
|
cl = super
|
111
52
|
cl.run_at = nil
|
112
53
|
cl
|
113
54
|
end
|
114
55
|
|
115
|
-
|
116
|
-
|
117
|
-
job.touch(:started_at)
|
118
|
-
end
|
119
|
-
|
120
|
-
def perform
|
121
|
-
raise NoMethodError.new('perform')
|
122
|
-
end
|
123
|
-
|
124
|
-
def after(job)
|
125
|
-
end
|
126
|
-
|
127
|
-
def success(job)
|
128
|
-
job.touch(:finished_at)
|
129
|
-
enqueue_again
|
130
|
-
end
|
131
|
-
|
132
|
-
def failure(job, error)
|
133
|
-
job.update_attributes(:started_at => nil)
|
134
|
-
end
|
135
|
-
|
136
|
-
def enqueue_again
|
137
|
-
if self.re_enqueuable
|
138
|
-
new_worker = self.clone()
|
139
|
-
if self.__re_enqueue_block
|
140
|
-
self.__re_enqueue_block.call(self, new_worker)
|
141
|
-
end
|
142
|
-
new_worker.enqueue
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
end
|
147
|
-
end
|
56
|
+
end # Worker
|
57
|
+
end # DJ
|
data/lib/dj_remixes.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Dir.glob(File.join(File.dirname(__FILE__), 'dj_remixes', '**/*.rb')).each do |f|
|
2
2
|
# require File.expand_path(f)
|
3
3
|
# end
|
4
|
-
require 'delayed_job'
|
4
|
+
# require 'delayed_job'
|
5
5
|
|
6
6
|
Delayed::Worker.guess_backend
|
7
7
|
|
@@ -9,11 +9,17 @@ path = File.join(File.dirname(__FILE__), 'dj_remixes')
|
|
9
9
|
|
10
10
|
require File.join(path, 'dj_remixes')
|
11
11
|
require File.join(path, 'worker')
|
12
|
+
require File.join(path, 'attributes')
|
13
|
+
require File.join(path, 'callbacks')
|
14
|
+
require File.join(path, 'priority')
|
15
|
+
require File.join(path, 'run_at')
|
16
|
+
require File.join(path, 'unique')
|
17
|
+
require File.join(path, 're_enqueue')
|
12
18
|
require File.join(path, 'hoptoad')
|
13
19
|
|
14
20
|
if Rails.version.match(/^2/)
|
15
|
-
require File.join(path, 'rails2', 'action_mailer')
|
16
|
-
require File.join(path, 'rails2', 'unique_validator')
|
21
|
+
# require File.join(path, 'rails2', 'action_mailer')
|
22
|
+
# require File.join(path, 'rails2', 'unique_validator')
|
17
23
|
else
|
18
24
|
require File.join(path, 'rails3', 'railtie')
|
19
25
|
require File.join(path, 'rails3', 'unique_validator')
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 17
|
9
|
+
version: 0.0.17
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- markbates
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-06
|
17
|
+
date: 2010-07-06 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -28,13 +28,17 @@ extra_rdoc_files:
|
|
28
28
|
- README
|
29
29
|
- LICENSE
|
30
30
|
files:
|
31
|
+
- lib/dj_remixes/attributes.rb
|
32
|
+
- lib/dj_remixes/callbacks.rb
|
31
33
|
- lib/dj_remixes/dj_remixes.rb
|
32
34
|
- lib/dj_remixes/hoptoad.rb
|
33
|
-
- lib/dj_remixes/
|
34
|
-
- lib/dj_remixes/rails2/unique_validator.rb
|
35
|
+
- lib/dj_remixes/priority.rb
|
35
36
|
- lib/dj_remixes/rails3/action_mailer.rb
|
36
37
|
- lib/dj_remixes/rails3/railtie.rb
|
37
38
|
- lib/dj_remixes/rails3/unique_validator.rb
|
39
|
+
- lib/dj_remixes/re_enqueue.rb
|
40
|
+
- lib/dj_remixes/run_at.rb
|
41
|
+
- lib/dj_remixes/unique.rb
|
38
42
|
- lib/dj_remixes/worker.rb
|
39
43
|
- lib/dj_remixes.rb
|
40
44
|
- README
|
@@ -49,6 +53,7 @@ rdoc_options: []
|
|
49
53
|
require_paths:
|
50
54
|
- lib
|
51
55
|
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
52
57
|
requirements:
|
53
58
|
- - ">="
|
54
59
|
- !ruby/object:Gem::Version
|
@@ -56,6 +61,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
56
61
|
- 0
|
57
62
|
version: "0"
|
58
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
59
65
|
requirements:
|
60
66
|
- - ">="
|
61
67
|
- !ruby/object:Gem::Version
|
@@ -65,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
71
|
requirements: []
|
66
72
|
|
67
73
|
rubyforge_project:
|
68
|
-
rubygems_version: 1.3.
|
74
|
+
rubygems_version: 1.3.7
|
69
75
|
signing_key:
|
70
76
|
specification_version: 3
|
71
77
|
summary: dj_remixes
|
@@ -1,62 +0,0 @@
|
|
1
|
-
if defined?(ActionMailer)
|
2
|
-
module ActionMailer
|
3
|
-
class Base
|
4
|
-
|
5
|
-
class << self
|
6
|
-
|
7
|
-
def inherited(klass)
|
8
|
-
super
|
9
|
-
eval %{
|
10
|
-
class ::#{klass}Worker < DJ::Worker
|
11
|
-
|
12
|
-
priority :immediate
|
13
|
-
|
14
|
-
attr_accessor :called_method
|
15
|
-
attr_accessor :args
|
16
|
-
|
17
|
-
def initialize(called_method, *args)
|
18
|
-
self.called_method = called_method
|
19
|
-
self.args = args
|
20
|
-
end
|
21
|
-
|
22
|
-
def perform
|
23
|
-
# ::#{klass}.send(self.called_method, *self.args)
|
24
|
-
::#{klass}.send(:new, self.called_method, *self.args).deliver!
|
25
|
-
end
|
26
|
-
|
27
|
-
class << self
|
28
|
-
|
29
|
-
def method_missing(sym, *args)
|
30
|
-
::#{klass}Worker.enqueue(sym, *args)
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
}
|
37
|
-
end
|
38
|
-
|
39
|
-
def method_missing_with_extras(method_symbol, *parameters) #:nodoc:
|
40
|
-
if ActionMailer::Base.delivery_method == :test
|
41
|
-
return method_missing_without_extras(method_symbol, *parameters)
|
42
|
-
end
|
43
|
-
|
44
|
-
if match = matches_dynamic_method?(method_symbol)
|
45
|
-
case match[1]
|
46
|
-
when 'deliver'# then new(match[2], *parameters).deliver!
|
47
|
-
"#{self.name}Worker".constantize.enqueue(match[2], *parameters)
|
48
|
-
else
|
49
|
-
method_missing_without_extras(method_symbol, *parameters)
|
50
|
-
end
|
51
|
-
else
|
52
|
-
super
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
alias_method_chain :method_missing, :extras
|
57
|
-
|
58
|
-
end # class << self
|
59
|
-
|
60
|
-
end # Base
|
61
|
-
end # ActionMailer
|
62
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
Delayed::Worker.backend.send(:class_eval) do
|
2
|
-
def validate_with_unique
|
3
|
-
validate_without_unique
|
4
|
-
if self.payload_object.respond_to?(:unique?) && self.new_record?
|
5
|
-
if self.payload_object.unique?
|
6
|
-
if DJ.count(:all, :conditions => {:worker_class_name => self.payload_object.worker_class_name, :finished_at => nil}) > 0
|
7
|
-
self.errors.add_to_base("Only one #{self.payload_object.worker_class_name} can be queued at a time!")
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
alias_method_chain :validate, :unique
|
14
|
-
end
|