Class: RRRSpec::Client::SlaveRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/rrrspec/client/slave_runner.rb

Defined Under Namespace

Classes: RedisReportingFormatter, SoftTimeoutException

Constant Summary collapse

TASKQUEUE_TASK_TIMEOUT =
-1
TASKQUEUE_ARBITER_TIMEOUT =
20
TIMEOUT_EXITCODE =
42

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slave, working_dir, taskset_key) ⇒ SlaveRunner

Returns a new instance of SlaveRunner.



16
17
18
19
20
21
22
23
24
25
# File 'lib/rrrspec/client/slave_runner.rb', line 16

def initialize(slave, working_dir, taskset_key)
  @slave = slave
  @taskset = Taskset.new(taskset_key)
  @timeout = TASKQUEUE_TASK_TIMEOUT
  @rspec_runner = RSpecRunner.new
  @working_path = File.join(working_dir, @taskset.rsync_name)
  @unknown_spec_timeout_sec = @taskset.unknown_spec_timeout_sec
  @least_timeout_sec = @taskset.least_timeout_sec
  @worked_task_keys = Set.new
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/rrrspec/client/slave_runner.rb', line 9

def key
  @key
end

Instance Method Details

#spec_timeout_sec(task) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rrrspec/client/slave_runner.rb', line 31

def spec_timeout_sec(task)
  if task.estimate_sec == nil
    soft_timeout_sec = @unknown_spec_timeout_sec
    hard_timeout_sec = @unknown_spec_timeout_sec + 30
  else
    estimate_sec = task.estimate_sec
    soft_timeout_sec = estimate_sec * 2
    hard_timeout_sec = estimate_sec * 3
  end
  return [soft_timeout_sec, @least_timeout_sec].max, [hard_timeout_sec, @least_timeout_sec].max
end

#workObject



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rrrspec/client/slave_runner.rb', line 43

def work
  task = @taskset.dequeue_task(@timeout)
  unless task
    @timeout = TASKQUEUE_ARBITER_TIMEOUT
    ArbiterQueue.check(@taskset)
  else
    if @worked_task_keys.include?(task.key)
      @taskset.reversed_enqueue_task(task)
      return
    else
      @worked_task_keys << task.key
    end
    return if task.status.present?

    @timeout = TASKQUEUE_TASK_TIMEOUT
    trial = Trial.create(task, @slave)

    @rspec_runner.reset
    $0 = "rrrspec slave[#{ENV['SLAVE_NUMBER']}]: setting up #{task.spec_file}"
    status, outbuf, errbuf = @rspec_runner.setup(File.join(@working_path, task.spec_file))
    unless status
      trial.finish('error', outbuf, errbuf, nil, nil, nil)
      ArbiterQueue.trial(trial)
      return
    end

    soft_timeout_sec, hard_timeout_sec = spec_timeout_sec(task)

    formatter = RedisReportingFormatter
    trial.start
    $0 = "rrrspec slave[#{ENV['SLAVE_NUMBER']}]: running #{task.spec_file}"
    status, outbuf, errbuf = ExtremeTimeout::timeout(
      hard_timeout_sec, TIMEOUT_EXITCODE
    ) do
      Timeout::timeout(soft_timeout_sec, SoftTimeoutException) do
        @rspec_runner.run(formatter)
      end
    end
    if status
      trial.finish(formatter.status, outbuf, errbuf,
                   formatter.passed, formatter.pending, formatter.failed)
    else
      trial.finish('error', outbuf, errbuf, nil, nil, nil)
    end

    ArbiterQueue.trial(trial)
  end
ensure
  $0 = "rrrspec slave[#{ENV['SLAVE_NUMBER']}]"
end

#work_loopObject



27
28
29
# File 'lib/rrrspec/client/slave_runner.rb', line 27

def work_loop
  loop { work }
end