Class: Holistics::Helpers::JobManager

Inherits:
Object
  • Object
show all
Defined in:
lib/holistics/helpers/job_manager.rb

Constant Summary collapse

MAX_RETRIES =
3

Instance Method Summary collapse

Instance Method Details

#auth_helperObject



6
7
8
# File 'lib/holistics/helpers/job_manager.rb', line 6

def auth_helper
  @auth_info ||= Helpers::AuthInfo.new
end

#fetch_job_details(job_id, last_id = 0) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/holistics/helpers/job_manager.rb', line 10

def fetch_job_details(job_id, last_id = 0)
  tries ||= MAX_RETRIES

  url = auth_helper.api_url_for("jobs/#{job_id}/logs.json", last_id: last_id)
  response = http_request.simple_get url

  JSON.parse(response.body)
rescue StandardError => e
  sleep 2 ** (MAX_RETRIES - tries) unless Holistics.test?
  if (tries -= 1) >= 0
    puts 'Retrying...'
    retry
  end
  puts 'Retry exceeded... Raise error'
  raise e
end

#fetch_job_results(job_id) ⇒ Object



27
28
29
30
# File 'lib/holistics/helpers/job_manager.rb', line 27

def fetch_job_results(job_id)
  url = "jobs/#{job_id}/get_results.json"
  http_request.get(url, "Cannot fetch info of job #{job_id}")
end

#job_show(options) ⇒ Object



32
33
34
35
# File 'lib/holistics/helpers/job_manager.rb', line 32

def job_show options
  job_id = options[:job_id]
  tail_logs(job_id)
end


51
52
53
54
# File 'lib/holistics/helpers/job_manager.rb', line 51

def print_log log
  ts = Time.parse(log['timestamp'])
  Holistics.logger.log(log['level'], log['message'], timestamp: ts)
end

#tail_logs(job_id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/holistics/helpers/job_manager.rb', line 37

def tail_logs(job_id)
  last_id = 0
  while true
    parsed = fetch_job_details(job_id, last_id)
    logs = parsed['logs'] || []
    logs.each { |log| print_log(log) }
    last_id = logs.last['id'] if logs.size > 0

    break unless parsed['has_more']

    sleep 0.5
  end
end