Class: PersonalAccessTokens::ExpiringWorker

Inherits:
Object
  • Object
show all
Includes:
ApplicationWorker, CronjobQueue
Defined in:
app/workers/personal_access_tokens/expiring_worker.rb

Overview

rubocop:disable Scalability/IdempotentWorker

Constant Summary collapse

MAX_TOKENS =
100

Constants included from ApplicationWorker

ApplicationWorker::LOGGING_EXTRA_KEY, ApplicationWorker::SAFE_PUSH_BULK_LIMIT

Constants included from Gitlab::Loggable

Gitlab::Loggable::ANONYMOUS

Constants included from WorkerAttributes

WorkerAttributes::DEFAULT_DATA_CONSISTENCY, WorkerAttributes::DEFAULT_DEFER_DELAY, WorkerAttributes::NAMESPACE_WEIGHTS, WorkerAttributes::VALID_DATA_CONSISTENCIES, WorkerAttributes::VALID_RESOURCE_BOUNDARIES, WorkerAttributes::VALID_URGENCIES

Instance Method Summary collapse

Methods included from Gitlab::Loggable

#build_structured_payload

Methods included from Gitlab::SidekiqVersioning::Worker

#job_version

Methods included from WorkerContext

#with_context

Instance Method Details

#perform(*args) ⇒ Object



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
# File 'app/workers/personal_access_tokens/expiring_worker.rb', line 15

def perform(*args)
  notification_service = NotificationService.new
  limit_date = PersonalAccessToken::DAYS_TO_EXPIRE.days.from_now.to_date

  User.with_expiring_and_not_notified_personal_access_tokens(limit_date).find_each do |user|
    with_context(user: user) do
      expiring_user_tokens = user.personal_access_tokens.without_impersonation.expiring_and_not_notified(limit_date)

      # rubocop: disable CodeReuse/ActiveRecord
      # We never materialise the token instances. We need the names to mention them in the
      # email. Later we trigger an update query on the entire relation, not on individual instances.
      token_names = expiring_user_tokens.limit(MAX_TOKENS).pluck(:name)
      # We're limiting to 100 tokens so we avoid loading too many tokens into memory.
      # At the time of writing this would only affect 69 users on GitLab.com

      # rubocop: enable CodeReuse/ActiveRecord

      message = if user.project_bot?
                  notification_service.resource_access_tokens_about_to_expire(user, token_names)

                  "Notifying Bot User resource owners about expiring tokens"
                else
                  notification_service.access_token_about_to_expire(user, token_names)

                  "Notifying User about expiring tokens"
                end

      Gitlab::AppLogger.info(
        message: message,
        class: self.class,
        user_id: user.id
      )

      expiring_user_tokens.each_batch do |expiring_tokens|
        expiring_tokens.update_all(expire_notification_delivered: true)
      end
    end
  end
end