diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-05-14 00:08:46 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-05-14 00:08:46 +0300 |
commit | 8d9963a8e3ece091a98c8e96297d5229c31d7548 (patch) | |
tree | 535f3d3be801ba844431f33a712d1fc182a7637d /app/workers/projects | |
parent | 9adada1187b920547a57b4bb406c3e3c55436bf1 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/workers/projects')
-rw-r--r-- | app/workers/projects/inactive_projects_deletion_cron_worker.rb | 73 | ||||
-rw-r--r-- | app/workers/projects/inactive_projects_deletion_notification_worker.rb | 31 |
2 files changed, 104 insertions, 0 deletions
diff --git a/app/workers/projects/inactive_projects_deletion_cron_worker.rb b/app/workers/projects/inactive_projects_deletion_cron_worker.rb new file mode 100644 index 00000000000..2c3f4191502 --- /dev/null +++ b/app/workers/projects/inactive_projects_deletion_cron_worker.rb @@ -0,0 +1,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 diff --git a/app/workers/projects/inactive_projects_deletion_notification_worker.rb b/app/workers/projects/inactive_projects_deletion_notification_worker.rb new file mode 100644 index 00000000000..0bf808fd753 --- /dev/null +++ b/app/workers/projects/inactive_projects_deletion_notification_worker.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module Projects + class InactiveProjectsDeletionNotificationWorker + include ApplicationWorker + include ExceptionBacktrace + + idempotent! + data_consistency :sticky + sidekiq_options retry: 3 + feature_category :compliance_management + + def perform(project_id, deletion_date) + return if Gitlab::InactiveProjectsDeletionWarningTracker.new(project_id).notified? + + project = Project.find(project_id) + + notification_service.inactive_project_deletion_warning(project, deletion_date) + + Gitlab::InactiveProjectsDeletionWarningTracker.new(project_id).mark_notified + rescue ActiveRecord::RecordNotFound => error + Gitlab::ErrorTracking.log_exception(error, project_id: project_id) + end + + private + + def notification_service + @notification_service ||= NotificationService.new + end + end +end |