dj_remixes 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2010 markbates
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,3 @@
1
+ README
2
+ ========================================================================
3
+ dj_remixes was developed by: markbates
@@ -0,0 +1,11 @@
1
+ module DJ
2
+
3
+ class << self
4
+
5
+ def method_missing(sym, *args, &block)
6
+ Delayed::Worker.backend.send(sym, *args, &block)
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,12 @@
1
+ module DJ
2
+ class Worker
3
+
4
+ def failure_with_hoptoad(job, error)
5
+ HoptoadNotifier.notify_or_ignore(error, :cgi_data => self.attributes)
6
+ failure_without_hoptoad(job, error)
7
+ end
8
+
9
+ alias_method_chain :failure, :hoptoad
10
+
11
+ end
12
+ end
@@ -0,0 +1,62 @@
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
@@ -0,0 +1,14 @@
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
@@ -0,0 +1,26 @@
1
+ module Mail
2
+ class Message
3
+
4
+ class MailmanWorker < DJ::Worker
5
+ priority :immediate
6
+
7
+ def perform
8
+ message = Marshal.load(self.mail)
9
+ message.deliver_without_worker
10
+ end
11
+ end
12
+
13
+ def deliver_with_worker
14
+ if ActionMailer::Base.delivery_method == :test
15
+ deliver_without_worker
16
+ else
17
+ Mail::Message::MailmanWorker.enqueue(:mail => Marshal.dump(self))
18
+ return self
19
+ end
20
+ end
21
+
22
+ alias_method_chain :deliver, :worker
23
+
24
+ end # Base
25
+ end # ActionMailer
26
+
@@ -0,0 +1,10 @@
1
+ module DJ
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer "dj.configure_rails_initialization" do
5
+ ActiveSupport.on_load(:action_mailer) do
6
+ require File.join(File.dirname(__FILE__), 'action_mailer')
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ # Rails 3 style validation:
2
+ class UniqueDJValidator < ActiveModel::Validator
3
+ def validate(record)
4
+
5
+ if record.payload_object.respond_to?(:unique?) && record.new_record?
6
+ if record.payload_object.unique?
7
+ if DJ.where(:worker_class_name => record.payload_object.worker_class_name, :finished_at => nil).count > 0
8
+ record.errors.add_to_base("Only one #{record.payload_object.worker_class_name} can be queued at a time!")
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ Delayed::Worker.backend.send(:class_eval) do
16
+ validates_with UniqueDJValidator
17
+ end
@@ -0,0 +1,147 @@
1
+ module DJ
2
+ class Worker
3
+
4
+ PRIORITY_LEVELS = {:immediate => 10000, :high => 1000, :medium => 500, :normal => 0, :low => -100, :who_cares => -1000}
5
+
6
+ attr_accessor :run_at
7
+ attr_accessor :priority
8
+ attr_accessor :worker_class_name
9
+ attr_accessor :re_enqueuable
10
+ attr_accessor :id
11
+ attr_accessor :attributes
12
+ attr_accessor :dj_object
13
+
14
+ class << self
15
+
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
+ def enqueue(*args)
32
+ self.new(*args).enqueue
33
+ end
34
+
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
+ end
51
+
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
+ def run_at
77
+ return @run_at ||= Time.now
78
+ end
79
+
80
+ def dj_object=(dj)
81
+ @dj_object = dj.id
82
+ end
83
+
84
+ def dj_object
85
+ DJ.find(@dj_object)
86
+ end
87
+
88
+ def worker_class_name
89
+ if self.id
90
+ @worker_class_name ||= File.join(self.class.to_s.underscore, self.id.to_s)
91
+ else
92
+ @worker_class_name ||= self.class.to_s.underscore
93
+ end
94
+ end
95
+
96
+ def enqueue(priority = self.priority, run_at = self.run_at)
97
+ job = DJ.enqueue(self, priority, run_at)
98
+ job.worker_class_name = self.worker_class_name
99
+ job.save
100
+ return job
101
+ end
102
+
103
+ alias_method :save, :enqueue
104
+
105
+ def unique?
106
+ false
107
+ end
108
+
109
+ def clone
110
+ cl = super
111
+ cl.run_at = nil
112
+ cl
113
+ end
114
+
115
+ def before(job)
116
+ self.dj_object = job
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
data/lib/dj_remixes.rb ADDED
@@ -0,0 +1,20 @@
1
+ # Dir.glob(File.join(File.dirname(__FILE__), 'dj_remixes', '**/*.rb')).each do |f|
2
+ # require File.expand_path(f)
3
+ # end
4
+ require 'delayed_job'
5
+
6
+ Delayed::Worker.guess_backend
7
+
8
+ path = File.join(File.dirname(__FILE__), 'dj_remixes')
9
+
10
+ require File.join(path, 'dj_remixes')
11
+ require File.join(path, 'worker')
12
+ require File.join(path, 'hoptoad')
13
+
14
+ if Rails.version.match(/^2/)
15
+ require File.join(path, 'rails2', 'action_mailer')
16
+ require File.join(path, 'rails2', 'unique_validator')
17
+ else
18
+ require File.join(path, 'rails3', 'railtie')
19
+ require File.join(path, 'rails3', 'unique_validator')
20
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dj_remixes
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 10
9
+ version: 0.0.10
10
+ platform: ruby
11
+ authors:
12
+ - markbates
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-14 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: "dj_remixes was developed by: markbates"
22
+ email: ""
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ - LICENSE
30
+ files:
31
+ - lib/dj_remixes/dj_remixes.rb
32
+ - lib/dj_remixes/hoptoad.rb
33
+ - lib/dj_remixes/rails2/action_mailer.rb
34
+ - lib/dj_remixes/rails2/unique_validator.rb
35
+ - lib/dj_remixes/rails3/action_mailer.rb
36
+ - lib/dj_remixes/rails3/railtie.rb
37
+ - lib/dj_remixes/rails3/unique_validator.rb
38
+ - lib/dj_remixes/worker.rb
39
+ - lib/dj_remixes.rb
40
+ - README
41
+ - LICENSE
42
+ has_rdoc: true
43
+ homepage: ""
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.6
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: dj_remixes
72
+ test_files: []
73
+