Skip to content
Snippets Groups Projects
Commit ccea8498 authored by Andrew Cimino's avatar Andrew Cimino
Browse files

Move AR querying logic into AR models

parent 8f35a038
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -164,6 +164,7 @@ def persisted_environment=(environment)
 
scope :with_artifacts_not_expired, -> { with_downloadable_artifacts.where('artifacts_expire_at IS NULL OR artifacts_expire_at > ?', Time.current) }
scope :with_expired_artifacts, -> { with_downloadable_artifacts.where('artifacts_expire_at < ?', Time.current) }
scope :with_pipeline_locked_artifacts, -> { joins(:pipeline).where('pipeline.locked': Ci::Pipeline.lockeds[:artifacts_locked]) }
scope :last_month, -> { where('created_at > ?', Date.today - 1.month) }
scope :manual_actions, -> { where(when: :manual, status: COMPLETED_STATUSES + %i[manual]) }
scope :scheduled_actions, -> { where(when: :delayed, status: COMPLETED_STATUSES + %i[scheduled]) }
Loading
Loading
Loading
Loading
@@ -133,6 +133,7 @@ class JobArtifact < Ci::ApplicationRecord
 
scope :not_expired, -> { where('expire_at IS NULL OR expire_at > ?', Time.current) }
scope :for_sha, ->(sha, project_id) { joins(job: :pipeline).where(ci_pipelines: { sha: sha, project_id: project_id }) }
scope :for_job_ids, ->(job_ids) { where(job_id: job_ids) }
scope :for_job_name, ->(name) { joins(:job).where(ci_builds: { name: name }) }
 
scope :with_job, -> { joins(:job).includes(:job) }
Loading
Loading
@@ -266,6 +267,10 @@ def self.artifacts_size_for(project)
self.where(project: project).sum(:size)
end
 
def self.distinct_job_ids
distinct.pluck(:job_id)
end
##
# FastDestroyAll concerns
# rubocop: disable CodeReuse/ServiceClass
Loading
Loading
Loading
Loading
@@ -54,19 +54,19 @@ def optional_artifacts_backlog_update(service_response)
end
 
def work_on_unknown_artifacts_backlog
build_ids = Ci::JobArtifact.expired_before(@start_at).artifact_unknown.distinct.limit(BATCH_SIZE).pluck(:job_id)
build_ids = Ci::JobArtifact.expired_before(@start_at).artifact_unknown.limit(BATCH_SIZE).distinct_job_ids
 
return false unless build_ids.present?
 
locked_pipeline_build_ids = ::Ci::Build.joins(:pipeline).where(id: build_ids, :'pipeline.locked' => Ci::Pipeline.lockeds[:artifacts_locked]).pluck(:id)
unlocked_pipeline_build_ids = build_ids - locked_pipeline_build_ids # IDs in memory
locked_pipeline_build_ids = ::Ci::Build.with_pipeline_locked_artifacts.id_in(build_ids).pluck_primary_key
unlocked_pipeline_build_ids = build_ids - locked_pipeline_build_ids
 
update_unknown_artifacts(locked_pipeline_build_ids, Ci::JobArtifact.lockeds[:artifacts_locked]) ||
update_unknown_artifacts(unlocked_pipeline_build_ids, Ci::JobArtifact.lockeds[:unlocked])
end
 
def update_unknown_artifacts(build_ids, locked_value)
Ci::JobArtifact.where(job_id: build_ids).update_all(locked: locked_value) if build_ids.any?
Ci::JobArtifact.for_job_ids(build_ids).update_all(locked: locked_value) if build_ids.any?
end
 
def destroy_job_artifacts_with_slow_iteration
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment