Class: ResourceAccessTokens::InactiveTokensDeletionCronWorker

Inherits:
Object
  • Object
show all
Includes:
ApplicationWorker, CronjobQueue, Gitlab::Utils::StrongMemoize
Defined in:
app/workers/resource_access_tokens/inactive_tokens_deletion_cron_worker.rb

Constant Summary collapse

BATCH_SIZE =
1000
MAX_RUNTIME =
3.minutes

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_CONCURRENCY_LIMIT_PERCENTAGE_BY_URGENCY, WorkerAttributes::DEFAULT_DATA_CONSISTENCY, WorkerAttributes::DEFAULT_DATA_CONSISTENCY_PER_DB, WorkerAttributes::DEFAULT_DEFER_DELAY, WorkerAttributes::LOAD_BALANCED_DATA_CONSISTENCIES, 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(cursor = nil) ⇒ Object



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
# File 'app/workers/resource_access_tokens/inactive_tokens_deletion_cron_worker.rb', line 16

def perform(cursor = nil)
  cut_off =
    Gitlab::CurrentSettings.inactive_resource_access_tokens_delete_after_days&.days&.ago
  return unless cut_off

  runtime_limiter = Gitlab::Metrics::RuntimeLimiter.new(MAX_RUNTIME)

  # rubocop:disable CodeReuse/ActiveRecord -- each_batch
  User.project_bot.where('"users"."id" > ?', cursor || 0).each_batch(of: BATCH_SIZE) do |relation|
    project_bot_users_whose_all_tokens_became_inactive_before_cut_off_date_or_without_tokens =
      relation
        .select(:id, :username, :organization_id)
        .where(
          'NOT EXISTS (?)',
          PersonalAccessToken
            .select(1)
            .where('"personal_access_tokens"."user_id" = "users"."id"')
            .and(
              PersonalAccessToken.active
                .or(
                  PersonalAccessToken.expired_after(cut_off).or(PersonalAccessToken.revoked_after(cut_off))
                )
            )
        )

    initiate_deletion_for(project_bot_users_whose_all_tokens_became_inactive_before_cut_off_date_or_without_tokens)

    if runtime_limiter.over_time? # rubocop:disable Style/Next -- we must break iteration
      self.class.perform_in(2.minutes, relation.maximum(:id))

      break
    end
  end
  # rubocop:enable CodeReuse/ActiveRecord -- each_batch
end