Some checks failed
Lock Threads / action (push) Has been cancelled
Mark stale issues and pull requests / stale (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot EE docker images / merge (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot CE docker images / merge (push) Has been cancelled
Run Chatwoot CE spec / lint-backend (push) Has been cancelled
Run Chatwoot CE spec / lint-frontend (push) Has been cancelled
Run Chatwoot CE spec / frontend-tests (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (0, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (1, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (10, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (11, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (12, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (13, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (14, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (15, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (2, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (3, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (4, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (5, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (6, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (7, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (8, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (9, 16) (push) Has been cancelled
Run Linux nightly installer / nightly (push) Has been cancelled
- Add Logistics component with progress tracking - Add OrderDetail component for order information - Support data-driven steps and actions - Add blue color scale to widget SCSS - Fix node overflow and progress bar rendering issues - Add English translations for dashboard components Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
64 lines
2.0 KiB
Ruby
64 lines
2.0 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
|
|
|
|
# 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
|