Class: Binpacker::Worker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, runner_class, passthrough: []) ⇒ Worker

Returns a new instance of Worker.



9
10
11
12
13
14
15
16
17
18
# File 'lib/binpacker/worker.rb', line 9

def initialize(id, runner_class, passthrough: [])
  @id = id
  @runner_class = runner_class
  @passthrough = passthrough
  @status = :created
  @timings = []
  @exit_code = nil
  @example_count = 0
  @passed_count = 0
end

Instance Attribute Details

#example_countObject (readonly)

Returns the value of attribute example_count.



7
8
9
# File 'lib/binpacker/worker.rb', line 7

def example_count
  @example_count
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/binpacker/worker.rb', line 7

def id
  @id
end

#passed_countObject (readonly)

Returns the value of attribute passed_count.



7
8
9
# File 'lib/binpacker/worker.rb', line 7

def passed_count
  @passed_count
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/binpacker/worker.rb', line 7

def status
  @status
end

Instance Method Details

#cleanupObject



99
100
101
102
103
104
# File 'lib/binpacker/worker.rb', line 99

def cleanup
  kill! if @status == :running || @status == :ready
  [@stdin_w, @stdout_r, @stderr_r].each { |io| io&.close unless io&.closed? }
  @stderr_thread&.kill
rescue IOError
end

#finishObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/binpacker/worker.rb', line 62

def finish
  @stdin_w.puts JSON.generate({ type: "done" })
  @stdin_w.close

  @status = :running
  @stdout_r.each_line do |line|
    data = JSON.parse(line.strip)
    case data["type"]
    when "timing"
      @timings << { file: data["file"], name: data["name"], time: data["time"] }
    when "result"
      @exit_code = data["exit_code"]
      @passed = data["passed"]
      @example_count = data["total"] || 0
      @passed_count = data["passed_count"] || 0
    when "output"
      $stdout.write data["text"] if data["text"]
    end
  rescue JSON::ParserError
    $stdout.write line
  end

  Process.wait(@pid)
  @status = :finished
rescue Errno::ECHILD
  @status = :crashed
  @passed = false
end

#send_test(test) ⇒ Object



58
59
60
# File 'lib/binpacker/worker.rb', line 58

def send_test(test)
  @stdin_w.puts JSON.generate({ file: test.file, name: test.name })
end

#send_tests(tests) ⇒ Object



54
55
56
# File 'lib/binpacker/worker.rb', line 54

def send_tests(tests)
  tests.each { |t| @stdin_w.puts JSON.generate({ file: t.file, name: t.name }) }
end

#startObject



20
21
22
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
48
49
50
51
52
# File 'lib/binpacker/worker.rb', line 20

def start
  worker_script = File.expand_path("../../exe/binpacker-worker", __dir__)

  @stdin_r, @stdin_w = IO.pipe(encoding: "UTF-8")
  @stdout_r, @stdout_w = IO.pipe(encoding: "UTF-8")
  @stderr_r, @stderr_w = IO.pipe(encoding: "UTF-8")

  @pid = Process.spawn(
    RbConfig.ruby, worker_script,
    "--runner", @runner_class.runner_name,
    *passthrough_args,
    in: @stdin_r, out: @stdout_w, err: @stderr_w,
    close_others: true
  )

  @stdin_r.close; @stdout_w.close; @stderr_w.close

  @stderr_thread = Thread.new do
    @stderr_r.each_line { |line| $stderr.write line }
  end

  ready_line = read_line(timeout: 30)
  if ready_line
    data = JSON.parse(ready_line.strip)
    @status = :ready if data["type"] == "ready"
  end

  raise WorkerError, "worker #{@id} failed to start" unless @status == :ready
  self
rescue JSON::ParserError
  @status = :error
  raise WorkerError, "worker #{@id} sent invalid ready signal"
end

#success?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/binpacker/worker.rb', line 95

def success?
  @exit_code == 0
end

#timingsObject



91
92
93
# File 'lib/binpacker/worker.rb', line 91

def timings
  @timings
end