-
Notifications
You must be signed in to change notification settings - Fork 18
IVS-876 - Django Management Command to display Redis Locks #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rw-bsi
merged 1 commit into
development
from
IVS-876_Django_Mgmt_Command_for_Redis_locks
Jun 8, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
backend/apps/ifc_validation/management/commands/display_user_locks.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from redis import Redis | ||
| from redis.lock import Lock | ||
| import logging | ||
|
|
||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from core.settings import CELERY_BROKER_URL | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| redis_client: Redis = Redis.from_url(CELERY_BROKER_URL, decode_responses=True) | ||
|
|
||
| class Command(BaseCommand): | ||
|
|
||
| help = ( | ||
| 'Scans and displays all current user locks (user ID, task name, TTL)' | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
|
|
||
| # Scan for keys matching the lock pattern | ||
| lock_pattern = "lock:celery:user:*:task:*" | ||
| lock_keys = redis_client.keys(lock_pattern) | ||
|
|
||
| if not lock_keys: | ||
| logger.info("No active user locks found.") | ||
| return | ||
|
|
||
| logger.info(f"Found {len(lock_keys)} active user lock(s):") | ||
| for key in lock_keys: | ||
| # Extract user_id and task_name from the key | ||
| parts = key.split(":") | ||
| if len(parts) >= 6: | ||
| user_id = parts[3] | ||
| task_name = parts[5] | ||
| ttl = redis_client.ttl(key) | ||
| logger.info(f"- User ID: {user_id}, Task: {task_name}, TTL: {ttl:,} seconds") | ||
34 changes: 34 additions & 0 deletions
34
backend/apps/ifc_validation/tests/tests_management_commands.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from django.core.management import call_command | ||
| from django.contrib.auth.models import User | ||
| from django.test import TransactionTestCase | ||
|
|
||
| from core.redis_lock import acquire_user_lock | ||
|
|
||
|
|
||
| class DisplayUserLocksManagementCommandTestCase(TransactionTestCase): | ||
|
|
||
| def test_display_user_locks_no_active_locks(self): | ||
|
|
||
| # arrange | ||
| test_user = User.objects.create_user(username='testuser', password='testpass') | ||
|
|
||
| # act | ||
| with self.assertLogs(level='INFO') as cm: | ||
| call_command('display_user_locks') | ||
|
|
||
| # assert | ||
| self.assertTrue(any("No active user locks found." in message for message in cm.output)) | ||
|
|
||
| def test_display_user_locks_active_user_lock(self): | ||
|
|
||
| # arrange | ||
| test_user = User.objects.create_user(username='testuser', password='testpass') | ||
| with acquire_user_lock(user_id=test_user.id, task_name='test_task') as lock: | ||
|
|
||
| # act | ||
| with self.assertLogs(level='INFO') as cm: | ||
| call_command('display_user_locks') | ||
|
|
||
| # assert | ||
| print(cm.output) # for debugging if test fails | ||
| self.assertTrue(any(f"User ID: {test_user.id}, Task: test_task" in message for message in cm.output)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried for a second or so to make this a bit more declarative like but I guess it's not worth it until we have a lot more of these patterns: