Class: Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, tourists, number, runner_id, tour_list) ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
11
# File 'lib/runner.rb', line 7

def initialize(host, tourists, number, runner_id, tour_list)
  @host, @tourists, @number, @runner_id, @tour_list = host, tourists, number, runner_id, tour_list
  @runner_type = self.send(:class).to_s
  log("Ready to run #{@runner_type}")
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/runner.rb', line 5

def host
  @host
end

#numberObject (readonly)

Returns the value of attribute number.



5
6
7
# File 'lib/runner.rb', line 5

def number
  @number
end

#runner_idObject (readonly)

Returns the value of attribute runner_id.



5
6
7
# File 'lib/runner.rb', line 5

def runner_id
  @runner_id
end

#runner_typeObject (readonly)

Returns the value of attribute runner_type.



5
6
7
# File 'lib/runner.rb', line 5

def runner_type
  @runner_type
end

#touristsObject (readonly)

Returns the value of attribute tourists.



5
6
7
# File 'lib/runner.rb', line 5

def tourists
  @tourists
end

Instance Method Details

#run_touristsObject

Dispatches to subclass run method



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
# File 'lib/runner.rb', line 14

def run_tourists 
  log "Filtering on tours #{@tour_list.join(', ')}" unless @tour_list.to_a.empty?
  tourists,tours,passes,fails,errors = 0,0,0,0,0
  1.upto(number) do |num|
    log("Starting #{@runner_type} run #{num}/#{number}")
    @tourists.each do |tourist_name|
      
      log("Starting run #{num}/#{number} of Tourist #{tourist_name}")
      tourists += 1
      tourist = Tourist.make_tourist(tourist_name,@host,@tourists,@number,@runner_id)
      tourist.before_tours
      
      tourist.tours.each do |tour|
        times = Hash.new {|h,k| h[k] = {}}
        
        next if tour_limited_to(tour)

        begin
          tours += 1
          times[tour][:started] = Time.now
          tourist.run_tour tour
          passes += 1
        rescue TourBusException, WebratError => e
          log("********** FAILURE IN RUN! **********")
          log e.message
          e.backtrace.each do |trace|
            log trace
          end
          fails += 1
        rescue Exception => e
          log("*************************************")
          log("*********** ERROR IN RUN! ***********")
          log("*************************************")
          log e.message
          e.backtrace.each do |trace|
            log trace
          end
          errors += 1
        ensure
          times[tour][:finished] = Time.now
          times[tour][:elapsed] = times[tour][:finished] - times[tour][:started]
        end 
        log("Finished run #{num}/#{number} of Tourist #{tourist_name}")
      end
      
      tourist.after_tours
    end
    log("Finished #{@runner_type} run #{num}/#{number}")
  end
  log("Finished all #{@runner_type} tourists.")
  [tourists,tours,passes,fails,errors]
end