Commit 70d015d1 authored by Shinya Maeda's avatar Shinya Maeda Committed by Alessio Caiazza

Cleanup drop_stale_scheduled_builds code

parent a220e72c
...@@ -10,6 +10,7 @@ class StuckCiJobsWorker ...@@ -10,6 +10,7 @@ class StuckCiJobsWorker
BUILD_PENDING_OUTDATED_TIMEOUT = 1.day BUILD_PENDING_OUTDATED_TIMEOUT = 1.day
BUILD_SCHEDULED_OUTDATED_TIMEOUT = 1.hour BUILD_SCHEDULED_OUTDATED_TIMEOUT = 1.hour
BUILD_PENDING_STUCK_TIMEOUT = 1.hour BUILD_PENDING_STUCK_TIMEOUT = 1.hour
BUILD_SCHEDULED_OUTDATED_BATCH_SIZE = 100
def perform def perform
return unless try_obtain_lease return unless try_obtain_lease
...@@ -68,8 +69,12 @@ class StuckCiJobsWorker ...@@ -68,8 +69,12 @@ class StuckCiJobsWorker
# `ci_builds` table has a partial index on `id` with `scheduled_at <> NULL` condition. # `ci_builds` table has a partial index on `id` with `scheduled_at <> NULL` condition.
# Therefore this query's first step uses Index Search, and the following expensive # Therefore this query's first step uses Index Search, and the following expensive
# filter `scheduled_at < ?` will only perform on a small subset (max: 100 rows) # filter `scheduled_at < ?` will only perform on a small subset (max: 100 rows)
Ci::Build.include(EachBatch).where('scheduled_at IS NOT NULL').each_batch(of: 100) do |relation| Ci::Build.include(EachBatch)
relation.where('scheduled_at < ?', BUILD_SCHEDULED_OUTDATED_TIMEOUT.ago).find_each do |build| .where('scheduled_at IS NOT NULL')
.each_batch(of: BUILD_SCHEDULED_OUTDATED_BATCH_SIZE) do |relation|
relation
.where('scheduled_at < ?', BUILD_SCHEDULED_OUTDATED_TIMEOUT.ago)
.find_each(batch_size: BUILD_SCHEDULED_OUTDATED_BATCH_SIZE) do |build|
drop_build(:outdated, build, :scheduled, BUILD_SCHEDULED_OUTDATED_TIMEOUT, :schedule_expired) drop_build(:outdated, build, :scheduled, BUILD_SCHEDULED_OUTDATED_TIMEOUT, :schedule_expired)
end end
end end
......
...@@ -189,4 +189,26 @@ class ScheduledJobFixture ...@@ -189,4 +189,26 @@ class ScheduledJobFixture
def cancel_pipeline def cancel_pipeline
Ci::Pipeline.last.cancel_running Ci::Pipeline.last.cancel_running
end end
def create_stale_scheduled_builds
count = 100
rows = []
last_pipeline = Ci::Pipeline.last
last_stage = last_pipeline.stages.last
count.times do |i|
rows << {
name: "delayed-job-bulk-#{i}",
project_id: project.id,
commit_id: last_pipeline.id,
status: 'scheduled',
scheduled_at: 1.day.ago,
user_id: user.id,
stage_id: last_stage.id,
type: 'Ci::Build'
}
end
Gitlab::Database.bulk_insert('ci_builds', rows)
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