Commit 2084e7ab authored by Shinya Maeda's avatar Shinya Maeda

Move find_builds_from_stale_live_traces method to Ci::Build

parent 40644815
...@@ -583,6 +583,21 @@ module Ci ...@@ -583,6 +583,21 @@ module Ci
super(options).merge(when: read_attribute(:when)) super(options).merge(when: read_attribute(:when))
end end
# Find stale live traces and return their build ids
def self.find_builds_from_stale_live_traces
binding.pry
Ci::BuildTraceChunk
.include(EachBatch).select(:build_id).group(:build_id).joins(:build)
.merge(Ci::Build.finished).where('ci_builds.finished_at < ?', 1.hour.ago)
.each_batch(column: :build_id) do |chunks|
build_ids = chunks.map { |chunk| chunk.build_id }
binding.pry
yield where(id: build_ids)
end
end
private private
def update_artifacts_size def update_artifacts_size
......
...@@ -50,21 +50,6 @@ module Ci ...@@ -50,21 +50,6 @@ module Ci
def finalize_fast_destroy(keys) def finalize_fast_destroy(keys)
redis_delete_data(keys) redis_delete_data(keys)
end end
# Find stale live traces and return their build ids
def find_builds_from_stale_live_trace
include(EachBatch)
.select(:build_id)
.group(:build_id)
.joins(:build)
.merge(Ci::Build.finished)
.where('ci_builds.finished_at < ?', 1.hour.ago)
.each_batch(column: :build_id) do |chunks|
build_ids = chunks.map { |chunk| chunk.build_id }
yield build_ids
end
end
end end
## ##
......
...@@ -7,12 +7,12 @@ module Ci ...@@ -7,12 +7,12 @@ module Ci
# Archive live traces which still resides in redis or database # Archive live traces which still resides in redis or database
# This could happen when sidekiq-jobs for archivements are lost by SIGKILL # This could happen when sidekiq-jobs for archivements are lost by SIGKILL
# Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/36791 # Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/36791
Ci::BuildTraceChunk.find_builds_from_stale_live_trace do |build_ids| Ci::Build.find_builds_from_stale_live_traces do |builds|
Ci::Build.where(id: build_ids).find_each do |build| builds.each do |build|
begin begin
build.trace.archive! build.trace.archive!
rescue => e rescue => e
Rails.logger.info "Failed to archive stale live trace. id: #{build.id} message: #{e.message}" Rails.logger.error "Failed to archive stale live trace. id: #{build.id} message: #{e.message}"
end end
end end
end end
......
...@@ -35,8 +35,8 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do ...@@ -35,8 +35,8 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do
end end
end end
describe '.find_builds_from_stale_live_trace' do describe '.find_builds_from_stale_live_traces' do
subject { described_class.find_builds_from_stale_live_trace } subject { described_class.find_builds_from_stale_live_traces }
context 'when build status is finished' do context 'when build status is finished' do
context 'when build finished 2 days ago' do context 'when build finished 2 days ago' do
...@@ -44,7 +44,7 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do ...@@ -44,7 +44,7 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do
let!(:build) { create(:ci_build, :success, :trace_artifact, finished_at: 2.days.ago) } let!(:build) { create(:ci_build, :success, :trace_artifact, finished_at: 2.days.ago) }
it 'does not yield build id' do it 'does not yield build id' do
expect { |b| described_class.find_builds_from_stale_live_trace(&b) }.not_to yield_control expect { |b| described_class.find_builds_from_stale_live_traces(&b) }.not_to yield_control
end end
end end
...@@ -52,7 +52,7 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do ...@@ -52,7 +52,7 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do
let!(:build) { create(:ci_build, :success, :trace_live, finished_at: 2.days.ago) } let!(:build) { create(:ci_build, :success, :trace_live, finished_at: 2.days.ago) }
it 'yields build id' do it 'yields build id' do
expect { |b| described_class.find_builds_from_stale_live_trace(&b) }.to yield_with_args([build.id]) expect { |b| described_class.find_builds_from_stale_live_traces(&b) }.to yield_with_args([build.id])
end end
end end
end end
...@@ -61,7 +61,7 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do ...@@ -61,7 +61,7 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do
let!(:build) { create(:ci_build, :success, :trace_live, finished_at: 10.minutes.ago) } let!(:build) { create(:ci_build, :success, :trace_live, finished_at: 10.minutes.ago) }
it 'does not yield build id' do it 'does not yield build id' do
expect { |b| described_class.find_builds_from_stale_live_trace(&b) }.not_to yield_control expect { |b| described_class.find_builds_from_stale_live_traces(&b) }.not_to yield_control
end end
end end
end end
...@@ -70,7 +70,7 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do ...@@ -70,7 +70,7 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do
let!(:build) { create(:ci_build, :running, :trace_live) } let!(:build) { create(:ci_build, :running, :trace_live) }
it 'does not yield build id' do it 'does not yield build id' do
expect { |b| described_class.find_builds_from_stale_live_trace(&b) }.not_to yield_control expect { |b| described_class.find_builds_from_stale_live_traces(&b) }.not_to yield_control
end end
end end
end end
......
...@@ -11,6 +11,7 @@ describe Ci::RescueStaleLiveTraceWorker do ...@@ -11,6 +11,7 @@ describe Ci::RescueStaleLiveTraceWorker do
it do it do
subject subject
build.reload
expect(build.job_artifacts_trace).to be_exist expect(build.job_artifacts_trace).to be_exist
end end
end end
...@@ -33,16 +34,16 @@ describe Ci::RescueStaleLiveTraceWorker do ...@@ -33,16 +34,16 @@ describe Ci::RescueStaleLiveTraceWorker do
it_behaves_like 'archives trace' it_behaves_like 'archives trace'
context 'when build has both archived trace and live trace' do # context 'when build has both archived trace and live trace' do
let!(:build2) { create(:ci_build, :success, :trace_live, finished_at: 2.days.ago) } # let!(:build2) { create(:ci_build, :success, :trace_live, finished_at: 2.days.ago) }
it 'archives only available targets' do # it 'archives only available targets' do
subject # subject
build.reload # build.reload
expect(build.job_artifacts_trace).to be_exist # expect(build.job_artifacts_trace).to be_exist
end # end
end # end
end end
context 'when a job was failed 2 hours ago' do context 'when a job was failed 2 hours ago' do
......
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