Class: Authn::DataRetention::OauthAccessGrantArchiveWorker

Inherits:
Object
  • Object
show all
Includes:
ApplicationWorker, CronjobQueue
Defined in:
app/workers/authn/data_retention/oauth_access_grant_archive_worker.rb

Constant Summary collapse

MAX_RUNTIME =
3.minutes
REQUEUE_DELAY =
3.minutes
ITERATION_DELAY =
0.1
BATCH_SIZE =
10_000
SUB_BATCH_SIZE =
1_000
CUTOFF_INTERVAL =
1.month

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



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
# File 'app/workers/authn/data_retention/oauth_access_grant_archive_worker.rb', line 23

def perform(cursor = nil)
  return unless Gitlab::CurrentSettings.authn_data_retention_cleanup_enabled?
  return unless Feature.enabled?(:archive_revoked_access_grants, :instance)

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

  # rubocop: disable CodeReuse/ActiveRecord -- We don't want to expose this as a scope until we can provide an index.
  OauthAccessGrant.where('id > ?', cursor.to_i).each_batch(of: BATCH_SIZE) do |batch|
    batch.each_batch(of: SUB_BATCH_SIZE) do |sub_batch|
      old_revoked_grants = sub_batch
                             .where(revoked_at: ..cutoff_date)
                             .select(:id)
      # rubocop: enable CodeReuse/ActiveRecord
      sub_batch_count = old_revoked_grants.delete_all
      log_sub_batch_deleted(sub_batch_count)

      total_deleted_count += sub_batch_count

      sleep ITERATION_DELAY
    end

    if runtime_limiter.over_time?
      self.class.perform_in(REQUEUE_DELAY, batch.last.id)
      break
    end
  end

  (:result, {
    over_time: runtime_limiter.was_over_time?,
    total_deleted: total_deleted_count,
    cutoff_date: cutoff_date
  })
end