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 ...@@ -49,25 +49,25 @@ class Projects::JobsController < Projects::ApplicationController
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
def trace def trace
@build.trace.read do |stream| @build.trace.being_watched! if @build.running?
respond_to do |format|
format.json do
@build.trace.being_watched!
build_trace = Ci::BuildTrace.new( if @build.has_trace?
build: @build, @build.trace.read do |stream|
stream: stream, respond_to do |format|
state: params[:state]) format.json do
build_trace = Ci::BuildTrace.new(
build: @build,
stream: stream,
state: params[:state])
if @build.has_trace?
render json: BuildTraceSerializer render json: BuildTraceSerializer
.new(project: @project, current_user: @current_user) .new(project: @project, current_user: @current_user)
.represent(build_trace) .represent(build_trace)
else
head :no_content
end end
end end
end end
else
head :no_content
end end
end end
......
...@@ -700,10 +700,22 @@ RSpec.describe Projects::JobsController, :clean_gitlab_redis_shared_state do ...@@ -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 }] expect(json_response['lines']).to eq [{ 'content' => [{ 'text' => 'BUILD TRACE' }], 'offset' => 0 }]
end end
it 'sets being-watched flag for the job' do context 'when job is running' do
expect(response).to have_gitlab_http_status(:ok) 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
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