Class: Oxidized::Jobs

Inherits:
Array
  • Object
show all
Defined in:
lib/oxidized/jobs.rb

Constant Summary collapse

AVERAGE_DURATION =

initially presume nodes take 5s to complete

5
MAX_INTER_JOB_GAP =

add job if more than X from last job started

300

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max, interval, nodes) ⇒ Jobs

Returns a new instance of Jobs.



7
8
9
10
11
12
13
14
15
# File 'lib/oxidized/jobs.rb', line 7

def initialize max, interval, nodes
  @max       = max
  @interval  = interval
  @nodes     = nodes
  @last      = Time.now.utc
  @durations = Array.new @nodes.size, AVERAGE_DURATION
  duration AVERAGE_DURATION
  super()
end

Instance Attribute Details

#intervalObject

Returns the value of attribute interval.



5
6
7
# File 'lib/oxidized/jobs.rb', line 5

def interval
  @interval
end

#maxObject

Returns the value of attribute max.



5
6
7
# File 'lib/oxidized/jobs.rb', line 5

def max
  @max
end

#wantObject

Returns the value of attribute want.



5
6
7
# File 'lib/oxidized/jobs.rb', line 5

def want
  @want
end

Instance Method Details

#duration(last) ⇒ Object



22
23
24
25
26
# File 'lib/oxidized/jobs.rb', line 22

def duration last
  @durations.push(last).shift
  @duration = @durations.inject(:+).to_f / @nodes.size #rolling average
  new_count
end

#new_countObject



28
29
30
31
32
33
# File 'lib/oxidized/jobs.rb', line 28

def new_count
  @want = ((@nodes.size * @duration) / @interval).to_i
  @want = 1 if @want < 1
  @want = @nodes.size if @want > @nodes.size
  @want = @max if @want > @max
end

#push(arg) ⇒ Object



17
18
19
20
# File 'lib/oxidized/jobs.rb', line 17

def push arg
  @last = Time.now.utc
  super
end

#workObject



35
36
37
38
39
40
41
42
43
# File 'lib/oxidized/jobs.rb', line 35

def work
  # if   a) we want less or same amount of threads as we now running
  # and  b) we want less threads running than the total amount of nodes
  # and  c) there is more than MAX_INTER_JOB_GAP since last one was started
  # then we want one more thread (rationale is to fix hanging thread causing HOLB)
  if @want <= size and @want < @nodes.size
    @want +=1 if (Time.now.utc - @last) > MAX_INTER_JOB_GAP
  end
end