Class: Specwrk::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/specwrk/worker.rb,
lib/specwrk/worker/executor.rb,
lib/specwrk/worker/null_formatter.rb,
lib/specwrk/worker/progress_formatter.rb,
lib/specwrk/worker/completion_formatter.rb

Defined Under Namespace

Classes: CompletionFormatter, Executor, NullFormatter, ProgressFormatter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWorker

Returns a new instance of Worker.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/specwrk/worker.rb', line 15

def initialize
  Process.setproctitle ENV.fetch("SPECWRK_ID", "specwrk-worker")
  FileUtils.mkdir_p(ENV["SPECWRK_OUT"]) if ENV["SPECWRK_OUT"]

  @running = true
  @client = Client.new
  @executor = Executor.new
  @all_examples_completed = false
  @seed_waits = ENV.fetch("SPECWRK_SEED_WAITS", "10").to_i
  @heartbeat_thread ||= Thread.new do
    thump
  end
end

Class Method Details

.run!Object



11
12
13
# File 'lib/specwrk/worker.rb', line 11

def self.run!
  new.run
end

Instance Method Details

#complete_examplesObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/specwrk/worker.rb', line 87

def complete_examples
  @next_examples = client.complete_and_fetch_examples executor.examples
rescue UnhandledResponseError => e
  # I do not think we should so lightly abandon the completion of executed examples
  # try to complete until successful or terminated
  warn e.message

  sleep 1
  retry
end

#executeObject



72
73
74
75
76
77
78
79
80
# File 'lib/specwrk/worker.rb', line 72

def execute
  executor.run next_examples
  @next_examples = nil
  complete_examples
rescue UnhandledResponseError => e
  # If fetching examples via next_exampels fails we can just try again so warn and return
  # Expects complete_examples to rescue this error if raised in that method
  warn e.message
end

#next_examplesObject



82
83
84
85
# File 'lib/specwrk/worker.rb', line 82

def next_examples
  return @next_examples if @next_examples&.length&.positive?
  client.fetch_examples
end

#runObject



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
# File 'lib/specwrk/worker.rb', line 29

def run
  Client.wait_for_server!

  loop do
    break if Specwrk.force_quit

    execute
  rescue CompletedAllExamplesError
    @all_examples_completed = true
    break
  rescue NoMoreExamplesError
    # Wait for the other processes (workers) on the same host to finish
    # This will cause workers to 'hang' until all work has been completed
    # TODO: break here if all the other worker processes on this host are done executing examples
    sleep 0.5
  rescue WaitingForSeedError
    @seed_wait_count ||= 0
    @seed_wait_count += 1

    if @seed_wait_count <= @seed_waits
      warn "No examples seeded yet, waiting..."
      sleep 1
    else
      warn "No examples seeded, giving up!"
      break
    end
  end

  executor.flush_log
  executor.final_output.tap(&:rewind).each_line { |line| final_output.write line }

  @heartbeat_thread.kill
  client.close

  status
rescue Errno::ECONNREFUSED
  warn "\nServer at #{ENV.fetch("SPECWRK_SRV_URI", "http://localhost:5138")} is refusing connections, exiting..."
  1
rescue Errno::ECONNRESET
  warn "\nServer at #{ENV.fetch("SPECWRK_SRV_URI", "http://localhost:5138")} stopped responding to connections, exiting..."
  1
end

#thumpObject



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/specwrk/worker.rb', line 98

def thump
  while running && !Specwrk.force_quit
    sleep 10

    begin
      client.heartbeat if client.last_request_at.nil? || client.last_request_at < Time.now - 9
    rescue
      warn "Heartbeat failed!"
    end
  end
end