Class: Resque::Plugins::ResqueCleaner::Limiter

Inherits:
Object
  • Object
show all
Defined in:
lib/resque_cleaner.rb

Overview

Through the Limiter class, you accesses only the last x(default 1000) jobs.

Constant Summary collapse

DEFAULT_MAX_JOBS =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cleaner) ⇒ Limiter

Returns a new instance of Limiter.



193
194
195
196
197
# File 'lib/resque_cleaner.rb', line 193

def initialize(cleaner)
  @cleaner = cleaner
  @maximum = DEFAULT_MAX_JOBS
  @locked = false
end

Instance Attribute Details

#maximumObject

Returns the value of attribute maximum.



192
193
194
# File 'lib/resque_cleaner.rb', line 192

def maximum
  @maximum
end

Instance Method Details

#all(index = 0, count = 1) ⇒ Object

Wraps Resque’s all and returns always array.



225
226
227
228
229
230
231
# File 'lib/resque_cleaner.rb', line 225

def all(index=0,count=1)
  jobs = @cleaner.failure.all( index, count)
  jobs = [] unless jobs
  jobs = [jobs] unless jobs.is_a?(Array)
  jobs.each{|j| j.extend FailedJobEx}
  jobs
end

#countObject

Returns limited count.



206
207
208
209
210
211
212
# File 'lib/resque_cleaner.rb', line 206

def count
  if @locked
    @jobs.size
  else
    on? ? @maximum : @cleaner.failure.count
  end
end

#jobsObject

Returns jobs. If numbers of jobs is more than maixum, it returns only the maximum.



216
217
218
219
220
221
222
# File 'lib/resque_cleaner.rb', line 216

def jobs
  if @locked
    @jobs
  else
    all( - count, count)
  end
end

#lockObject

Assuming new failures pushed while cleaner is dealing with failures, you need to lock the range.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/resque_cleaner.rb', line 244

def lock
  old = @locked

  unless @locked
    total_count = @cleaner.failure.count
    if total_count>@maximum
      @start_index = total_count-@maximum
      @jobs = all( @start_index, @maximum)
    else
      @start_index = 0
      @jobs = all( 0, total_count)
    end
  end

  @locked = true
  yield
ensure
  @locked = old
end

#on?Boolean

Returns true if limiter is ON: number of failed jobs is more than maximum value.

Returns:

  • (Boolean)


201
202
203
# File 'lib/resque_cleaner.rb', line 201

def on?
  @cleaner.failure.count > @maximum
end

#start_indexObject

Returns a start index of jobs in :failed list.



234
235
236
237
238
239
240
# File 'lib/resque_cleaner.rb', line 234

def start_index
  if @locked
    @start_index
  else
    on? ? @cleaner.failure.count-@maximum : 0
  end
end