Class: KmsKeyRotation::BatchInitiatorJob

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
app/sidekiq/kms_key_rotation/batch_initiator_job.rb

Constant Summary collapse

MAX_RECORDS_PER_BATCH =
15_000_000
MAX_RECORDS_PER_JOB =
100
MODELS_FOR_QUERY =
{
  'ClaimsApi::V2::AutoEstablishedClaim' => ClaimsApi::AutoEstablishedClaim
}.freeze

Instance Method Summary collapse

Instance Method Details

#gids_for_model(model, max_records_per_batch) ⇒ Object (private)



43
44
45
46
47
48
49
50
51
52
# File 'app/sidekiq/kms_key_rotation/batch_initiator_job.rb', line 43

def gids_for_model(model, max_records_per_batch)
  model = MODELS_FOR_QUERY[model.name] if MODELS_FOR_QUERY.key?(model.name)

  model
    # Exclude records with the current KMS version
    .where.not('encrypted_kms_key LIKE ?', "v#{KmsEncryptedModelPatch.kms_version}:%")
    .limit(max_records_per_batch)
    .pluck(model.primary_key)
    .map { |id| URI::GID.build(app: GlobalID.app, model_name: model.name, model_id: id).to_s }
end

#modelsObject (private)



39
40
41
# File 'app/sidekiq/kms_key_rotation/batch_initiator_job.rb', line 39

def models
  @models ||= ApplicationRecord.descendants_using_encryption.map(&:name).map(&:constantize)
end

#perform(max_records_per_job = MAX_RECORDS_PER_JOB, max_records_per_batch = MAX_RECORDS_PER_BATCH) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/sidekiq/kms_key_rotation/batch_initiator_job.rb', line 16

def perform(max_records_per_job = MAX_RECORDS_PER_JOB, max_records_per_batch = MAX_RECORDS_PER_BATCH)
  records_enqueued = 0

  models.each do |model|
    if records_enqueued >= max_records_per_batch
      Rails.logger.info("Maximum enqueued #{records_enqueued} records for key rotation reached. Stopping.")
      break
    end

    Rails.logger.info("Enqueuing #{model} records for key rotation. #{records_enqueued} records enqueued so far")

    gids = gids_for_model(model, max_records_per_batch)

    gids.each_slice(max_records_per_job) do |slice|
      KmsKeyRotation::RotateKeysJob.perform_async(slice)
    end

    records_enqueued += gids.size
  end
end