Class: ProcessBalancer::Base

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

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker_index, options = {}) ⇒ Base

Returns a new instance of Base.



22
23
24
25
# File 'lib/process_balancer/base.rb', line 22

def initialize(worker_index, options = {})
  @worker_index = worker_index
  @options      = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/process_balancer/base.rb', line 5

def options
  @options
end

#statusObject (readonly)

Returns the value of attribute status.



5
6
7
# File 'lib/process_balancer/base.rb', line 5

def status
  @status
end

#worker_indexObject (readonly)

Returns the value of attribute worker_index.



5
6
7
# File 'lib/process_balancer/base.rb', line 5

def worker_index
  @worker_index
end

Class Method Details

.lock_driver(driver) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/process_balancer/base.rb', line 7

def self.lock_driver(driver)
  if driver.is_a?(Symbol)
    file = "process_balancer/lock/#{driver}"
    driver = driver.to_s
    unless driver !~ /_/ && driver =~ /[A-Z]+.*/
      driver = driver.split('_').map(&:capitalize).join
    end
    require file
    klass = ProcessBalancer::Lock.const_get(driver)
    include klass
  else
    raise ArgumentError, 'Please pass a symbol for the driver to use'
  end
end

Instance Method Details

#after_performObject



62
# File 'lib/process_balancer/base.rb', line 62

def after_perform; end

#before_performObject



60
# File 'lib/process_balancer/base.rb', line 60

def before_perform; end

#job_idObject



56
57
58
# File 'lib/process_balancer/base.rb', line 56

def job_id
  options[:id]
end

#lock_recordsObject

Raises:

  • (NotImplementedError)


64
65
66
# File 'lib/process_balancer/base.rb', line 64

def lock_records
  raise NotImplementedError
end

#performObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/process_balancer/base.rb', line 27

def perform
  before_perform
  worker_lock do |lock|
    @status = nil
    records = lock_records
    lock.extend!
    records&.each do |r|
      process_record(r)
      lock.extend!
    end
    @status
  ensure
    unlock_records
    after_perform
  end
end

#process_record(record) ⇒ Object

Raises:

  • (NotImplementedError)


72
73
74
# File 'lib/process_balancer/base.rb', line 72

def process_record(record)
  raise NotImplementedError
end

#runtime_lock_timeoutObject



52
53
54
# File 'lib/process_balancer/base.rb', line 52

def runtime_lock_timeout
  options[:runtime_lock_timeout] || 30
end

#status_abortObject



44
45
46
# File 'lib/process_balancer/base.rb', line 44

def status_abort
  @status = :abort
end

#status_sleep(duration) ⇒ Object



48
49
50
# File 'lib/process_balancer/base.rb', line 48

def status_sleep(duration)
  @status = [:sleep, duration]
end

#unlock_recordsObject

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/process_balancer/base.rb', line 68

def unlock_records
  raise NotImplementedError
end

#worker_lock(&_block) ⇒ Object

Raises:

  • (NotImplementedError)


76
77
78
# File 'lib/process_balancer/base.rb', line 76

def worker_lock(&_block)
  raise NotImplementedError, 'Specify a locking driver via lock_driver :driver'
end