Browse files

Add optional limiting to AR backend

Make mongoid behave as normal when asked to limit (as compared to blowing up) until someone feels like implementing it there too (didn't have it installed so couldn't verify the tests pass, sorry!)
  • Loading branch information...
1 parent 284c033 commit cf747cfc510cf616925eac69d3fcad242708f20a John Wilkinson committed Jul 24, 2013
Showing with 30 additions and 8 deletions.
  1. +5 −5 app/models/dj_mon/dj_report.rb
  2. +2 −2 lib/dj_mon/backend.rb
  3. +19 −1 lib/dj_mon/backend/activerecord.rb
  4. +4 −0 lib/dj_mon/backend/mongoid.rb
View
10 app/models/dj_mon/dj_report.rb
@@ -9,7 +9,7 @@ def initialize delayed_job
end
def as_json(options={})
- {
+ {
:id => delayed_job.id,
:payload => payload(delayed_job),
:priority => delayed_job.priority,
@@ -34,19 +34,19 @@ def reports_for jobs
end
def all_reports
- reports_for DjMon::Backend.all
+ reports_for DjMon::Backend.limited.all
end
def failed_reports
- reports_for DjMon::Backend.failed
+ reports_for DjMon::Backend.limited.failed
end
def active_reports
- reports_for DjMon::Backend.active
+ reports_for DjMon::Backend.limited.active
end
def queued_reports
- reports_for DjMon::Backend.queued
+ reports_for DjMon::Backend.limited.queued
end
def dj_counts
View
4 lib/dj_mon/backend.rb
@@ -1,6 +1,6 @@
module DjMon
module Backend
- BACKEND_METHODS = [:all, :failed, :active, :queued, :destroy, :retry]
+ BACKEND_METHODS = [:all, :failed, :active, :queued, :destroy, :retry, :limited]
class << self
def used_backend
@@ -14,7 +14,7 @@ def used_backend
raise "DjMon has no backend for '#{delayed_job_backend}'"
end
end
-
+
BACKEND_METHODS << {:to => :used_backend}
delegate *BACKEND_METHODS
end
View
20 lib/dj_mon/backend/activerecord.rb
@@ -1,9 +1,27 @@
module DjMon
module Backend
module ActiveRecord
+ module LimitedProxy
+ class << self
+ def method_missing(method, *args, &block)
+ scope = ::DjMon::Backend::ActiveRecord.send(method, *args, &block)
+ limit = Rails.configuration.dj_mon.results_limit
+ limit.present? ? scope.order('id DESC').limit(limit) : scope
+ end
+
+ def respond_to?(method)
+ super || ::DjMon::Backend::ActiveRecord.respond_to?(method)
+ end
+ end
+ end
+
class << self
+ def limited
+ LimitedProxy
+ end
+
def all
- Delayed::Job.all
+ Delayed::Job.scoped
end
def failed
View
4 lib/dj_mon/backend/mongoid.rb
@@ -2,6 +2,10 @@ module DjMon
module Backend
module Mongoid
class << self
+ def limited
+ self #TODO: Implement me! See activerecord.rb
+ end
+
def all
Delayed::Job.all
end

0 comments on commit cf747cf

Please sign in to comment.