Class: JCukeForker::Worker

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/jcukeforker/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status_path, task_path, recorder = nil) ⇒ Worker



19
20
21
22
23
24
25
26
27
28
# File 'lib/jcukeforker/worker.rb', line 19

def initialize(status_path, task_path, recorder = nil)
  @status_path = status_path
  @task_path = task_path
  if ENV['DISPLAY'] && recorder
    config = JSON.parse(recorder)
    add_observer JCukeForker::RecordingVncListener.new(self, config)
  end
  @status_socket = TCPSocket.new 'localhost', status_path
  @status = nil
end

Instance Attribute Details

#basenameObject (readonly)

Returns the value of attribute basename.



17
18
19
# File 'lib/jcukeforker/worker.rb', line 17

def basename
  @basename
end

#featureObject (readonly)

Returns the value of attribute feature.



17
18
19
# File 'lib/jcukeforker/worker.rb', line 17

def feature
  @feature
end

#formatObject (readonly)

Returns the value of attribute format.



17
18
19
# File 'lib/jcukeforker/worker.rb', line 17

def format
  @format
end

#outObject (readonly)

Returns the value of attribute out.



17
18
19
# File 'lib/jcukeforker/worker.rb', line 17

def out
  @out
end

Instance Method Details

#argsObject



85
86
87
88
89
90
# File 'lib/jcukeforker/worker.rb', line 85

def args
  args = Array(format).flat_map { |f| %W[--format #{f} --out #{output(f)}] }
  args += @extra_args
  args << feature
  args
end

#closeObject



35
36
37
38
# File 'lib/jcukeforker/worker.rb', line 35

def close
  @worker_server.close
  @status_socket.close
end

#failed?Boolean



68
69
70
# File 'lib/jcukeforker/worker.rb', line 68

def failed?
  @status.nil? || !@status
end

#output(format = nil) ⇒ Object



72
73
74
75
# File 'lib/jcukeforker/worker.rb', line 72

def output(format = nil)
  format = @format if format.nil?
  File.join out, "#{basename}.#{format}"
end

#registerObject



30
31
32
33
# File 'lib/jcukeforker/worker.rb', line 30

def register
  @worker_server = UNIXServer.new @task_path
  update_status :on_worker_register
end

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jcukeforker/worker.rb', line 40

def run
  worker_socket = @worker_server.accept
  loop do
    raw_message = worker_socket.gets
    if raw_message.nil? then
      sleep 0.3
      next
    end
    if raw_message.strip == '__KILL__'
      update_status :on_worker_dead
      break
    end
    set_state raw_message
    update_status :on_task_starting, feature
    status = execute_cucumber
    update_status :on_task_finished, feature, status
  end
end

#stderrObject



81
82
83
# File 'lib/jcukeforker/worker.rb', line 81

def stderr
  File.join out, "#{basename}.stderr"
end

#stdoutObject



77
78
79
# File 'lib/jcukeforker/worker.rb', line 77

def stdout
  File.join out, "#{basename}.stdout"
end

#update_status(meth, *args) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/jcukeforker/worker.rb', line 59

def update_status(meth, *args)
  message = [meth, @task_path]
  message += args

  changed
  notify_observers *message
  @status_socket.puts(message.to_json)
end