Skip to content

Commit 3173d05

Browse files
committed
[feature] Add --number_of_hours flag to cleanup_stale_radacct
1 parent 9e026a5 commit 3173d05

5 files changed

Lines changed: 42 additions & 9 deletions

File tree

docs/user/management_commands.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ For example:
6060
6161
./manage.py cleanup_stale_radacct 15
6262
63+
If you need to clean up stale sessions more aggressively, you can use
64+
hours instead of days:
65+
66+
.. code-block:: shell
67+
68+
./manage.py cleanup_stale_radacct --number_of_hours=4
69+
6370
``deactivate_expired_users``
6471
----------------------------
6572

openwisp_radius/base/models.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,15 @@ def __str__(self):
529529
return self.unique_id
530530

531531
@classmethod
532-
def close_stale_sessions(cls, days):
533-
older_than = timezone.now() - timedelta(days=days)
532+
def close_stale_sessions(cls, days=None, hours=None):
533+
if hours:
534+
delta = timedelta(hours=hours)
535+
elif days:
536+
delta = timedelta(days=days)
537+
else:
538+
raise ValueError("Missing `days` or `hours`")
539+
# determine limit date time
540+
older_than = timezone.now() - delta
534541
# If the "update_time" is recent, then the session is not closed
535542
# even when the "start_time" is older than the specified time.
536543
# The "start_time" of a session is only checked when the

openwisp_radius/management/commands/base/cleanup_stale_radacct.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@ class BaseCleanupRadacctCommand(BaseCommand):
1010

1111
def add_arguments(self, parser):
1212
parser.add_argument("number_of_days", type=int, nargs="?", default=15)
13+
parser.add_argument("number_of_hours", type=int, nargs="?", default=0)
1314

1415
def handle(self, *args, **options):
15-
RadiusAccounting.close_stale_sessions(days=options["number_of_days"])
16-
self.stdout.write(
17-
f'Closed active sessions older than {options["number_of_days"]} days'
16+
RadiusAccounting.close_stale_sessions(
17+
days=options["number_of_days"],
18+
# defaults to zero
19+
hours=options["number_of_hours"],
1820
)
21+
if options["number_of_hours"]:
22+
time_output = f"{options['number_of_hours']} hours"
23+
else:
24+
time_output = f"{options['number_of_days']} days"
25+
self.stdout.write(f"Closed active sessions older than {time_output}")

openwisp_radius/tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def delete_old_radacct(number_of_days=365):
2828

2929

3030
@shared_task
31-
def cleanup_stale_radacct(number_of_days=365):
32-
management.call_command("cleanup_stale_radacct", number_of_days)
31+
def cleanup_stale_radacct(number_of_days=365, number_of_hours=0):
32+
management.call_command("cleanup_stale_radacct", number_of_days, number_of_hours)
3333

3434

3535
@shared_task

openwisp_radius/tests/test_commands.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,26 @@ def test_cleanup_stale_radacct_command(self):
6969
self.assertEqual(session.update_time, session.stop_time)
7070
self.assertEqual(session.terminate_cause, "Session Timeout")
7171

72-
with self.subTest("Test does not affect closed session"):
72+
with self.subTest("Test start_time and update_time older than specified hours"):
7373
options["unique_id"] = "120"
74+
options["update_time"] = "2017-06-10 10:50:00"
75+
options["start_time"] = "2017-06-10 10:50:00"
76+
self._create_radius_accounting(**options)
77+
call_command("cleanup_stale_radacct", number_of_hours=4)
78+
session = RadiusAccounting.objects.get(unique_id="120")
79+
self.assertNotEqual(session.stop_time, None)
80+
self.assertNotEqual(session.session_time, None)
81+
self.assertEqual(session.update_time, session.stop_time)
82+
self.assertEqual(session.terminate_cause, "Session Timeout")
83+
84+
with self.subTest("Test does not affect closed session"):
85+
options["unique_id"] = "121"
7486
options["start_time"] = "2017-06-10 10:50:00"
7587
options["update_time"] = "2017-06-10 10:55:00"
7688
options["stop_time"] = "2017-06-10 10:55:00"
7789
self._create_radius_accounting(**options)
7890
call_command("cleanup_stale_radacct", 1)
79-
session = RadiusAccounting.objects.get(unique_id="120")
91+
session = RadiusAccounting.objects.get(unique_id="121")
8092
self.assertEqual(
8193
session.stop_time.astimezone(get_default_timezone()).strftime(
8294
"%Y-%m-%d %H:%M:%S"

0 commit comments

Comments
 (0)