Class: Nitra::Workers::Worker

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

Direct Known Subclasses

Cucumber, Rspec

Constant Summary collapse

@@worker_classes =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner_id, worker_number, configuration) ⇒ Worker

Returns a new instance of Worker.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nitra/worker.rb', line 30

def initialize(runner_id, worker_number, configuration)
  @runner_id = runner_id
  @worker_number = worker_number
  @configuration = configuration
  @forked_worker_pid = nil

  ENV["TEST_ENV_NUMBER"] = worker_number.to_s

  # Frameworks don't like it when you change the IO between invocations.
  # So we make one object and flush it after every invocation.
  @io = StringIO.new
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



28
29
30
# File 'lib/nitra/worker.rb', line 28

def channel
  @channel
end

#configurationObject (readonly)

Returns the value of attribute configuration.



28
29
30
# File 'lib/nitra/worker.rb', line 28

def configuration
  @configuration
end

#ioObject (readonly)

Returns the value of attribute io.



28
29
30
# File 'lib/nitra/worker.rb', line 28

def io
  @io
end

#runner_idObject (readonly)

Returns the value of attribute runner_id.



28
29
30
# File 'lib/nitra/worker.rb', line 28

def runner_id
  @runner_id
end

#worker_numberObject (readonly)

Returns the value of attribute worker_number.



28
29
30
# File 'lib/nitra/worker.rb', line 28

def worker_number
  @worker_number
end

Class Method Details

.framework_nameObject

Return the framework name of this worker



22
23
24
# File 'lib/nitra/worker.rb', line 22

def framework_name
  self.name.split("::").last.downcase
end

.inherited(klass) ⇒ Object



11
12
13
# File 'lib/nitra/worker.rb', line 11

def inherited(klass)
  @@worker_classes[klass.framework_name] = klass
end

.worker_classesObject



15
16
17
# File 'lib/nitra/worker.rb', line 15

def worker_classes
  @@worker_classes
end

Instance Method Details

#fork_and_runObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nitra/worker.rb', line 44

def fork_and_run
  client, server = Nitra::Channel.pipe

  pid = fork do
    # This is important. We don't want anything bubbling up to the master that we didn't send there.
    # We reopen later to get the output from the framework run.
    $stdout.reopen('/dev/null', 'a')
    $stderr.reopen('/dev/null', 'a')

    trap("USR1") { interrupt_forked_worker_and_exit }

    server.close
    @channel = client
    begin
      run
    rescue => e
      channel.write("command" => "error", "process" => "init framework", "text" => e.message, "worker_number" => worker_number)
    end
  end

  client.close

  [pid, server]
end