Commit e36c03cd authored by drew cimino's avatar drew cimino

Add scheduled_at_before scope to CommitStatus

The query we use to get scheduled builds in this service has been
thoroughly reviewed and optimized. We should make the query available
to other parts of the application.
parent c9555383
...@@ -62,6 +62,9 @@ class CommitStatus < Ci::ApplicationRecord ...@@ -62,6 +62,9 @@ class CommitStatus < Ci::ApplicationRecord
scope :updated_before, ->(lookback:, timeout:) { scope :updated_before, ->(lookback:, timeout:) {
where('(ci_builds.created_at BETWEEN ? AND ?) AND (ci_builds.updated_at BETWEEN ? AND ?)', lookback, timeout, lookback, timeout) where('(ci_builds.created_at BETWEEN ? AND ?) AND (ci_builds.updated_at BETWEEN ? AND ?)', lookback, timeout, lookback, timeout)
} }
scope :scheduled_at_before, ->(date) {
where('ci_builds.scheduled_at IS NOT NULL AND ci_builds.scheduled_at < ?', date)
}
# The scope applies `pluck` to split the queries. Use with care. # The scope applies `pluck` to split the queries. Use with care.
scope :for_project_paths, -> (paths) do scope :for_project_paths, -> (paths) do
......
...@@ -16,10 +16,7 @@ module Ci ...@@ -16,10 +16,7 @@ module Ci
private private
def scheduled_timed_out_builds def scheduled_timed_out_builds
Ci::Build.where(status: :scheduled).where( # rubocop: disable CodeReuse/ActiveRecord Ci::Build.scheduled.scheduled_at_before(BUILD_SCHEDULED_OUTDATED_TIMEOUT.ago)
'ci_builds.scheduled_at IS NOT NULL AND ci_builds.scheduled_at < ?',
BUILD_SCHEDULED_OUTDATED_TIMEOUT.ago
)
end end
end end
end end
......
...@@ -123,6 +123,16 @@ RSpec.describe CommitStatus do ...@@ -123,6 +123,16 @@ RSpec.describe CommitStatus do
end end
end end
describe '.scheduled_at_before' do
let!(:never_scheduled) { create(:commit_status) }
let!(:stale_scheduled) { create(:commit_status, scheduled_at: 1.day.ago) }
let!(:fresh_scheduled) { create(:commit_status, scheduled_at: 1.minute.ago) }
subject { CommitStatus.scheduled_at_before(1.hour.ago) }
it { is_expected.to contain_exactly(stale_scheduled) }
end
describe '#processed' do describe '#processed' do
subject { commit_status.processed } subject { commit_status.processed }
......
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