diff --git a/app/jobs/internal/process_stale_redis_keys_job.rb b/app/jobs/internal/process_stale_redis_keys_job.rb index 3afb3b434..30c3cffed 100644 --- a/app/jobs/internal/process_stale_redis_keys_job.rb +++ b/app/jobs/internal/process_stale_redis_keys_job.rb @@ -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 diff --git a/app/jobs/internal/remove_stale_redis_keys_job.rb b/app/jobs/internal/remove_stale_redis_keys_job.rb index 667fa398b..da3892b3c 100644 --- a/app/jobs/internal/remove_stale_redis_keys_job.rb +++ b/app/jobs/internal/remove_stale_redis_keys_job.rb @@ -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 diff --git a/app/services/internal/remove_stale_redis_keys_service.rb b/app/services/internal/remove_stale_redis_keys_service.rb index a3e1da207..553cc6c6a 100644 --- a/app/services/internal/remove_stale_redis_keys_service.rb +++ b/app/services/internal/remove_stale_redis_keys_service.rb @@ -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 diff --git a/spec/jobs/internal/process_stale_redis_keys_job_spec.rb b/spec/jobs/internal/process_stale_redis_keys_job_spec.rb new file mode 100644 index 000000000..6189b1b5c --- /dev/null +++ b/spec/jobs/internal/process_stale_redis_keys_job_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +RSpec.describe Internal::ProcessStaleRedisKeysJob do + let(:account) { create(:account) } + + describe '#perform' do + it 'calls the RemoveStaleRedisKeysService with the correct account ID' do + expect(Internal::RemoveStaleRedisKeysService).to receive(:new) + .with(account_id: account.id) + .and_call_original + + described_class.perform_now(account) + end + end +end diff --git a/spec/jobs/internal/remove_stale_redis_keys_job_spec.rb b/spec/jobs/internal/remove_stale_redis_keys_job_spec.rb new file mode 100644 index 000000000..a1b98c9db --- /dev/null +++ b/spec/jobs/internal/remove_stale_redis_keys_job_spec.rb @@ -0,0 +1,13 @@ +require 'rails_helper' + +RSpec.describe Internal::RemoveStaleRedisKeysJob do + let(:account) { create(:account) } + + describe '#perform' do + it 'enqueues ProcessStaleRedisKeysJob for the account' do + expect(Internal::ProcessStaleRedisKeysJob).to receive(:perform_later).with(account) + + described_class.perform_now + end + end +end diff --git a/spec/services/internal/remove_stale_redis_keys_service_spec.rb b/spec/services/internal/remove_stale_redis_keys_service_spec.rb new file mode 100644 index 000000000..75d182aab --- /dev/null +++ b/spec/services/internal/remove_stale_redis_keys_service_spec.rb @@ -0,0 +1,18 @@ +require 'rails_helper' + +RSpec.describe Internal::RemoveStaleRedisKeysService, type: :service do + let(:account_id) { 1 } + let(:service) { described_class.new(account_id: account_id) } + + describe '#perform' do + it 'removes stale Redis keys for the specified account' do + presence_key = OnlineStatusTracker.presence_key(account_id, 'Contact') + + # Mock Redis calls + expect(Redis::Alfred).to receive(:zremrangebyscore) + .with(presence_key, '-inf', anything) + + service.perform + end + end +end