job_artifact.rb 1.78 KB
Newer Older
1
2
module Ci
  class JobArtifact < ActiveRecord::Base
3
    include AfterCommitQueue
4
    include ObjectStorage::BackgroundMove
5
6
7
    extend Gitlab::Ci::Model

    belongs_to :project
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
8
    belongs_to :job, class_name: "Ci::Build", foreign_key: :job_id
9

10
    before_save :update_file_store
11
    before_save :set_size, if: :file_changed?
12
13
    after_save :update_project_statistics_after_save, if: :size_changed?
    after_destroy :update_project_statistics_after_destroy, unless: :project_destroyed?
14

15
    scope :with_files_stored_locally, -> { where(file_store: [nil, ::JobArtifactUploader::Store::LOCAL]) }
16

17
    mount_uploader :file, JobArtifactUploader
18

19
    delegate :exists?, :open, to: :file
20

21
22
    enum file_type: {
      archive: 1,
23
24
      metadata: 2,
      trace: 3
25
26
    }

27
28
29
30
    def update_file_store
      self.file_store = file.object_store
    end

31
32
33
34
    def self.artifacts_size_for(project)
      self.where(project: project).sum(:size)
    end

35
36
37
38
    def local_store?
      [nil, ::JobArtifactUploader::Store::LOCAL].include?(self.file_store)
    end

39
40
41
42
43
44
45
46
47
48
    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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

    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
72
73
  end
end