lib/jcukeforker/worker.rb in jcukeforker-0.2.10 vs lib/jcukeforker/worker.rb in jcukeforker-0.3.1

- old
+ new

@@ -1,6 +1,5 @@ -require 'socket' require 'securerandom' require 'json' require 'fileutils' require 'cucumber/cli/main' require 'observer' @@ -14,57 +13,61 @@ class Worker include Observable attr_reader :feature, :format, :out, :basename - def initialize(status_path, task_path, recorder = nil) + def initialize(status_path, task_path, worker_num, recorder = nil) @status_path = status_path @task_path = task_path + @worker_num = worker_num if ENV['DISPLAY'] && recorder config = JSON.parse(recorder) add_observer JCukeForker::RecordingVncListener.new(self, config) end - @status_socket = TCPSocket.new 'localhost', status_path + @status_file = File.open(status_path, 'a') + @status_file.sync = true @status = nil end def register - @worker_server = UNIXServer.new @task_path + @event_file = File.open(@task_path, 'r') update_status :on_worker_register end def close - @worker_server.close - @status_socket.close + @event_file.close + @status_file.close end def run - worker_socket = @worker_server.accept loop do - raw_message = worker_socket.gets + raw_message = @event_file.gets(sep=$-0) if raw_message.nil? then - sleep 0.3 + sleep 0.1 next end - if raw_message.strip == '__KILL__' + json_obj = JSON.parse raw_message + next unless json_obj['worker'] == @worker_num + if json_obj['action'] == '__KILL__' update_status :on_worker_dead break end - set_state raw_message + set_state json_obj update_status :on_task_starting, feature status = execute_cucumber update_status :on_task_finished, feature, status end end def update_status(meth, *args) - message = [meth, @task_path] + message = [meth, @worker_num] + message += args changed notify_observers *message - @status_socket.puts(message.to_json) + @status_file.write("#{message.to_json}#{$-0}") end def failed? @status.nil? || !@status end @@ -89,12 +92,11 @@ args end private - def set_state(raw_message) + def set_state(json_obj) @status = nil - json_obj = JSON.parse raw_message @format = json_obj['format'] @feature = json_obj['feature'] @extra_args = json_obj['extra_args'] @out = json_obj['out'] @basename = feature.gsub(/\W/, '_')