Class: Binpacker::Worker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Worker.

Parameters:

  • (Object)
  • (Object)
  • passthrough: (Object) (defaults to: [])
  • quiet: (Object) (defaults to: false)


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

def initialize(id, runner_class, passthrough: [], quiet: false)
  @id = id
  @runner_class = runner_class
  @passthrough = passthrough
  @quiet = quiet
  @status = :created
  @timings = []
  @exit_code = 0
  @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

#status:crashed, ... (readonly)

Returns the value of attribute status.

Returns:

  • (:crashed, :created, :error, :finished, :ready, :running)


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

def status
  @status
end

Instance Method Details

#batch_donenil

Returns:

  • (nil)


64
65
66
# File 'lib/binpacker/worker.rb', line 64

def batch_done
  @stdin_w.puts JSON.generate({ type: "done" })
end

#cleanupObject



154
155
156
157
158
159
# File 'lib/binpacker/worker.rb', line 154

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

#collect_results:finished

Returns:

  • (:finished)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/binpacker/worker.rb', line 112

def collect_results
  @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
      break
    when "batch_result"
      @exit_code = data["passed"] ? 0 : 1
      @passed = data["passed"]
      @example_count = data["total"] || 0
      @passed_count = data["passed_count"] || 0
      break
    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) ⇒ nil

Parameters:

  • (Object)

Returns:

  • (nil)


60
61
62
# File 'lib/binpacker/worker.rb', line 60

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

#send_tests(tests) ⇒ Object



56
57
58
# File 'lib/binpacker/worker.rb', line 56

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

#signal_doneObject



68
69
70
71
# File 'lib/binpacker/worker.rb', line 68

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

#startBinpacker::Worker

Returns:



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
53
54
# File 'lib/binpacker/worker.rb', line 21

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,
    "--id", @id.to_s,
    *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 unless @quiet }
  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)


150
151
152
# File 'lib/binpacker/worker.rb', line 150

def success?
  @exit_code == 0
end

#timingsArray[untyped]

Returns:

  • (Array[untyped])


146
147
148
# File 'lib/binpacker/worker.rb', line 146

def timings
  @timings
end

#wait_for_batchnil, ...

Returns:

  • (nil, { timings: [], exit_code: 0 | 1, examples: untyped, passed: untyped }, { timings: [], exit_code: untyped, examples: untyped, passed: untyped })


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/binpacker/worker.rb', line 73

def wait_for_batch
  deadline = Time.now + 300
  while Time.now < deadline
    begin
      line = read_line(timeout: 1)
      return nil unless line
      next unless line.strip.start_with?("{")
      data = JSON.parse(line.strip)

      if data["type"] == "timing"
        @timings << { file: data["file"], name: data["name"], time: data["time"] }
      elsif data["type"] == "batch_result"
        @exit_code = data["passed"] ? 0 : 1
        @example_count += data["total"] || 0
        @passed_count += data["passed_count"] || 0
        return {
          timings: [],
          exit_code: @exit_code,
          examples: data["total"] || 0,
          passed: data["passed_count"] || 0
        }
      elsif data["type"] == "result"
        @exit_code = data["exit_code"]
        @passed = data["passed"]
        @example_count = data["total"] || 0
        @passed_count = data["passed_count"] || 0
        return {
          timings: [],
          exit_code: @exit_code,
          examples: @example_count,
          passed: @passed_count
        }
      end
    rescue JSON::ParserError
    end
  end
  nil
end