class Flatware::Worker

executes tests and sends results to the sink

Attributes

id[R]
runner[R]
sink[R]

Public Class Methods

new(id, runner, sink_endpoint) click to toggle source
# File lib/flatware/worker.rb, line 11
def initialize(id, runner, sink_endpoint)
  @id       = id
  @runner   = runner
  @sink     = DRbObject.new_with_uri sink_endpoint
  Flatware::Sink.client = @sink
end
spawn(count:, runner:, sink:, **) click to toggle source
# File lib/flatware/worker.rb, line 18
def self.spawn(count:, runner:, sink:, **)
  Flatware.configuration.before_fork.call
  count.times do |i|
    fork do
      $0 = "flatware worker #{i}"
      ENV['TEST_ENV_NUMBER'] = i.to_s
      Flatware.configuration.after_fork.call(i)
      new(i, runner, sink).listen
    end
  end
end

Public Instance Methods

listen() click to toggle source
# File lib/flatware/worker.rb, line 30
def listen
  retrying(times: 10, wait: 0.1) do
    job = sink.ready id
    until want_to_quit? || job.sentinel?
      job.worker = id
      sink.started job
      run job
      job = sink.ready id
    end
  end
end

Private Instance Methods

retrying(times:, wait:) { || ... } click to toggle source
# File lib/flatware/worker.rb, line 63
def retrying(times:, wait:)
  tries = 0
  begin
    yield unless want_to_quit?
  rescue DRb::DRbConnError => e
    raise if (tries += 1) >= times

    sleep wait
    Flatware.log('retrying', e.message)
    retry
  end
end
run(job) click to toggle source
# File lib/flatware/worker.rb, line 44
def run(job)
  runner.run job.id, job.args
  sink.finished job
rescue Interrupt
  want_to_quit!
rescue StandardError => e
  Flatware.log e
  job.failed!
  sink.finished job
end
want_to_quit!() click to toggle source
# File lib/flatware/worker.rb, line 55
def want_to_quit!
  @want_to_quit = true
end
want_to_quit?() click to toggle source
# File lib/flatware/worker.rb, line 59
def want_to_quit?
  @want_to_quit == true
end