Class: Gitlab::Cleanup::OrphanJobArtifactFilesBatch

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/cleanup/orphan_job_artifact_files_batch.rb

Defined Under Namespace

Classes: ArtifactFile

Constant Summary collapse

BatchFull =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(batch_size:, dry_run: true, logger: Gitlab::AppLogger) ⇒ OrphanJobArtifactFilesBatch

Returns a new instance of OrphanJobArtifactFilesBatch.


25
26
27
28
29
30
# File 'lib/gitlab/cleanup/orphan_job_artifact_files_batch.rb', line 25

def initialize(batch_size:, dry_run: true, logger: Gitlab::AppLogger)
  @batch_size = batch_size
  @dry_run = dry_run
  @logger = logger
  @artifact_files = []
end

Instance Attribute Details

#artifact_filesObject

Returns the value of attribute artifact_files


23
24
25
# File 'lib/gitlab/cleanup/orphan_job_artifact_files_batch.rb', line 23

def artifact_files
  @artifact_files
end

#batch_sizeObject (readonly)

Returns the value of attribute batch_size


22
23
24
# File 'lib/gitlab/cleanup/orphan_job_artifact_files_batch.rb', line 22

def batch_size
  @batch_size
end

#dry_runObject (readonly)

Returns the value of attribute dry_run


22
23
24
# File 'lib/gitlab/cleanup/orphan_job_artifact_files_batch.rb', line 22

def dry_run
  @dry_run
end

Instance Method Details

#<<(artifact_path) ⇒ Object

Raises:


44
45
46
47
48
# File 'lib/gitlab/cleanup/orphan_job_artifact_files_batch.rb', line 44

def <<(artifact_path)
  raise BatchFull, "Batch full! Already contains #{artifact_files.count} artifacts" if full?

  artifact_files << ArtifactFile.new(artifact_path)
end

#clean!Object


32
33
34
35
36
37
38
# File 'lib/gitlab/cleanup/orphan_job_artifact_files_batch.rb', line 32

def clean!
  return if artifact_files.empty?

  lost_and_found.each do |artifact|
    clean_one!(artifact)
  end
end

#full?Boolean

Returns:

  • (Boolean)

40
41
42
# File 'lib/gitlab/cleanup/orphan_job_artifact_files_batch.rb', line 40

def full?
  artifact_files.count >= batch_size
end

#lost_and_foundObject


50
51
52
53
54
55
56
57
# File 'lib/gitlab/cleanup/orphan_job_artifact_files_batch.rb', line 50

def lost_and_found
  strong_memoize(:lost_and_found) do
    artifact_file_ids = artifact_files.map(&:artifact_id)
    existing_artifact_ids = ::Ci::JobArtifact.id_in(artifact_file_ids).pluck_primary_key

    artifact_files.reject { |artifact| existing_artifact_ids.include?(artifact.artifact_id) }
  end
end