Class: Paraspec::Supervisor

Inherits:
Object
  • Object
show all
Includes:
DrbHelpers, ProcessHelpers
Defined in:
lib/paraspec/supervisor.rb

Overview

Supervisor is the process that spawns all other processes. Its primary responsibility is to be a “clean slate”, specifically the supervisor should not ever have any of the tests loaded in its address space.

Constant Summary

Constants included from DrbHelpers

DrbHelpers::WAIT_TIME

Instance Method Summary collapse

Methods included from ProcessHelpers

#kill_child_processes

Methods included from DrbHelpers

#master_client

Constructor Details

#initialize(options = {}) ⇒ Supervisor

Returns a new instance of Supervisor.



10
11
12
13
14
15
16
17
# File 'lib/paraspec/supervisor.rb', line 10

def initialize(options={})
  @original_process_title = $0
  $0 = "#{@original_process_title} [supervisor]"
  Paraspec.logger.ident = '[s]'
  @concurrency = options[:concurrency] || 1
  @terminal = options[:terminal]
  @options = options
end

Instance Method Details

#identObject



123
124
125
# File 'lib/paraspec/supervisor.rb', line 123

def ident
  "[s]"
end

#runObject



19
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
53
54
55
56
57
58
59
60
61
62
# File 'lib/paraspec/supervisor.rb', line 19

def run
  unless @terminal
    Process.setpgrp
  end

  supervisor_pid = $$
  at_exit do
    # We fork, therefore this handler will be run in master and
    # workers as well but it should only run in supervisor.
    # Guard accordingly
    if $$ == supervisor_pid
      # first kill workers, then master
      ((@worker_pids || []) + [@master_pid]).compact.each do |pid|
        begin
          Process.kill('TERM', pid)
        rescue SystemCallError
        end
      end
      # then kill our process group
      unless @terminal
        kill_child_processes
      end
    end
  end

  rd, wr = IO.pipe
  if @master_pid = fork
    # parent
    wr.close
    @master_pipe = rd
    run_supervisor
  else
    # child - master
    $0 = "#{@original_process_title} [master]"
    if @options[:master_is_1]
      ENV['TEST_ENV_NUMBER'] = '1'
    end
    Paraspec.logger.ident = '[m]'
    rd.close
    master = Master.new(:supervisor_pipe => wr)
    master.run
    exit(0)
  end
end

#run_supervisorObject



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/paraspec/supervisor.rb', line 64

def run_supervisor
  start_time = Time.now

  if master_client.request('non-example-exception-count').to_i == 0
    master_client.request('suite-started')

    @worker_pipes = []
    @worker_pids = []

    1.upto(@concurrency) do |i|
      rd, wr = IO.pipe
      if worker_pid = fork
        # parent
        wr.close
        @worker_pipes << rd
        @worker_pids << worker_pid
      else
        # child - worker
        $0 = "#{@original_process_title} [worker-#{i}]"
        Paraspec.logger.ident = "[w#{i}]"
        rd.close
        if RSpec.world.example_groups.count > 0
          raise 'Example groups loaded too early/spilled across processes'
        end
        Worker.new(:number => i, :supervisor_pipe => wr).run
        exit(0)
      end
    end

    Paraspec.logger.debug_state("Waiting for workers")
    @worker_pids.each_with_index do |pid, i|
      Paraspec.logger.debug_state("Waiting for worker #{i+1} at #{pid}")
      wait_for_process(pid)
    end
    status = 0
  else
    status = 1
  end

  master_client.reconnect!
  puts "dumping summary"
  master_client.request('dump-summary')
  if status == 0
    status = master_client.request('status')
  end
  Paraspec.logger.debug_state("Asking master to stop")
  master_client.request('stop')
  wait_for_process(@master_pid)
  exit status
end

#wait_for_process(pid) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/paraspec/supervisor.rb', line 115

def wait_for_process(pid)
  begin
    Process.wait(pid)
  rescue Errno::ECHILD
    # already dead
  end
end