lib/cloudstack-cli/helper.rb in cloudstack-cli-1.5.9 vs lib/cloudstack-cli/helper.rb in cloudstack-cli-1.5.10
- old
+ new
@@ -22,11 +22,11 @@
def watch_jobs(jobs)
chars = %w(| / - \\)
call = 0
opts = {t_start: Time.now}
jobs = update_job_status(jobs)
- while jobs.select{|job| job[:status].to_i < 1 }.size > 0 do
+ while jobs.count{|job| job[:status].to_i < 1 } > 0 do
if call.modulo(40) == 0
t = Thread.new { jobs = update_job_status(jobs) }
while t.alive?
chars = print_job_status(jobs, chars,
call == 0 ? opts.merge(no_clear: true) : opts
@@ -48,11 +48,16 @@
def update_job_status(jobs)
jobs.each do |job|
job[:status] = 0 unless job[:status]
if job[:status] == 0
- job[:status] = client.query_async_job_result(job_id: job[:id])['jobstatus']
+ result = client.query_async_job_result(job_id: job[:id])
+ job[:status] = result["jobstatus"]
+ # add result information for terminated jobs
+ if job[:status].between?(1, 2)
+ job[:result] = result["jobresult"]
+ end
end
end
jobs
end
@@ -60,11 +65,11 @@
view_thread = Thread.new do
chars = %w(| / - \\)
call = 0
opts = {t_start: Time.now}
- while jobs.select{|job| job[:status] < 1 }.size > 0 do
+ while jobs.count{|job| job[:status] < 1 } > 0 do
if call.modulo(40) == 0
t = Thread.new { jobs = update_jobs(jobs, command) }
while t.alive?
chars = print_job_status(jobs, chars,
call == 0 ? opts.merge(no_clear: true) : opts
@@ -88,23 +93,27 @@
def update_jobs(jobs, command)
# update running job status
threads = jobs.select{|job| job[:status] == 0 }.map do |job|
Thread.new do
- job[:status] = client.query_async_job_result(job_id: job[:job_id])['jobstatus']
+ result = client.query_async_job_result(job_id: job[:job_id])
+ job[:status] = result['jobstatus']
+ if job[:status].between?(1, 2)
+ job[:result] = result["jobresult"]
+ end
end
end
threads.each(&:join)
# launch new jobs if required and possible
- launch_capacity = options[:concurrency] - jobs.select{|job| job[:status] == 0 }.count
+ launch_capacity = (options[:concurrency] ||= 10) - jobs.count{|job| job[:status] == 0 }
threads = []
jobs.select{|job| job[:status] == -1 }.each do |job|
if launch_capacity > 0
threads << Thread.new do
job[:job_id] = client.send(
- command, { id: job[:object_id] }, { sync: true }
+ command, job[:args], { sync: true }
)['jobid']
job[:status] = 0
end
launch_capacity -= 1
end
@@ -118,10 +127,10 @@
jobs.each_with_index do |job, i|
print "#{job[:name]} : job #{ASYNC_STATES[job[:status]]} "
puts job[:status] == 0 ? spinner.first : ""
end
t_elapsed = opts[:t_start] ? (Time.now - opts[:t_start]).round(1) : 0
- completed = jobs.select{|j| j[:status] == 1}.size
+ completed = jobs.count{|j| j[:status] == 1 }
say "Completed: #{completed} of #{jobs.size} (#{t_elapsed}s)", :magenta
sleep opts[:sleeptime] || 0.1
spinner.push spinner.shift
spinner
end