Module: Nucleus::Adapters::V1::Heroku::Logs

Included in:
Nucleus::Adapters::V1::Heroku
Defined in:
lib/nucleus/adapters/v1/heroku/logs.rb

Constant Summary collapse

CRLF =

Carriage return (newline in Mac OS) + line feed (newline in Unix) == CRLF (newline in Windows)

"\r\n".freeze

Instance Method Summary collapse

Instance Method Details

#log?(application_id, log_id) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



24
25
26
27
28
29
30
31
# File 'lib/nucleus/adapters/v1/heroku/logs.rb', line 24

def log?(application_id, log_id)
  # fails with 404 if application is not available
  get("/apps/#{application_id}")

  return true if log_id.to_sym == :all
  return true if log_id.to_sym == :build
  available_log_types.key? log_id.to_sym
end

#log_entries(application_id, log_id) ⇒ Object

See Also:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/nucleus/adapters/v1/heroku/logs.rb', line 34

def log_entries(application_id, log_id)
  unless log?(application_id, log_id)
    raise Errors::AdapterResourceNotFoundError,
          "Invalid log file '#{log_id}', not available for application '#{application_id}'"
  end

  return build_log_entries(application_id) if log_id.to_sym == Enums::ApplicationLogfileType::BUILD

  request_body = request_body(log_id.to_sym).merge(tail: false)
  log = post("/apps/#{application_id}/log-sessions", body: request_body).body
  logfile = get(log[:logplex_url], headers: {}).body
  # process to entries
  entries = []
  # skip empty logs, which are detected as Hash by the http client
  logfile.split(CRLF).each { |logfile_line| entries.push logfile_line } unless logfile == {}
  entries
end

#logs(application_id) ⇒ Object

See Also:



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/nucleus/adapters/v1/heroku/logs.rb', line 10

def logs(application_id)
  # fails with 404 if application is not available and serves for timestamps
  app = get("/apps/#{application_id}").body

  available_log_files = []
  available_log_types.keys.each do |type|
    # TODO: right now, we always assume the log has recently been updated
    available_log_files.push(id: type, name: type, type: type,
                             created_at: app[:created_at], updated_at: Time.now.utc.iso8601)
  end
  available_log_files
end

#tail(application_id, log_id, stream) ⇒ Object

See Also:



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nucleus/adapters/v1/heroku/logs.rb', line 53

def tail(application_id, log_id, stream)
  # Currently no tailing for build log possible
  if log_id == Enums::ApplicationLogfileType::BUILD
    entries = build_log_entries(application_id)
    entries.each { |entry| stream.send_message(entry) }
    stream.close
  else
    request_body = request_body(log_id.to_sym).merge(tail: true)
    log = post("/apps/#{application_id}/log-sessions", body: request_body).body
    tail_http_response(log[:logplex_url], stream)
  end
end