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, worker_num, recorder = nil) ⇒ Worker

Returns a new instance of Worker.



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

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_file = File.open(status_path, 'a')
  @status_file.sync = true
  @status = nil
end

Instance Attribute Details

#basenameObject (readonly)

Returns the value of attribute basename.



15
16
17
# File 'lib/jcukeforker/worker.rb', line 15

def basename
  @basename
end

#featureObject (readonly)

Returns the value of attribute feature.



15
16
17
# File 'lib/jcukeforker/worker.rb', line 15

def feature
  @feature
end

#formatObject (readonly)

Returns the value of attribute format.



15
16
17
# File 'lib/jcukeforker/worker.rb', line 15

def format
  @format
end

#outObject (readonly)

Returns the value of attribute out.



15
16
17
# File 'lib/jcukeforker/worker.rb', line 15

def out
  @out
end

Instance Method Details

#argsObject



87
88
89
90
91
92
# File 'lib/jcukeforker/worker.rb', line 87

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
  @event_file.close
  @status_file.close
end

#failed?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/jcukeforker/worker.rb', line 70

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

#output(format = nil) ⇒ Object



74
75
76
77
# File 'lib/jcukeforker/worker.rb', line 74

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
  @event_file = File.open(@task_path, 'r')
  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
58
# File 'lib/jcukeforker/worker.rb', line 40

def run
  loop do
    raw_message = @event_file.gets(sep=$-0)
    if raw_message.nil? then
      sleep 0.1
      next
    end
    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 json_obj
    update_status :on_task_starting, feature
    status = execute_cucumber
    update_status :on_task_finished, feature, status
  end
end

#stderrObject



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

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

#stdoutObject



79
80
81
# File 'lib/jcukeforker/worker.rb', line 79

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

#update_status(meth, *args) ⇒ Object



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

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

  message += args

  changed
  notify_observers *message
  @status_file.write("#{message.to_json}#{$-0}")
end