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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cleaner) ⇒ Limiter

Returns a new instance of Limiter.



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

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

Instance Attribute Details

#maximumObject

Returns the value of attribute maximum.



216
217
218
# File 'lib/resque_cleaner.rb', line 216

def maximum
  @maximum
end

Class Method Details

.default_maximumObject



207
208
209
# File 'lib/resque_cleaner.rb', line 207

def default_maximum
  @@default_maximum
end

.default_maximum=(v) ⇒ Object



211
212
213
# File 'lib/resque_cleaner.rb', line 211

def default_maximum=(v)
  @@default_maximum = v
end

Instance Method Details

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

Wraps Resque’s all and returns always array.



249
250
251
252
253
254
255
# File 'lib/resque_cleaner.rb', line 249

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.



230
231
232
233
234
235
236
# File 'lib/resque_cleaner.rb', line 230

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

#jobsObject

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



240
241
242
243
244
245
246
# File 'lib/resque_cleaner.rb', line 240

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.



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/resque_cleaner.rb', line 268

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)


225
226
227
# File 'lib/resque_cleaner.rb', line 225

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

#start_indexObject

Returns a start index of jobs in :failed list.



258
259
260
261
262
263
264
# File 'lib/resque_cleaner.rb', line 258

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