chore: Remove stale contact presence records (#5205)

fixes: chatwoot/product#493
This commit is contained in:
Sojan Jose
2022-08-05 12:51:39 +02:00
committed by GitHub
parent 124390a019
commit 8e75b3fc2a
3 changed files with 34 additions and 4 deletions

View File

@@ -39,7 +39,11 @@ module OnlineStatusTracker
end
def self.get_available_contact_ids(account_id)
::Redis::Alfred.zrangebyscore(presence_key(account_id, 'Contact'), (Time.zone.now - PRESENCE_DURATION).to_i, Time.now.to_i)
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}")
::Redis::Alfred.zrangebyscore(presence_key(account_id, 'Contact'), range_start, '+inf')
end
def self.get_available_contacts(account_id)
@@ -58,9 +62,8 @@ module OnlineStatusTracker
def self.get_available_user_ids(account_id)
account = Account.find(account_id)
# TODO: to migrate this to zrange as its is being deprecated
# https://redis.io/commands/zrangebyscore/
user_ids = ::Redis::Alfred.zrangebyscore(presence_key(account_id, 'User'), (Time.zone.now - PRESENCE_DURATION).to_i, Time.now.to_i)
range_start = (Time.zone.now - PRESENCE_DURATION).to_i
user_ids = ::Redis::Alfred.zrangebyscore(presence_key(account_id, 'User'), range_start, '+inf')
# since we are dealing with redis items as string, casting to string
user_ids += account.account_users.where(auto_offline: false)&.map(&:user_id)&.map(&:to_s)
user_ids.uniq

View File

@@ -89,5 +89,11 @@ module Redis::Alfred
def zrangebyscore(key, range_start, range_end)
$alfred.zrangebyscore(key, range_start, range_end)
end
# remove values by score
# exclusive score is specified by prefixing (
def zremrangebyscore(key, range_start, range_end)
$alfred.zremrangebyscore(key, range_start, range_end)
end
end
end