Class: Specwrk::CLI::Start

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

Constant Summary collapse

SEED_INIT_SCRIPT =
"require \"json\"\nrequire \"specwrk\"\nSpecwrk::Hooks.load_file(ENV[\"SPECWRK_HOOKS_FILE\"])\nrequire \"specwrk/list_examples\"\nrequire \"specwrk/client\"\n\ndef status(msg)\n  print \"\\e[2K\\r\#{msg}\"\n  $stdout.flush\nend\n\ndir = JSON.parse(ENV.fetch(\"SPECWRK_SEED_DIRS\"))\nmax_retries = Integer(ENV.fetch(\"SPECWRK_MAX_RETRIES\", \"0\"))\n\nexamples = Specwrk::ListExamples.new(dir).examples\n\nstatus \"Waiting for server to respond...\"\nSpecwrk::Client.wait_for_server!\nstatus \"Server responding \u2713\"\nstatus \"Seeding \#{examples.length} examples...\"\nSpecwrk::Client.new.seed(examples, max_retries)\nfile_count = examples.group_by { |e| e[:file_path] }.keys.size\nstatus \"\u{1F331} Seeded \#{examples.size} examples across \#{file_count} files\"\nexit(1) if examples.size.zero?\n"

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



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/specwrk/cli.rb', line 310

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



359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/specwrk/cli.rb', line 359

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



372
373
374
375
# File 'lib/specwrk/cli.rb', line 372

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