Class: Raif::Evals::WorkerPool::State

Inherits:
Object
  • Object
show all
Defined in:
lib/raif/evals/worker_pool.rb

Overview

The pool's shared mutable state, behind one mutex.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items:) ⇒ State

Returns a new instance of State.



76
77
78
79
80
81
82
83
# File 'lib/raif/evals/worker_pool.rb', line 76

def initialize(items:)
  @items = items
  @results = Array.new(items.size)
  @next_index = 0
  @stopped = false
  @failure = nil
  @mutex = Mutex.new
end

Instance Attribute Details

#failureObject (readonly)

Returns the value of attribute failure.



74
75
76
# File 'lib/raif/evals/worker_pool.rb', line 74

def failure
  @failure
end

#itemsObject (readonly)

Returns the value of attribute items.



74
75
76
# File 'lib/raif/evals/worker_pool.rb', line 74

def items
  @items
end

#resultsObject (readonly)

Returns the value of attribute results.



74
75
76
# File 'lib/raif/evals/worker_pool.rb', line 74

def results
  @results
end

Instance Method Details

#fail!(error) ⇒ Object

First failure wins: the ones that follow are usually the same provider outage seen by the other workers.



102
103
104
105
106
107
# File 'lib/raif/evals/worker_pool.rb', line 102

def fail!(error)
  @mutex.synchronize do
    @stopped = true
    @failure ||= error
  end
end

#stop!Object



96
97
98
# File 'lib/raif/evals/worker_pool.rb', line 96

def stop!
  @mutex.synchronize { @stopped = true }
end

#takeObject

The index of the next item to run, or nil once the list is exhausted or the run is stopping. Deliberately checked between items rather than mid-item: an execution that has already paid for its inference should finish and be recorded.



88
89
90
91
92
93
94
# File 'lib/raif/evals/worker_pool.rb', line 88

def take
  @mutex.synchronize do
    return if @stopped || @next_index >= @items.size

    @next_index.tap { @next_index += 1 }
  end
end