Class: WorkerKiller::RufusScheduler::BaseLimiter

Inherits:
Object
  • Object
show all
Defined in:
lib/worker_killer/rufus_scheduler/base_limiter.rb

Overview

Base class for registration rufus process killer.

Direct Known Subclasses

JobsLimiter, OOMLimiter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scheduler:, limiter:, frequency: 30, timeout: 60, logger: nil, job_filter: nil) ⇒ BaseLimiter

Returns a new instance of BaseLimiter.

Parameters:

  • scheduler (Rufus::Scheduler)
  • limiter (#check, #started_at)
  • frequency (Integer, nil) (defaults to: 30)

    run after each job if frequency is nil

  • timeout (Integer) (defaults to: 60)
  • logger (Logger, nil) (defaults to: nil)
  • job_filter (Proc, nil) (defaults to: nil)

    when frequency=nil, used to select which jobs should be counted



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/worker_killer/rufus_scheduler/base_limiter.rb', line 19

def initialize(scheduler:, limiter:, frequency: 30, timeout: 60, logger: nil, job_filter: nil)
  ::WorkerKiller.configure do |config|
    # Setting up logger for both limiter and killer
    config.logger = logger unless logger.nil?
    # We use wait instead of attempts, because attempts does not work for rufus.
    # rufus can either kill active jobs now or wait N seconds until they finished.
    config.quit_attempts = 1
    config.term_attempts = 0
  end

  @scheduler = scheduler
  @frequency = frequency&.to_i
  @timeout = timeout.to_i
  @killer = ::WorkerKiller::Killer::RufusScheduler.new
  @limiter = limiter
  @job_filter = job_filter
end

Instance Attribute Details

#frequencyObject (readonly)

Returns the value of attribute frequency.



11
12
13
# File 'lib/worker_killer/rufus_scheduler/base_limiter.rb', line 11

def frequency
  @frequency
end

#job_filterObject (readonly)

Returns the value of attribute job_filter.



11
12
13
# File 'lib/worker_killer/rufus_scheduler/base_limiter.rb', line 11

def job_filter
  @job_filter
end

#killerObject (readonly)

Returns the value of attribute killer.



11
12
13
# File 'lib/worker_killer/rufus_scheduler/base_limiter.rb', line 11

def killer
  @killer
end

#limiterObject (readonly)

Returns the value of attribute limiter.



11
12
13
# File 'lib/worker_killer/rufus_scheduler/base_limiter.rb', line 11

def limiter
  @limiter
end

#schedulerObject (readonly)

Returns the value of attribute scheduler.



11
12
13
# File 'lib/worker_killer/rufus_scheduler/base_limiter.rb', line 11

def scheduler
  @scheduler
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



11
12
13
# File 'lib/worker_killer/rufus_scheduler/base_limiter.rb', line 11

def timeout
  @timeout
end

Class Method Details

.register(**kwargs) ⇒ Object



7
8
9
# File 'lib/worker_killer/rufus_scheduler/base_limiter.rb', line 7

def self.register(**kwargs)
  new(**kwargs).register
end

Instance Method Details

#check_after_job?(job, trigger_time) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/worker_killer/rufus_scheduler/base_limiter.rb', line 49

def check_after_job?(job, trigger_time)
  return true if job_filter.nil?

  job_filter.call(job, trigger_time)
end

#registerObject



37
38
39
40
41
42
43
# File 'lib/worker_killer/rufus_scheduler/base_limiter.rb', line 37

def register
  if frequency
    register_every_job
  else
    register_post_run_job
  end
end

#run_checkObject



45
46
47
# File 'lib/worker_killer/rufus_scheduler/base_limiter.rb', line 45

def run_check
  killer.kill(limiter.started_at, scheduler: scheduler, timeout: timeout) if limiter.check
end