Class: DTK::Shell::StatusMonitor

Inherits:
Object
  • Object
show all
Includes:
Client::CommandBase, Singleton
Defined in:
lib/shell/status_monitor.rb

Constant Summary collapse

THREAD_SLEEP_TIME =
DTK::Configuration.get(:task_check_frequency)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Client::CommandBase

#get, #get_connection, handle_argument_error, #post, #post_file, #rest_url, #rotate_args

Constructor Details

#initializeStatusMonitor

Returns a new instance of StatusMonitor.



46
47
48
49
50
# File 'lib/shell/status_monitor.rb', line 46

def initialize
  @threads        = []
  @finished_tasks = []
  @conn           = DTK::Client::Session.get_connection()
end

Class Method Details

.check_statusObject



56
57
58
# File 'lib/shell/status_monitor.rb', line 56

def self.check_status
  self.instance.check_status
end

.start_monitoring(task_id) ⇒ Object



52
53
54
# File 'lib/shell/status_monitor.rb', line 52

def self.start_monitoring(task_id)
  self.instance.start_monitoring(task_id)
end

Instance Method Details

#check_statusObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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