Class: Bosh::Cli::TaskTracker

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

Overview

This class is responsible for tracking director tasks

Constant Summary collapse

MAX_POLLS =

not limited

nil
POLL_INTERVAL =

second

1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of TaskTracker.

Parameters:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cli/task_tracker.rb', line 16

def initialize(director, task_id, options = {})
  @director = director
  @task_id = task_id
  @options = options

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

  @log_type = options[:log_type] || default_log_type
  @use_cache = options.key?(:use_cache) ? @options[:use_cache] : true

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

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

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



11
12
13
# File 'lib/cli/task_tracker.rb', line 11

def output
  @output
end

Instance Method Details

#pollObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cli/task_tracker.rb', line 63

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


119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/cli/task_tracker.rb', line 119

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

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

  if task_status == :done && @renderer.duration_known?
    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).yellow}")
  end
end

#prompt_for_debug_logObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cli/task_tracker.rb', line 88

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 ".red +
          "to see the debug log".red)
  end
end

#prompt_for_task_cancelObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cli/task_tracker.rb', line 102

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.

Returns:

  • (Symbol)

    Task status



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cli/task_tracker.rb', line 42

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

  cached_output = get_cached_task_output

  if cached_output
    task_status = @task.state.to_sym
    output_received(cached_output)
    @renderer.refresh
  else
    task_status = poll
  end

  print_task_summary(task_status)

  save_task_output unless cached_output
  task_status
end