class DTK::Shell::StatusMonitor

Constants

THREAD_SLEEP_TIME

Public Class Methods

check_status() click to toggle source
# File lib/shell/status_monitor.rb, line 56
def self.check_status
  self.instance.check_status
end
new() click to toggle source
# File lib/shell/status_monitor.rb, line 46
def initialize
  @threads        = []
  @finished_tasks = []
  @conn           = DTK::Client::Session.get_connection()
end
start_monitoring(task_id) click to toggle source
# File lib/shell/status_monitor.rb, line 52
def self.start_monitoring(task_id)
  self.instance.start_monitoring(task_id)
end

Public Instance Methods

check_status() click to toggle source
# File lib/shell/status_monitor.rb, line 60
def check_status
  @threads.each do |t|
    if t.finished
      @finished_tasks << t
    end
  end

  # removes finished tasks from the main queue
  @threads = @threads - @finished_tasks

  @finished_tasks.each do |t|
    puts ""
    puts "[TASK NOTICE] Task with ID: #{t.task_id}, has finished with status: #{colorize_status(t.status)}"
  end

  @finished_tasks.clear
end
start_monitoring(task_id) click to toggle source
# File lib/shell/status_monitor.rb, line 78
def start_monitoring(task_id)
  puts "Client has started monitoring task [ID:#{task_id}]. You will be notified when task has been completed."
  @threads << DTK::Shell::TaskStatusThread.new do
    begin
      response, post_hash_body = nil, {}
      post_hash_body[:task_id] = task_id
      DTK::Shell::TaskStatusThread.current.task_id = task_id

      # pooling server for task status
      while task_running?(response)
        sleep(THREAD_SLEEP_TIME) unless response.nil?
        response = post rest_url("task/status"),post_hash_body
        # we break if there is error in response
        break unless response.ok?
      end

      DTK::Shell::TaskStatusThread.current.finished = true

      if response.ok?
        DTK::Shell::TaskStatusThread.current.status = response.data['status'].upcase
      else
        DTK::Shell::TaskStatusThread.current.status = "RESPONSE NOT OK, RESPONSE: #{response}"
      end

    rescue Exception => e
      DtkLogger.instance.error_pp("[THREAD ERROR] Error getting task status with message: #{e.message}", e.backtrace)
    end
  end
end

Private Instance Methods

colorize_status(status) click to toggle source
# File lib/shell/status_monitor.rb, line 110
def colorize_status(status)
  color = status.eql?('FAILED') ? :red : :green
  return DTK::Client::OsUtil.colorize(status, color)
end
task_running?(response) click to toggle source

return true if status SUCCEDED, FAILED returns false if status EXECUTING

# File lib/shell/status_monitor.rb, line 117
def task_running?(response)
  return true if response.nil?
  return !(response.data['status'].eql?("succeeded") || response.data['status'].eql?("failed"))
end