Class: Bosh::Cli::TaskTracking::TaskTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/task_tracking/task_tracker.rb

Overview

This class is responsible for tracking director tasks

Constant Summary collapse

MAX_POLLS =

not limited

nil
DEFAULT_POLL_INTERVAL =

second

1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(director, task_id, options = {}) ⇒ TaskTracker


13
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
# File 'lib/cli/task_tracking/task_tracker.rb', line 13

def initialize(director, task_id, options = {})
  @director = director
  @task_id = task_id
  @task_finished_states = 'done error cancelled'
  if(options[:task_success_state])
    @task_finished_states << options[:task_success_state].to_s
  end
  @options = options

  @quiet = !!options[:quiet]
  default_log_type = @quiet ? 'none' : 'event'

  @log_type = options[:log_type] || default_log_type

  @output = nil
  @task = Bosh::Cli::DirectorTask.new(@director, @task_id, @log_type)

  if options[:renderer]
    @renderer = options[:renderer]
  elsif options[:raw_output]
    @renderer = TaskLogRenderer.new
  else
    @renderer = TaskLogRenderer.create_for_log_type(@log_type)
  end

  @poll_interval = Bosh::Cli::Config.poll_interval || DEFAULT_POLL_INTERVAL
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output


7
8
9
# File 'lib/cli/task_tracking/task_tracker.rb', line 7

def output
  @output
end

#rendererObject (readonly)

Returns the value of attribute renderer


8
9
10
# File 'lib/cli/task_tracking/task_tracker.rb', line 8

def renderer
  @renderer
end

Instance Method Details

#pollObject


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cli/task_tracking/task_tracker.rb', line 56

def poll
  polls = 0

  while true
    polls += 1
    state = @task.state
    output = @task.output

    output_received(output)
    @renderer.refresh

    if finished?(state)
      return state.to_sym
    elsif MAX_POLLS && polls >= MAX_POLLS
      return :track_timeout
    end

    sleep(@poll_interval)
  end

  :unknown
rescue Interrupt # Local ctrl-c handler
  prompt_for_task_cancel
end

112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cli/task_tracking/task_tracker.rb', line 112

def print_task_summary(task_status)
  output_received(@task.flush_output)
  @renderer.finish(task_status)

  nl
  say("Task #{@task_id} #{task_status.to_s.make_yellow}")

  if task_status == :done && @renderer.duration_known?
    nl
    say("Started\t\t#{@renderer.started_at.utc.to_s}")
    say("Finished\t#{@renderer.finished_at.utc.to_s}")
    say("Duration\t#{format_time(@renderer.duration).make_yellow}")
  end
end

#prompt_for_debug_logObject


81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cli/task_tracking/task_tracker.rb', line 81

def prompt_for_debug_log
  return unless interactive?
  nl
  confirm = ask('The task has returned an error status, ' +
    'do you want to see debug log? [Yn]: ')
  if confirm.empty? || confirm =~ /y(es)?/i
    self.class.new(@director, @task_id,
                   @options.merge(:log_type => 'debug')).track
  else
    say("Please use 'bosh task #{@task_id}' command ".make_red +
          'to see the debug log'.make_red)
  end
end

#prompt_for_task_cancelObject


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cli/task_tracking/task_tracker.rb', line 95

def prompt_for_task_cancel
  return unless interactive?
  nl
  confirm = ask("Do you want to cancel task #{@task_id}? [yN] " +
    '(^C again to detach): ')

  if confirm =~ /y(es)?/i
    say("Cancelling task #{@task_id}...")
    @director.cancel_task(@task_id)
  end

  poll
rescue Interrupt
  nl
  err("Task #{@task_id} is still running")
end

#trackSymbol

Tracks director task. Blocks until task is in one of the 'finished' states (done, error, cancelled). Handles Ctrl+C by prompting to cancel task.


45
46
47
48
49
50
51
52
53
54
# File 'lib/cli/task_tracking/task_tracker.rb', line 45

def track
  nl
  @renderer.time_adjustment = @director.get_time_difference
  say("Director task #{@task_id.to_s.make_yellow}")
  task_status = poll

  print_task_summary(task_status)

  task_status
end