Commit f183c88c authored by Matija Čupić's avatar Matija Čupić Committed by Stan Hu

Optimize Projects::JobsController#trace endpoint

Optimizes Projects::JobsController#trace endpoint to:

- Mark the job as being watched only if it's running
- Serialize the job trace only if there is a trace to serialize
parent 65c4e95e
......@@ -49,25 +49,25 @@ class Projects::JobsController < Projects::ApplicationController
# rubocop: enable CodeReuse/ActiveRecord
def trace
@build.trace.read do |stream|
respond_to do |format|
format.json do
@build.trace.being_watched!
@build.trace.being_watched! if @build.running?
build_trace = Ci::BuildTrace.new(
build: @build,
stream: stream,
state: params[:state])
if @build.has_trace?
@build.trace.read do |stream|
respond_to do |format|
format.json do
build_trace = Ci::BuildTrace.new(
build: @build,
stream: stream,
state: params[:state])
if @build.has_trace?
render json: BuildTraceSerializer
.new(project: @project, current_user: @current_user)
.represent(build_trace)
else
head :no_content
end
end
end
else
head :no_content
end
end
......
......@@ -700,10 +700,22 @@ RSpec.describe Projects::JobsController, :clean_gitlab_redis_shared_state do
expect(json_response['lines']).to eq [{ 'content' => [{ 'text' => 'BUILD TRACE' }], 'offset' => 0 }]
end
it 'sets being-watched flag for the job' do
expect(response).to have_gitlab_http_status(:ok)
context 'when job is running' do
let(:job) { create(:ci_build, :trace_live, :running, pipeline: pipeline) }
it 'sets being-watched flag for the job' do
expect(response).to have_gitlab_http_status(:ok)
expect(job.trace.being_watched?).to be(true)
end
end
expect(job.trace.being_watched?).to be(true)
context 'when job is not running' do
it 'does not set being-watched flag for the job' do
expect(response).to have_gitlab_http_status(:ok)
expect(job.trace.being_watched?).to be(false)
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment