Class: Gitlab::Ci::Trace

Inherits:
Object
  • Object
show all
Includes:
Checksummable, ExclusiveLeaseHelpers
Defined in:
lib/gitlab/ci/trace.rb,
lib/gitlab/ci/trace/stream.rb,
lib/gitlab/ci/trace/metrics.rb,
lib/gitlab/ci/trace/checksum.rb,
lib/gitlab/ci/trace/chunked_io.rb,
lib/gitlab/ci/trace/section_parser.rb

Defined Under Namespace

Classes: Checksum, ChunkedIO, Metrics, SectionParser, Stream

Constant Summary collapse

LOCK_TTL =
10.minutes
LOCK_RETRIES =
2
LOCK_SLEEP =
0.001.seconds
WATCH_FLAG_TTL =
10.seconds
UPDATE_FREQUENCY_DEFAULT =
30.seconds
UPDATE_FREQUENCY_WHEN_BEING_WATCHED =
3.seconds
ArchiveError =
Class.new(StandardError)
AlreadyArchivedError =
Class.new(StandardError)
LockedError =
Class.new(StandardError)

Constants included from ExclusiveLeaseHelpers

ExclusiveLeaseHelpers::FailedToObtainLockError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ExclusiveLeaseHelpers

#in_lock

Constructor Details

#initialize(job) ⇒ Trace

Returns a new instance of Trace.


25
26
27
# File 'lib/gitlab/ci/trace.rb', line 25

def initialize(job)
  @job = job
end

Instance Attribute Details

#jobObject (readonly)

Returns the value of attribute job


21
22
23
# File 'lib/gitlab/ci/trace.rb', line 21

def job
  @job
end

Instance Method Details

#append(data, offset) ⇒ Object


60
61
62
63
64
65
66
67
68
69
# File 'lib/gitlab/ci/trace.rb', line 60

def append(data, offset)
  write('a+b') do |stream|
    current_length = stream.size
    break current_length unless current_length == offset

    data = job.hide_secrets(data)
    stream.append(data, offset)
    stream.size
  end
end

#archive!Object


110
111
112
113
114
# File 'lib/gitlab/ci/trace.rb', line 110

def archive!
  in_write_lock do
    unsafe_archive!
  end
end

#archived_trace_exist?Boolean

Returns:

  • (Boolean)

75
76
77
# File 'lib/gitlab/ci/trace.rb', line 75

def archived_trace_exist?
  trace_artifact&.exists?
end

#being_watched!Object


120
121
122
123
124
# File 'lib/gitlab/ci/trace.rb', line 120

def being_watched!
  Gitlab::Redis::SharedState.with do |redis|
    redis.set(being_watched_cache_key, true, ex: WATCH_FLAG_TTL)
  end
end

#being_watched?Boolean

Returns:

  • (Boolean)

126
127
128
129
130
# File 'lib/gitlab/ci/trace.rb', line 126

def being_watched?
  Gitlab::Redis::SharedState.with do |redis|
    redis.exists(being_watched_cache_key)
  end
end

#erase!Object


96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gitlab/ci/trace.rb', line 96

def erase!
  ##
  # Erase the archived trace
  trace_artifact&.destroy!

  ##
  # Erase the live trace
  job.trace_chunks.fast_destroy_all # Destroy chunks of a live trace
  FileUtils.rm_f(current_path) if current_path # Remove a trace file of a live trace
  job.erase_old_trace! if job.has_old_trace? # Remove a trace in database of a live trace
ensure
  @current_path = nil
end

#exist?Boolean

Returns:

  • (Boolean)

71
72
73
# File 'lib/gitlab/ci/trace.rb', line 71

def exist?
  archived_trace_exist? || live_trace_exist?
end

#extract_coverage(regex) ⇒ Object


41
42
43
44
45
# File 'lib/gitlab/ci/trace.rb', line 41

def extract_coverage(regex)
  read do |stream|
    stream.extract_coverage(regex)
  end
end

#extract_sectionsObject


47
48
49
50
51
# File 'lib/gitlab/ci/trace.rb', line 47

def extract_sections
  read do |stream|
    stream.extract_sections
  end
end

#html(last_lines: nil) ⇒ Object


29
30
31
32
33
# File 'lib/gitlab/ci/trace.rb', line 29

def html(last_lines: nil)
  read do |stream|
    stream.html(last_lines: last_lines)
  end
end

#live_trace_exist?Boolean

Returns:

  • (Boolean)

79
80
81
# File 'lib/gitlab/ci/trace.rb', line 79

def live_trace_exist?
  job.trace_chunks.any? || current_path.present? || old_trace.present?
end

#lock(&block) ⇒ Object


132
133
134
135
136
# File 'lib/gitlab/ci/trace.rb', line 132

def lock(&block)
  in_write_lock(&block)
rescue FailedToObtainLockError
  raise LockedError, "build trace `#{job.id}` is locked"
end

#raw(last_lines: nil) ⇒ Object


35
36
37
38
39
# File 'lib/gitlab/ci/trace.rb', line 35

def raw(last_lines: nil)
  read do |stream|
    stream.raw(last_lines: last_lines)
  end
end

#read(&block) ⇒ Object


83
84
85
86
87
88
# File 'lib/gitlab/ci/trace.rb', line 83

def read(&block)
  read_stream(&block)
rescue Errno::ENOENT, ChunkedIO::FailedToGetChunkError
  job.reset
  read_stream(&block)
end

#set(data) ⇒ Object


53
54
55
56
57
58
# File 'lib/gitlab/ci/trace.rb', line 53

def set(data)
  write('w+b') do |stream|
    data = job.hide_secrets(data)
    stream.set(data)
  end
end

#update_intervalObject


116
117
118
# File 'lib/gitlab/ci/trace.rb', line 116

def update_interval
  being_watched? ? UPDATE_FREQUENCY_WHEN_BEING_WATCHED : UPDATE_FREQUENCY_DEFAULT
end

#write(mode, &blk) ⇒ Object


90
91
92
93
94
# File 'lib/gitlab/ci/trace.rb', line 90

def write(mode, &blk)
  in_write_lock do
    unsafe_write!(mode, &blk)
  end
end