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/chunked_io.rb,
lib/gitlab/ci/trace/section_parser.rb

Defined Under Namespace

Classes: 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)

Constants included from ExclusiveLeaseHelpers

ExclusiveLeaseHelpers::FailedToObtainLockError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Checksummable

#crc32

Methods included from ExclusiveLeaseHelpers

#in_lock

Constructor Details

#initialize(job) ⇒ Trace

Returns a new instance of Trace.


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

def initialize(job)
  @job = job
end

Instance Attribute Details

#jobObject (readonly)

Returns the value of attribute job


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

def job
  @job
end

Instance Method Details

#append(data, offset) ⇒ Object


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

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


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

def archive!
  in_write_lock do
    unsafe_archive!
  end
end

#archived_trace_exist?Boolean

Returns:

  • (Boolean)

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

def archived_trace_exist?
  trace_artifact&.exists?
end

#being_watched!Object


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

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)

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

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

#erase!Object


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

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)

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

def exist?
  archived_trace_exist? || live_trace_exist?
end

#extract_coverage(regex) ⇒ Object


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

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

#extract_sectionsObject


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

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

#html(last_lines: nil) ⇒ Object


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

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

#live_trace_exist?Boolean

Returns:

  • (Boolean)

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

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

#raw(last_lines: nil) ⇒ Object


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

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

#read(should_retry: true, &block) ⇒ Object


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

def read(should_retry: true, &block)
  read_stream(&block)
rescue Errno::ENOENT
  raise unless should_retry

  job.reset
  read_stream(&block)
end

#set(data) ⇒ Object


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

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

#update_intervalObject


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

def update_interval
  being_watched? ? UPDATE_FREQUENCY_WHEN_BEING_WATCHED : UPDATE_FREQUENCY_DEFAULT
end

#write(mode, &blk) ⇒ Object


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

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