Class: Flatware::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/flatware/worker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, runner, dispatch_endpoint, sink_endpoint) ⇒ Worker

Returns a new instance of Worker.



6
7
8
9
10
11
# File 'lib/flatware/worker.rb', line 6

def initialize(id, runner, dispatch_endpoint, sink_endpoint)
  @id       = id
  @runner   = runner
  @sink     = Sink::Client.new sink_endpoint
  @task     = Flatware.socket ZMQ::REQ, connect: dispatch_endpoint
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/flatware/worker.rb', line 4

def id
  @id
end

Class Method Details

.spawn(count:, runner:, dispatch:, sink:) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/flatware/worker.rb', line 13

def self.spawn(count:, runner:, dispatch:, sink:)
  count.times do |i|
    fork do
      $0 = "flatware worker #{i}"
      ENV['TEST_ENV_NUMBER'] = i.to_s
      new(i, runner, dispatch, sink).listen
    end
  end
end

Instance Method Details

#listenObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/flatware/worker.rb', line 23

def listen
  trap 'INT' do
    Flatware.close!
    @want_to_quit = true
    exit(1)
  end

  Sink.client = sink
  report_for_duty
  loop do
    job = task.recv
    break if job == 'seppuku' or @want_to_quit
    job.worker = id
    sink.started job
    begin
      runner.run job.id, job.args
    rescue => e
      Flatware.log e
      job.failed = true
    end
    sink.finished job
    report_for_duty
  end
  Flatware.close unless @want_to_quit
end