Module: Specwrk::CLI::WorkerProcesses

Included in:
Watch, Workable
Defined in:
lib/specwrk/cli.rb

Constant Summary collapse

WORKER_INIT_SCRIPT =
"require \"specwrk\"\nSpecwrk::Hooks.load_file(ENV[\"SPECWRK_HOOKS_FILE\"])\nSpecwrk::Hooks.run(:after_worker_fork)\n\nwriter = IO.for_fd(Integer(ENV.fetch(\"SPECWRK_FINAL_FD\")))\n$final_output = writer # standard:disable Style/GlobalVars\n$final_output.sync = true # standard:disable Style/GlobalVars\n$stdout.sync = true\n$stderr.sync = true\n\nrequire \"specwrk/worker\"\n\ntrap(\"INT\") do\n  RSpec.world.wants_to_quit = true if defined?(RSpec)\n  exit(1) if Specwrk.force_quit\n  Specwrk.force_quit = true\nend\n\nstatus = Specwrk::Worker.run!\nSpecwrk::Hooks.run(:before_worker_exit, status)\n$final_output.close # standard:disable Style/GlobalVars\nexit(status)\n"

Instance Method Summary collapse

Instance Method Details

#drain_outputsObject



77
78
79
80
81
82
# File 'lib/specwrk/cli.rb', line 77

def drain_outputs
  @final_outputs.each do |reader|
    reader.each_line { |line| $stdout.print line }
    reader.close
  end
end

#start_workersObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/specwrk/cli.rb', line 57

def start_workers
  @final_outputs = []
  @worker_pids = worker_count.times.map do |i|
    reader, writer = IO.pipe
    @final_outputs << reader

    env = worker_env_for(i + 1).merge(
      "SPECWRK_FINAL_FD" => writer.fileno.to_s
    )

    Hooks.run(:before_worker_fork)
    Process.spawn(
      env, RbConfig.ruby, "-e", WORKER_INIT_SCRIPT,
      writer.fileno => writer,
      :in => :close,
      :close_others => false
    ).tap { writer.close }
  end
end

#worker_countObject



84
85
86
# File 'lib/specwrk/cli.rb', line 84

def worker_count
  @worker_count ||= [1, ENV["SPECWRK_COUNT"].to_i].max
end

#worker_env_for(idx) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/specwrk/cli.rb', line 88

def worker_env_for(idx)
  {
    "TEST_ENV_NUMBER" => (idx == 1) ? "" : idx.to_s,
    "SPECWRK_FORKED" => idx.to_s,
    "SPECWRK_ID" => "#{ENV.fetch("SPECWRK_ID", "specwrk-worker")}-#{idx}"
  }
end