2017-09-21 04:34:12 -04:00
|
|
|
module Ci
|
|
|
|
class JobArtifact < ActiveRecord::Base
|
2017-12-08 04:09:06 -05:00
|
|
|
include AfterCommitQueue
|
2018-02-21 11:43:21 -05:00
|
|
|
include ObjectStorage::BackgroundMove
|
2017-09-21 04:34:12 -04:00
|
|
|
extend Gitlab::Ci::Model
|
|
|
|
|
|
|
|
belongs_to :project
|
2017-11-02 14:38:25 -04:00
|
|
|
belongs_to :job, class_name: "Ci::Build", foreign_key: :job_id
|
2017-09-21 04:34:12 -04:00
|
|
|
|
2018-04-13 04:20:07 -04:00
|
|
|
mount_uploader :file, JobArtifactUploader
|
|
|
|
|
2017-09-21 04:34:12 -04:00
|
|
|
before_save :set_size, if: :file_changed?
|
2018-03-20 19:03:50 -04:00
|
|
|
after_save :update_project_statistics_after_save, if: :size_changed?
|
|
|
|
after_destroy :update_project_statistics_after_destroy, unless: :project_destroyed?
|
2017-09-21 04:34:12 -04:00
|
|
|
|
2018-05-01 16:27:54 -04:00
|
|
|
after_save :update_file_store, if: :file_changed?
|
2017-09-21 04:34:12 -04:00
|
|
|
|
2018-04-13 04:20:07 -04:00
|
|
|
scope :with_files_stored_locally, -> { where(file_store: [nil, ::JobArtifactUploader::Store::LOCAL]) }
|
2017-12-08 04:09:06 -05:00
|
|
|
|
2018-02-21 11:43:21 -05:00
|
|
|
delegate :exists?, :open, to: :file
|
2018-02-06 09:18:32 -05:00
|
|
|
|
2017-09-21 04:34:12 -04:00
|
|
|
enum file_type: {
|
|
|
|
archive: 1,
|
2018-02-06 09:18:32 -05:00
|
|
|
metadata: 2,
|
|
|
|
trace: 3
|
2017-09-21 04:34:12 -04:00
|
|
|
}
|
|
|
|
|
2018-04-03 12:47:33 -04:00
|
|
|
def update_file_store
|
2018-04-13 04:20:07 -04:00
|
|
|
# The file.object_store is set during `uploader.store!`
|
|
|
|
# which happens after object is inserted/updated
|
|
|
|
self.update_column(:file_store, file.object_store)
|
2018-04-03 12:47:33 -04:00
|
|
|
end
|
|
|
|
|
2017-09-21 04:34:12 -04:00
|
|
|
def self.artifacts_size_for(project)
|
|
|
|
self.where(project: project).sum(:size)
|
|
|
|
end
|
|
|
|
|
2018-02-21 11:43:21 -05:00
|
|
|
def local_store?
|
|
|
|
[nil, ::JobArtifactUploader::Store::LOCAL].include?(self.file_store)
|
|
|
|
end
|
|
|
|
|
2017-12-03 06:02:11 -05:00
|
|
|
def expire_in
|
|
|
|
expire_at - Time.now if expire_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def expire_in=(value)
|
|
|
|
self.expire_at =
|
|
|
|
if value
|
|
|
|
ChronicDuration.parse(value)&.seconds&.from_now
|
|
|
|
end
|
|
|
|
end
|
2018-03-20 19:03:50 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_size
|
|
|
|
self.size = file.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_project_statistics_after_save
|
|
|
|
update_project_statistics(size.to_i - size_was.to_i)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_project_statistics_after_destroy
|
|
|
|
update_project_statistics(-self.size)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_project_statistics(difference)
|
|
|
|
ProjectStatistics.increment_statistic(project_id, :build_artifacts_size, difference)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_destroyed?
|
|
|
|
# Use job.project to avoid extra DB query for project
|
|
|
|
job.project.pending_delete?
|
|
|
|
end
|
2017-09-21 04:34:12 -04:00
|
|
|
end
|
|
|
|
end
|