## Description This is the first PR in a series of PRs for Introducing unread counts in the sidebar for inboxes and labels. In this PR: * Added the unread store, counter and builder modules * Added redis keys for unread count management * Added specs for all 3 modules, some specs are for testing enterprise only feature like specific roles and permissions which are added in the respective enterprise folder itself. **Note** None of this changes affect anything else and nothing is wired to existing modules. Issue: https://linear.app/chatwoot/issue/CW-6851/support-unread-conversation-counts ## Type of change Please delete options that are not relevant. - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality not to work as expected) - [ ] This change requires a documentation update ## How Has This Been Tested? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration. ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Sojan Jose <sojan@pepalo.com>
76 lines
2.2 KiB
Ruby
76 lines
2.2 KiB
Ruby
# Redis::LockManager provides a simple mechanism to handle distributed locks using Redis.
|
|
# This class ensures that only one instance of a given operation runs at a given time across all processes/nodes.
|
|
# It uses the $alfred Redis namespace for all its operations.
|
|
#
|
|
# Example Usage:
|
|
#
|
|
# lock_manager = Redis::LockManager.new
|
|
#
|
|
# if lock_manager.lock("some_key")
|
|
# # Critical code that should not be run concurrently
|
|
# lock_manager.unlock("some_key")
|
|
# end
|
|
#
|
|
class Redis::LockManager
|
|
# Default lock timeout set to 1 second. This means that if the lock isn't released
|
|
# within 1 second, it will automatically expire.
|
|
# This helps to avoid deadlocks in case the process holding the lock crashes or fails to release it.
|
|
LOCK_TIMEOUT = 1.second
|
|
|
|
# Attempts to acquire a lock for the given key.
|
|
#
|
|
# If the lock is successfully acquired, the method returns true. If the key is
|
|
# already locked or if any other error occurs, it returns false.
|
|
#
|
|
# === Parameters
|
|
# * +key+ - The key for which the lock is to be acquired.
|
|
# * +timeout+ - Duration in seconds for which the lock is valid. Defaults to +LOCK_TIMEOUT+.
|
|
#
|
|
# === Returns
|
|
# * +true+ if the lock was successfully acquired.
|
|
# * +false+ if the lock was not acquired.
|
|
def lock(key, timeout = LOCK_TIMEOUT)
|
|
value = Time.now.to_f.to_s
|
|
# nx: true means set the key only if it does not exist
|
|
Redis::Alfred.set(key, value, nx: true, ex: timeout) ? true : false
|
|
end
|
|
|
|
# Releases a lock for the given key.
|
|
#
|
|
# === Parameters
|
|
# * +key+ - The key for which the lock is to be released.
|
|
#
|
|
# === Returns
|
|
# * +true+ indicating the lock release operation was initiated.
|
|
#
|
|
# Note: If the key wasn't locked, this operation will have no effect.
|
|
def unlock(key)
|
|
Redis::Alfred.delete(key)
|
|
true
|
|
end
|
|
|
|
def with_lock(key, timeout = LOCK_TIMEOUT)
|
|
return false unless lock(key, timeout)
|
|
|
|
begin
|
|
yield
|
|
ensure
|
|
unlock(key)
|
|
end
|
|
|
|
true
|
|
end
|
|
|
|
# Checks if the given key is currently locked.
|
|
#
|
|
# === Parameters
|
|
# * +key+ - The key to check.
|
|
#
|
|
# === Returns
|
|
# * +true+ if the key is locked.
|
|
# * +false+ otherwise.
|
|
def locked?(key)
|
|
Redis::Alfred.exists?(key)
|
|
end
|
|
end
|