markbates-delayed_job_extras 0.1.1.20090805150516

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2009 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 @@
1
+ Adds support for Hoptoad and is_paranoid to Delayed::Job. Additionally it also adds 'stats' for your workers, and it even includes a base worker that encapsulates a some common functionality.
@@ -0,0 +1,10 @@
1
+ class DjExtrasGenerator < Rails::Generator::Base
2
+
3
+ def manifest
4
+ record do |m|
5
+ m.migration_template "migration.rb", 'db/migrate',
6
+ :migration_file_name => "add_delayed_job_extras"
7
+ end
8
+ end
9
+
10
+ end
@@ -0,0 +1,11 @@
1
+ class AddDelayedJobExtras < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :delayed_jobs, :deleted_at, :datetime
4
+ add_column :delayed_jobs, :worker_class_name, :string
5
+ end
6
+
7
+ def self.down
8
+ remove_column :delayed_jobs, :worker_class_name
9
+ remove_column :delayed_jobs, :deleted_at
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ begin
2
+ # If the hoptoad_notifier gem is available
3
+ # let's use it so we can get some good notifications
4
+ # when an exception is raised.
5
+ require 'hoptoad_notifier'
6
+ rescue Exception => e
7
+ end
8
+
9
+ begin
10
+ # If the is_paranoid gem is available
11
+ # let's use it so we can have a record of the
12
+ # tasks that have been performed.
13
+ require 'is_paranoid'
14
+ rescue Exception => e
15
+ end
16
+
17
+ Dir.glob(File.join(File.dirname(__FILE__), 'delayed_job_extras', '**/*.rb')).each do |f|
18
+ require File.expand_path(f)
19
+ end
@@ -0,0 +1,46 @@
1
+ module Delayed
2
+ class Job
3
+
4
+ if (self.respond_to?(:is_paranoid))
5
+ is_paranoid
6
+ else
7
+ alias_method :count_with_destroyed, :count
8
+ alias_method :find_with_destroyed, :find
9
+ end
10
+
11
+ def payload_object=(object)
12
+ self.worker_class_name = object.worker_class_name if object.respond_to?(:worker_class_name)
13
+ self['handler'] = object.to_yaml
14
+ end
15
+
16
+ class << self
17
+
18
+ def stats(start_date = 1.day.ago.beginning_of_day, end_date = Time.now.beginning_of_day)
19
+ workers = {}
20
+ Delayed::Job.find_with_destroyed(:all, :select => :worker_class_name, :group => :worker_class_name).each do |dj|
21
+ dj.worker_class_name = 'UNKNOWN' if dj.worker_class_name.blank?
22
+ workers[dj.worker_class_name] = {}
23
+ end
24
+ workers.each do |worker, stats|
25
+ wname = (worker == 'UNKNOWN' ? nil : worker)
26
+ stats[:total] = Delayed::Job.count_with_destroyed(:conditions => {:worker_class_name => wname})
27
+ stats[:remaining] = Delayed::Job.count(:conditions => {:worker_class_name => wname})
28
+ stats[:processed] = stats[:total] - stats[:remaining]
29
+ stats[:failures] = Delayed::Job.count(:conditions => ['worker_class_name = ? and attempts > 1', wname])
30
+ if start_date
31
+ date_stats = {:start_date => start_date, :end_date => end_date}
32
+ date_stats[:total] = Delayed::Job.count_with_destroyed(:conditions => ['worker_class_name = ? and (created_at > ? and created_at < ?)', wname, start_date, end_date])
33
+ date_stats[:remaining] = Delayed::Job.count(:conditions => ['worker_class_name = ? and (created_at > ? and created_at < ?)', wname, start_date, end_date])
34
+ date_stats[:processed] = stats[:total] - stats[:remaining]
35
+ date_stats[:failures] = Delayed::Job.count(:conditions => ['worker_class_name = ? and attempts > 1 and (created_at > ? and created_at < ?)', wname, start_date, end_date])
36
+ stats[:date_range] = date_stats
37
+ end
38
+ workers[worker] = stats
39
+ end
40
+ workers
41
+ end
42
+
43
+ end
44
+
45
+ end # Job
46
+ end # Delayed
@@ -0,0 +1,29 @@
1
+ module Delayed
2
+ class PerformableMethod
3
+
4
+ attr_accessor :worker_class_name
5
+
6
+ def initialize_with_worker_class_name(object, method, args)
7
+ self.worker_class_name = "#{object.class}__#{method}".underscore
8
+ initialize_without_worker_class_name(object, method, args)
9
+ end
10
+
11
+ alias_method_chain :initialize, :worker_class_name
12
+
13
+ if Object.const_defined?(:HoptoadNotifier)
14
+ include HoptoadNotifier::Catcher
15
+
16
+ def perform_with_hoptoad
17
+ begin
18
+ perform_without_hoptoad
19
+ rescue Exception => e
20
+ notify_hoptoad(e)
21
+ raise e
22
+ end
23
+ end
24
+
25
+ alias_method_chain :perform, :hoptoad
26
+ end
27
+
28
+ end # PerformableMethod
29
+ end # Delayed
@@ -0,0 +1,46 @@
1
+ module Delayed
2
+ class Worker
3
+
4
+ if Object.const_defined?(:HoptoadNotifier)
5
+ include HoptoadNotifier::Catcher
6
+ else
7
+ def notify_hoptoad(e)
8
+ end
9
+ end
10
+
11
+ def worker_class_name
12
+ self.class.to_s.underscore
13
+ end
14
+
15
+ def logger
16
+ RAILS_DEFAULT_LOGGER
17
+ end
18
+
19
+ class << self
20
+
21
+ # VideoWorker.encode(1) # => Delayed::Job.enqueue(VideoWorker.new(:encode, 1))
22
+ def method_missing(sym, *args)
23
+ self.enqueue(sym, *args)
24
+ end
25
+
26
+ # VideoWorker.enqueue(1) # => Delayed::Job.enqueue(VideoWorker.new(1))
27
+ def enqueue(*args)
28
+ Delayed::Job.enqueue(self.new(*args))
29
+ end
30
+
31
+ def perform(&block)
32
+ define_method(:perform) do
33
+ begin
34
+ self.instance_eval(&block)
35
+ rescue Exception => e
36
+ # send to hoptoad!
37
+ notify_hoptoad(e)
38
+ raise e
39
+ end
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ end # Worker
46
+ end # Delayed
@@ -0,0 +1,7 @@
1
+ module Delayed
2
+ class Job
3
+ def self.enqueue(obj, *args)
4
+ obj.perform
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markbates-delayed_job_extras
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1.20090805150516
5
+ platform: ruby
6
+ authors:
7
+ - markbates
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-05 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Adds support for Hoptoad and is_paranoid to Delayed::Job. Additionally it also adds 'stats' for your workers, and it even includes a base worker that encapsulates a some common functionality.
17
+ email: ""
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - LICENSE
25
+ files:
26
+ - lib/delayed_job_extras/job.rb
27
+ - lib/delayed_job_extras/performable_method.rb
28
+ - lib/delayed_job_extras/worker.rb
29
+ - lib/delayed_job_extras.rb
30
+ - lib/delayed_job_test_enhancements.rb
31
+ - README
32
+ - LICENSE
33
+ - generators/dj_extras_generator.rb
34
+ - generators/templates/migration.rb
35
+ has_rdoc: false
36
+ homepage: ""
37
+ licenses:
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project: magrathea
58
+ rubygems_version: 1.3.5
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: delayed_job_extras
62
+ test_files: []
63
+