Skip to content

Commit b4f1569

Browse files
committed
Add management command
1 parent 6a402a9 commit b4f1569

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Generated by Claude Sonnet 4
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Generated by Claude Sonnet 4
2+
import json
3+
4+
from django.core.management.base import BaseCommand
5+
6+
from ansible_base.lib.utils.db import advisory_lock_id_to_debug_info, get_active_advisory_locks
7+
8+
9+
class AdvisoryLocksCommand(BaseCommand):
10+
"""Base command class for advisory lock management."""
11+
12+
help = "Show currently active PostgreSQL advisory locks"
13+
14+
def add_arguments(self, parser):
15+
parser.add_argument(
16+
'--format',
17+
choices=['table', 'json'],
18+
default='table',
19+
help='Output format (default: table)',
20+
)
21+
parser.add_argument(
22+
'--show-debug-info',
23+
action='store_true',
24+
help='Include debug information for lock IDs',
25+
)
26+
27+
def handle(self, *args, **options):
28+
locks = get_active_advisory_locks()
29+
30+
if not locks:
31+
self.stdout.write("No active advisory locks found.")
32+
return
33+
34+
if options['format'] == 'json':
35+
self._output_json(locks, options['show_debug_info'])
36+
else:
37+
self._output_table(locks, options['show_debug_info'])
38+
39+
def _output_json(self, locks, show_debug_info):
40+
"""Output locks in JSON format."""
41+
if show_debug_info:
42+
for lock in locks:
43+
# Add debug info for the objid (main lock ID)
44+
lock['debug_info'] = advisory_lock_id_to_debug_info(lock['objid'])
45+
46+
self.stdout.write(json.dumps(locks, indent=2, default=str))
47+
48+
def _output_table(self, locks, show_debug_info):
49+
"""Output locks in table format."""
50+
# Header
51+
headers = ['PID', 'Mode', 'Granted', 'Class ID', 'Object ID', 'Object Sub ID']
52+
if show_debug_info:
53+
headers.extend(['Hex', 'Unsigned CRC32', 'High Bit'])
54+
55+
self.stdout.write(self.style.SUCCESS(' | '.join(headers)))
56+
self.stdout.write('-' * (len(' | '.join(headers)) + 20))
57+
58+
# Rows
59+
for lock in locks:
60+
row = [
61+
str(lock['pid']),
62+
lock['mode'],
63+
'Yes' if lock['granted'] else 'No',
64+
str(lock['classid']),
65+
str(lock['objid']),
66+
str(lock['objsubid']),
67+
]
68+
69+
if show_debug_info:
70+
debug_info = advisory_lock_id_to_debug_info(lock['objid'])
71+
row.extend(
72+
[
73+
debug_info['hex_representation'],
74+
str(debug_info['unsigned_crc32']),
75+
'Yes' if debug_info['had_high_bit_set'] else 'No',
76+
]
77+
)
78+
79+
self.stdout.write(' | '.join(row))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Generated by Claude Sonnet 4
2+
from ansible_base.lib.management.advisory_locks import AdvisoryLocksCommand
3+
4+
5+
class Command(AdvisoryLocksCommand):
6+
"""Management command to show active PostgreSQL advisory locks."""
7+
8+
pass

0 commit comments

Comments
 (0)