Class: Ask::TokenUsage::Rails::SweepExpiredTokensJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/ask/token_usage/rails/jobs/sweep_expired_tokens_job.rb

Overview

Expire grant entries whose expires_at has passed. Scheduled as a recurring job (Solid Queue cron, Sidekiq Cron, etc.):

# config/recurring.yml
ask_token_usage_sweep:
class: Ask::TokenUsage::Rails::SweepExpiredTokensJob
schedule: "every 1 hour"

For each expired grant, the actual amount removed is capped to the wallet's current balance (myrr approximation — correct for most grant-heavy usage patterns; perfect accuracy requires FIFO allocation which is a future option).

Instance Method Summary collapse

Instance Method Details

#perform(now: Time.current) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/ask/token_usage/rails/jobs/sweep_expired_tokens_job.rb', line 24

def perform(now: Time.current)
  Ask::TokenUsage::TokenTransaction
    .grants
    .where("expires_at IS NOT NULL AND expires_at <= ?", now)
    .where.not(entry_type: "expiry")
    .where("id NOT IN (SELECT source_transaction_id FROM token_transactions WHERE entry_type = ?)", "expiry")
    .find_each do |grant|
      sweep_grant(grant)
    end
end