Class: Ubalo::Task

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account, label, attributes) ⇒ Task

Returns a new instance of Task.



9
10
11
12
13
# File 'lib/ubalo/task.rb', line 9

def initialize(, label, attributes)
  @account = 
  @label = label
  update_attributes(attributes)
end

Instance Attribute Details

#arg_errorObject (readonly)

Returns the value of attribute arg_error.



3
4
5
# File 'lib/ubalo/task.rb', line 3

def arg_error
  @arg_error
end

#argvObject (readonly)

Returns the value of attribute argv.



3
4
5
# File 'lib/ubalo/task.rb', line 3

def argv
  @argv
end

#container_processObject (readonly)

Returns the value of attribute container_process.



115
116
117
# File 'lib/ubalo/task.rb', line 115

def container_process
  @container_process
end

#elapsed_timeObject (readonly)

Returns the value of attribute elapsed_time.



3
4
5
# File 'lib/ubalo/task.rb', line 3

def elapsed_time
  @elapsed_time
end

#labelObject (readonly)

Returns the value of attribute label.



3
4
5
# File 'lib/ubalo/task.rb', line 3

def label
  @label
end

#output_errorObject (readonly)

Returns the value of attribute output_error.



3
4
5
# File 'lib/ubalo/task.rb', line 3

def output_error
  @output_error
end

#pod_nameObject (readonly)

Returns the value of attribute pod_name.



3
4
5
# File 'lib/ubalo/task.rb', line 3

def pod_name
  @pod_name
end

#stateObject (readonly)

Returns the value of attribute state.



3
4
5
# File 'lib/ubalo/task.rb', line 3

def state
  @state
end

#submitted_atObject (readonly)

Returns the value of attribute submitted_at.



3
4
5
# File 'lib/ubalo/task.rb', line 3

def 
  @submitted_at
end

Class Method Details

.create_with_json(account, json) ⇒ Object



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

def self.create_with_json(, json)
  new(, json.fetch('label'), json)
end

Instance Method Details

#==(other) ⇒ Object



70
71
72
# File 'lib/ubalo/task.rb', line 70

def ==(other)
  label == other.label
end

#complete?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/ubalo/task.rb', line 25

def complete?
  state == 'exited'
end

#full_printable_resultObject



103
104
105
106
107
108
109
# File 'lib/ubalo/task.rb', line 103

def full_printable_result
  s = printable_result
  if container_process
    s << container_process.printable_result
  end
  s
end

#inspectObject



66
67
68
# File 'lib/ubalo/task.rb', line 66

def inspect
  "#<Task #{label.inspect} state=#{state.inspect}>"
end

#pending?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/ubalo/task.rb', line 21

def pending?
  %w{waiting running}.include?(state)
end

#pretty_argvObject



111
112
113
# File 'lib/ubalo/task.rb', line 111

def pretty_argv
  Shellwords.join(argv)
end

#printable_resultObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ubalo/task.rb', line 74

def printable_result
  s = ""
  s << " label: #{label}\n"
  s << "   pod: #{pod_name}\n"
  if argv
    s << "  argv: #{pretty_argv}\n"
  end
  if arg?
    if @arg_error
      s << "! Parse error in arg: #{@arg_error.message}"
    else
      s << "   arg:\n"
      s << Util.indent(arg.pretty_inspect)
    end
  end
  s << " state: #{state}\n"
  s << "  time: #{'%.1f' % elapsed_time} s\n"

  if output?
    if @output_error
      s << "! Parse error in output: #{@output_error.message}"
    else
      s << "output:\n"
      s << Util.indent(output.pretty_inspect)
    end
  end
  s
end

#refresh!Object



15
16
17
18
19
# File 'lib/ubalo/task.rb', line 15

def refresh!
  update_attributes(request(:get))
rescue RestClient::ResourceNotFound
  raise Ubalo::Error, "Could not find task #{label.inspect}"
end

#stop!Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ubalo/task.rb', line 50

def stop!
  tailer = Ubalo::TimedTailer.new(logger)

  tailer.update("Stopping #{label}...")
  request(:put, "/stop")
  refresh!

  logger.poll do
    tailer.update("Stopping #{label}...")
    refresh!
    complete?
  end

  tailer.update("Stopped #{label}. Exit code: #{container_process.exit_result}.\n")
end

#tail_processObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ubalo/task.rb', line 29

def tail_process
  tailer = Ubalo::TimedTailer.new(logger)

  wait_for_process(tailer)

  container_process.tail(tailer, "Running #{label}...") do
    refresh!
    complete?
  end

  tailer.update("Completed #{label}. Exit code: #{container_process.exit_result}.\n")
end

#urlObject



117
118
119
# File 'lib/ubalo/task.rb', line 117

def url
  @account.url_for(path_prefix)
end

#wait_for_process(tailer = nil) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/ubalo/task.rb', line 42

def wait_for_process(tailer = nil)
  logger.poll do
    tailer.update("Starting...") if tailer
    refresh!
    container_process
  end
end