fix: RemoveStaleRedisKeys service (#10562)

Fixes issues with RemoveStaleRedisKeys service

Co-authored-by: Sojan <sojan@pepalo.com>
This commit is contained in:
Vishnu Narayanan
2024-12-11 01:08:25 +05:30
committed by GitHub
parent 1b0e94ec95
commit 9a405d65ba
6 changed files with 61 additions and 7 deletions

View File

@@ -6,6 +6,7 @@ class Internal::ProcessStaleRedisKeysJob < ApplicationJob
queue_as :low
def perform(account)
Internal::RemoveStaleRedisKeysService.new(account_id: account.id).perform
removed_count = Internal::RemoveStaleRedisKeysService.new(account_id: account.id).perform
Rails.logger.info "Successfully cleaned up Redis keys for account #{account.id} (removed #{removed_count} keys)"
end
end

View File

@@ -6,9 +6,11 @@ class Internal::RemoveStaleRedisKeysJob < ApplicationJob
queue_as :scheduled_jobs
def perform
Account.find_each do |account|
Rails.logger.info "Enqueuing ProcessStaleRedisKeysJob for account #{account.id}"
Internal::ProcessStaleRedisKeysJob.perform_later(account)
Account.find_in_batches(batch_size: 100) do |accounts|
accounts.each do |account|
Rails.logger.info "Enqueuing ProcessStaleRedisKeysJob for account #{account.id}"
Internal::ProcessStaleRedisKeysJob.perform_later(account)
end
end
end
end

View File

@@ -1,10 +1,15 @@
class Internal::RemoveStaleRedisKeysService
pattr_initialize [:account_id!]
def perform(account_id)
range_start = (Time.zone.now - PRESENCE_DURATION).to_i
def perform
Rails.logger.info "Removing redis stale keys for account #{@account_id}"
range_start = (Time.zone.now - OnlineStatusTracker::PRESENCE_DURATION).to_i
# exclusive minimum score is specified by prefixing (
# we are clearing old records because this could clogg up the sorted set
::Redis::Alfred.zremrangebyscore(presence_key(account_id, 'Contact'), '-inf', "(#{range_start}")
::Redis::Alfred.zremrangebyscore(
OnlineStatusTracker.presence_key(@account_id, 'Contact'),
'-inf',
"(#{range_start}"
)
end
end