Class: Ci::AppendBuildTraceService

Inherits:
Object
  • Object
show all
Defined in:
app/services/ci/append_build_trace_service.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

TraceRangeError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(build, params) ⇒ AppendBuildTraceService

Returns a new instance of AppendBuildTraceService.



10
11
12
13
# File 'app/services/ci/append_build_trace_service.rb', line 10

def initialize(build, params)
  @build = build
  @params = params
end

Instance Attribute Details

#buildObject (readonly)

Returns the value of attribute build.



8
9
10
# File 'app/services/ci/append_build_trace_service.rb', line 8

def build
  @build
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'app/services/ci/append_build_trace_service.rb', line 8

def params
  @params
end

Instance Method Details

#execute(body_data) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/ci/append_build_trace_service.rb', line 15

def execute(body_data)
  # TODO:
  # it seems that `Content-Range` as formatted by runner is wrong,
  # the `byte_end` should point to final byte, but it points byte+1
  # that means that we have to calculate end of body,
  # as we cannot use `content_length[1]`
  # Issue: https://gitlab.com/gitlab-org/gitlab-runner/issues/3275

  content_range = stream_range.split('-')
  body_start = content_range[0].to_i
  body_end = body_start + body_data.bytesize

  if first_debug_chunk?(body_start)
    # Update the build metadata prior to appending trace content
    build.enable_debug_trace!
  end

  if trace_size_exceeded?(body_end)
    build.drop(:trace_size_exceeded)

    return Result.new(status: 403)
  end

  stream_size = build.trace.append(body_data, body_start)

  unless stream_size == body_end
    log_range_error(stream_size, body_end)

    return Result.new(status: 416, stream_size: stream_size)
  end

  Result.new(status: 202, stream_size: stream_size)
end