Welcome to mirror list, hosted at ThFree Co, Russian Federation.

inactive_projects_deletion_cron_worker.rb « projects « workers « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2c3f4191502d822b3d58d3f488af474f2c688ecd (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# frozen_string_literal: true

module Projects
  class InactiveProjectsDeletionCronWorker
    include ApplicationWorker
    include Gitlab::Utils::StrongMemoize
    include CronjobQueue

    idempotent!
    data_consistency :always
    feature_category :compliance_management

    INTERVAL = 2.seconds.to_i

    def perform
      return unless ::Gitlab::CurrentSettings.delete_inactive_projects?

      admin_user = User.admins.active.first

      return unless admin_user

      notified_inactive_projects = Gitlab::InactiveProjectsDeletionWarningTracker.notified_projects

      Project.inactive.without_deleted.find_each(batch_size: 100).with_index do |project, index| # rubocop: disable CodeReuse/ActiveRecord
        next unless Feature.enabled?(:inactive_projects_deletion, project.root_namespace)

        delay = index * INTERVAL

        with_context(project: project, user: admin_user) do
          deletion_warning_email_sent_on = notified_inactive_projects["project:#{project.id}"]

          if send_deletion_warning_email?(deletion_warning_email_sent_on, project)
            send_notification(delay, project, admin_user)
          elsif deletion_warning_email_sent_on && delete_due_to_inactivity?(deletion_warning_email_sent_on)
            Gitlab::InactiveProjectsDeletionWarningTracker.new(project.id).reset
            delete_project(project, admin_user)
          end
        end
      end
    end

    private

    def grace_months_after_deletion_notification
      strong_memoize(:grace_months_after_deletion_notification) do
        (::Gitlab::CurrentSettings.inactive_projects_delete_after_months -
          ::Gitlab::CurrentSettings.inactive_projects_send_warning_email_after_months).months
      end
    end

    def send_deletion_warning_email?(deletion_warning_email_sent_on, project)
      deletion_warning_email_sent_on.blank?
    end

    def delete_due_to_inactivity?(deletion_warning_email_sent_on)
      deletion_warning_email_sent_on < grace_months_after_deletion_notification.ago
    end

    def deletion_date
      grace_months_after_deletion_notification.from_now.to_date.to_s
    end

    def delete_project(project, user)
      ::Projects::DestroyService.new(project, user, {}).async_execute
    end

    def send_notification(delay, project, user)
      ::Projects::InactiveProjectsDeletionNotificationWorker.perform_in(delay, project.id, deletion_date)
    end
  end
end

Projects::InactiveProjectsDeletionCronWorker.prepend_mod