feat: remove stale ONLINE_PRESENCE contact keys in redis (#9558)
50% of Redis memory size comes from ONLINE_PRESENCE keys. This PR adds a periodic job to remove stale keys from all accounts.
This commit is contained in:
11
app/jobs/internal/process_stale_redis_keys_job.rb
Normal file
11
app/jobs/internal/process_stale_redis_keys_job.rb
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# housekeeping
|
||||||
|
# remove contact inboxes that does not have any conversations
|
||||||
|
# and are older than 3 months
|
||||||
|
|
||||||
|
class Internal::ProcessStaleRedisKeysJob < ApplicationJob
|
||||||
|
queue_as :low
|
||||||
|
|
||||||
|
def perform(account)
|
||||||
|
Internal::RemoveStaleRedisKeysService.new(account_id: account.id).perform
|
||||||
|
end
|
||||||
|
end
|
||||||
14
app/jobs/internal/remove_stale_redis_keys_job.rb
Normal file
14
app/jobs/internal/remove_stale_redis_keys_job.rb
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
# housekeeping
|
||||||
|
# ensure stale ONLINE PRESENCE KEYS for contacts are removed periodically
|
||||||
|
# should result in 50% redis mem size reduction
|
||||||
|
|
||||||
|
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)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
10
app/services/internal/remove_stale_redis_keys_service.rb
Normal file
10
app/services/internal/remove_stale_redis_keys_service.rb
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
class Internal::RemoveStaleRedisKeysService
|
||||||
|
pattr_initialize [:account_id!]
|
||||||
|
|
||||||
|
def perform(account_id)
|
||||||
|
range_start = (Time.zone.now - 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}")
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -26,3 +26,10 @@ remove_stale_contact_inboxes_job.rb:
|
|||||||
cron: '30 22 * * *'
|
cron: '30 22 * * *'
|
||||||
class: 'Internal::RemoveStaleContactInboxesJob'
|
class: 'Internal::RemoveStaleContactInboxesJob'
|
||||||
queue: scheduled_jobs
|
queue: scheduled_jobs
|
||||||
|
|
||||||
|
# executed daily at 2230 UTC
|
||||||
|
# which is our lowest traffic time
|
||||||
|
remove_stale_redis_keys_job.rb:
|
||||||
|
cron: '30 22 * * *'
|
||||||
|
class: 'Internal::RemoveStaleRedisKeysJob'
|
||||||
|
queue: scheduled_jobs
|
||||||
|
|||||||
Reference in New Issue
Block a user