summaryrefslogtreecommitdiff
path: root/app/services/container_expiration_policies/cleanup_service.rb
blob: f2bc2beab6334402b89322eb8d02a319a071b630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
52
53
# frozen_string_literal: true

module ContainerExpirationPolicies
  class CleanupService
    attr_reader :repository

    def initialize(repository)
      @repository = repository
    end

    def execute
      return ServiceResponse.error(message: 'no repository') unless repository

      repository.start_expiration_policy!

      result = Projects::ContainerRepository::CleanupTagsService
        .new(project, nil, policy_params.merge('container_expiration_policy' => true))
        .execute(repository)

      if result[:status] == :success
        repository.update!(
          expiration_policy_cleanup_status: :cleanup_unscheduled,
          expiration_policy_started_at: nil
        )
        success(:finished)
      else
        repository.cleanup_unfinished!

        success(:unfinished)
      end
    end

    private

    def success(cleanup_status)
      ServiceResponse.success(message: "cleanup #{cleanup_status}", payload: { cleanup_status: cleanup_status, container_repository_id: repository.id })
    end

    def policy_params
      return {} unless policy

      policy.policy_params
    end

    def policy
      project.container_expiration_policy
    end

    def project
      repository&.project
    end
  end
end