Class: Ci::JobArtifacts::DestroyBatchService

Inherits:
Object
  • Object
show all
Includes:
BaseServiceUtility, Gitlab::Utils::StrongMemoize
Defined in:
app/services/ci/job_artifacts/destroy_batch_service.rb

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(job_artifacts, pick_up_at: nil, fix_expire_at: fix_expire_at?) ) ⇒ DestroyBatchService

Adds the passed batch of job artifacts to the `ci_deleted_objects` table for asyncronous destruction of the objects in Object Storage via the `Ci::DeleteObjectsService` and then deletes the batch of related `ci_job_artifacts` records. Params:

job_artifacts

A relation of job artifacts to destroy (fewer than MAX_JOB_ARTIFACT_BATCH_SIZE)

pick_up_at

When to pick up for deletion of files

Returns:

Hash

A hash with status and destroyed_artifacts_count keys


20
21
22
23
24
# File 'app/services/ci/job_artifacts/destroy_batch_service.rb', line 20

def initialize(job_artifacts, pick_up_at: nil, fix_expire_at: fix_expire_at?)
  @job_artifacts = job_artifacts.with_destroy_preloads.to_a
  @pick_up_at = pick_up_at
  @fix_expire_at = fix_expire_at
end

Instance Method Details

#execute(update_stats: true) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/ci/job_artifacts/destroy_batch_service.rb', line 27

def execute(update_stats: true)
  # Detect and fix artifacts that had `expire_at` wrongly backfilled by migration
  # https://gitlab.com/gitlab-org/gitlab/-/merge_requests/47723
  detect_and_fix_wrongly_expired_artifacts

  return success(destroyed_artifacts_count: 0, statistics_updates: {}) if @job_artifacts.empty?

  destroy_related_records(@job_artifacts)

  destroy_around_hook(@job_artifacts) do
    Ci::DeletedObject.transaction do
      Ci::DeletedObject.bulk_import(@job_artifacts, @pick_up_at)
      Ci::JobArtifact.id_in(@job_artifacts.map(&:id)).delete_all
    end
  end

  after_batch_destroy_hook(@job_artifacts)

  # This is executed outside of the transaction because it depends on Redis
  update_project_statistics! if update_stats
  increment_monitoring_statistics(artifacts_count, artifacts_bytes)

  success(destroyed_artifacts_count: artifacts_count,
    statistics_updates: affected_project_statistics)
end