Class: Specwrk::CLI::Start

Inherits:
Dry::CLI::Command
  • Object
show all
Includes:
Clientable, Servable, Workable
Defined in:
lib/specwrk/cli.rb

Constant Summary collapse

SEED_INIT_SCRIPT =
<<~'RUBY'
  require "json"
  require "specwrk/list_examples"
  require "specwrk/client"

  def status(msg)
    print "\e[2K\r#{msg}"
    $stdout.flush
  end

  dir = JSON.parse(ENV.fetch("SPECWRK_SEED_DIRS"))
  max_retries = Integer(ENV.fetch("SPECWRK_MAX_RETRIES", "0"))

  examples = Specwrk::ListExamples.new(dir).examples

  status "Waiting for server to respond..."
  Specwrk::Client.wait_for_server!
  status "Server responding ✓"
  status "Seeding #{examples.length} examples..."
  Specwrk::Client.new.seed(examples, max_retries)
  file_count = examples.group_by { |e| e[:file_path] }.keys.size
  status "🌱 Seeded #{examples.size} examples across #{file_count} files"
  exit(1) if examples.size.zero?
RUBY

Constants included from WorkerProcesses

WorkerProcesses::WORKER_INIT_SCRIPT

Instance Method Summary collapse

Methods included from Hookable

extended, #included, #included_hooks, #on_included, #on_setup, #setup_hooks

Methods included from PortDiscoverable

#find_open_port

Methods included from WorkerProcesses

#drain_outputs, #start_workers, #worker_count, #worker_env_for

Instance Method Details

#call(max_retries:, dir:, **args) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/specwrk/cli.rb', line 276

def call(max_retries:, dir:, **args)
  dir = ["spec"] if dir.length.zero?

  self.class.setup(**args)
  $stdout.sync = true

  # nil this env var if it exists to prevent never-ending workers
  ENV["SPECWRK_SRV_URI"] = nil

  # Start on a random open port to not conflict with another server
  ENV["SPECWRK_SRV_PORT"] = find_open_port.to_s
  ENV["SPECWRK_SRV_URI"] = "http://localhost:#{ENV.fetch("SPECWRK_SRV_PORT", "5138")}"

  web_pid = Process.fork do
    require "specwrk/web"
    require "specwrk/web/app"

    ENV["SPECWRK_FORKED"] = "1"
    ENV["SPECWRK_SRV_SINGLE_RUN"] = "1"
    status "Starting queue server..."
    Specwrk::Web::App.run!
  end

  return if Specwrk.force_quit
  seed_pid = spawn_seed_process(dir, max_retries)

  if Specwrk.wait_for_pids_exit([seed_pid]).value?(1)
    status "Seeding examples failed, exiting."
    Process.kill("INT", web_pid)
    exit(1)
  end

  return if Specwrk.force_quit
  status "Starting #{worker_count} workers..."
  start_workers

  status "#{worker_count} workers started ✓\n"
  Specwrk.wait_for_pids_exit(@worker_pids)

  drain_outputs
  return if Specwrk.force_quit

  require "specwrk/cli_reporter"
  status = Specwrk::CLIReporter.new.report

  Specwrk.wait_for_pids_exit([web_pid, seed_pid])
  exit(status)
end

#spawn_seed_process(dir, max_retries) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/specwrk/cli.rb', line 325

def spawn_seed_process(dir, max_retries)
  Process.spawn(
    {
      "SPECWRK_FORKED" => "1",
      "SPECWRK_SEED" => "1",
      "SPECWRK_SEED_DIRS" => JSON.dump(dir),
      "SPECWRK_MAX_RETRIES" => max_retries.to_s
    },
    RbConfig.ruby, "-e", SEED_INIT_SCRIPT,
    close_others: false
  )
end

#status(msg) ⇒ Object



338
339
340
341
# File 'lib/specwrk/cli.rb', line 338

def status(msg)
  print "\e[2K\r#{msg}"
  $stdout.flush
end