summaryrefslogtreecommitdiff
path: root/lib/gitlab/inactive_projects_deletion_warning_tracker.rb
blob: 3fdb34d42b7dc2314da2b52ed3950f6473312052 (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
# frozen_string_literal: true

module Gitlab
  class InactiveProjectsDeletionWarningTracker
    include Gitlab::Utils::StrongMemoize

    attr_reader :project_id

    DELETION_TRACKING_REDIS_KEY = 'inactive_projects_deletion_warning_email_notified'

    # Redis key 'inactive_projects_deletion_warning_email_notified' is a hash. It stores the date when the
    # deletion warning notification email was sent for an inactive project. The fields and values look like:
    # {"project:1"=>"2022-04-22", "project:5"=>"2022-04-22", "project:7"=>"2022-04-25"}
    # @return [Hash]
    def self.notified_projects
      Gitlab::Redis::SharedState.with do |redis|
        redis.hgetall(DELETION_TRACKING_REDIS_KEY)
      end
    end

    def self.reset_all
      Gitlab::Redis::SharedState.with do |redis|
        redis.del(DELETION_TRACKING_REDIS_KEY)
      end
    end

    def initialize(project_id)
      @project_id = project_id
    end

    def notified?
      Gitlab::Redis::SharedState.with do |redis|
        redis.hexists(DELETION_TRACKING_REDIS_KEY, "project:#{project_id}")
      end
    end

    def mark_notified
      Gitlab::Redis::SharedState.with do |redis|
        redis.hset(DELETION_TRACKING_REDIS_KEY, "project:#{project_id}", Date.current)
      end
    end

    def notification_date
      Gitlab::Redis::SharedState.with do |redis|
        redis.hget(DELETION_TRACKING_REDIS_KEY, "project:#{project_id}")
      end
    end

    def scheduled_deletion_date
      if notification_date.present?
        (notification_date.to_date + grace_period_after_notification).to_s
      else
        grace_period_after_notification.from_now.to_date.to_s
      end
    end

    def reset
      Gitlab::Redis::SharedState.with do |redis|
        redis.hdel(DELETION_TRACKING_REDIS_KEY, "project:#{project_id}")
      end
    end

    private

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