Class: Paraspec::Supervisor
- Inherits:
-
Object
- Object
- Paraspec::Supervisor
- 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
Instance Method Summary collapse
- #ident ⇒ Object
-
#initialize(options = {}) ⇒ Supervisor
constructor
A new instance of Supervisor.
- #run ⇒ Object
- #run_supervisor ⇒ Object
- #wait_for_process(pid, process_name, exception_class) ⇒ Object
Methods included from ProcessHelpers
Methods included from DrbHelpers
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(={}) @original_process_title = $0 $0 = "#{@original_process_title} [supervisor]" Paraspec.logger.ident = '[s]' @concurrency = [:concurrency] || 1 @terminal = [:terminal] @options = end |
Instance Method Details
#ident ⇒ Object
132 133 134 |
# File 'lib/paraspec/supervisor.rb', line 132 def ident "[s]" end |
#run ⇒ Object
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 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/paraspec/supervisor.rb', line 19 def run unless @terminal Process.setpgrp end at_exit do # It seems that when the tests are run in Travis, killing # paraspec vaporizes the standard output... flush the # standard output streams here to work around. STDERR.flush STDOUT.flush 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 Paraspec::Ipc.pick_master_app_port rd, wr = IO.pipe if @master_pid = fork # parent - supervisor 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_supervisor ⇒ Object
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 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/paraspec/supervisor.rb', line 74 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, "Worker #{i+1}", WorkerFailed) 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, 'Master', MasterFailed) exit status end |
#wait_for_process(pid, process_name, exception_class) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/paraspec/supervisor.rb', line 125 def wait_for_process(pid, process_name, exception_class) pid, status = Process.wait2(pid) if status.exitstatus != 0 raise exception_class, "#{process_name} exited with status #{status.exitstatus}" end end |