feat: add additional logging to edx-clear-expired-tokens (#31986)
This commit is contained in:
@@ -65,8 +65,9 @@ class Command(BaseCommand): # lint-amnesty, pylint: disable=missing-class-docst
|
||||
deletions = model.objects.filter(pk__in=batch_id_list).delete()
|
||||
deletion_count = deletions[0]
|
||||
total_deletions += deletion_count
|
||||
logger.info(f"Removed {total_deletions} rows from {model.__name__} table")
|
||||
sleep(sleep_time)
|
||||
message = f'Cleaned {total_deletions} rows from {model.__name__} table'
|
||||
message = f'Final deletion count: Cleaned {total_deletions} rows from {model.__name__} table'
|
||||
logger.info(message)
|
||||
|
||||
def get_expiration_time(self, now): # lint-amnesty, pylint: disable=missing-function-docstring
|
||||
@@ -91,20 +92,24 @@ class Command(BaseCommand): # lint-amnesty, pylint: disable=missing-class-docst
|
||||
refresh_expire_at = self.get_expiration_time(now)
|
||||
|
||||
if options['revoked-tokens']:
|
||||
logger.info("Removing revoked RefreshTokens")
|
||||
# remove revoked, as opposed to expired, RefreshTokens
|
||||
revoked = RefreshToken.objects.filter(revoked__lt=refresh_expire_at).exclude(
|
||||
application_id__in=excluded_application_ids)
|
||||
self.clear_table_data(revoked, batch_size, RefreshToken, sleep_time)
|
||||
|
||||
if options['refresh-tokens']:
|
||||
logger.info("Removing expired RefreshTokens")
|
||||
query_set = RefreshToken.objects.filter(access_token__expires__lt=refresh_expire_at).exclude(
|
||||
application_id__in=excluded_application_ids)
|
||||
self.clear_table_data(query_set, batch_size, RefreshToken, sleep_time)
|
||||
|
||||
if options['access-tokens']:
|
||||
logger.info("Removing expired AccessTokens")
|
||||
query_set = AccessToken.objects.filter(refresh_token__isnull=True, expires__lt=now)
|
||||
self.clear_table_data(query_set, batch_size, AccessToken, sleep_time)
|
||||
|
||||
if options['grants']:
|
||||
logger.info("Removing expired Grants")
|
||||
query_set = Grant.objects.filter(expires__lt=now)
|
||||
self.clear_table_data(query_set, batch_size, Grant, sleep_time)
|
||||
|
||||
@@ -72,26 +72,26 @@ class EdxClearExpiredTokensTests(TestCase): # lint-amnesty, pylint: disable=mis
|
||||
factories.RefreshTokenFactory(user=application.user, application=application, access_token=access_token)
|
||||
with LogCapture(LOGGER_NAME) as log:
|
||||
call_command('edx_clear_expired_tokens', sleep_time=0, excluded_application_ids=str(application.id))
|
||||
log.check(
|
||||
log.check_present(
|
||||
(
|
||||
LOGGER_NAME,
|
||||
'INFO',
|
||||
f'Cleaned {0} rows from {RefreshToken.__name__} table'
|
||||
f'Final deletion count: Cleaned {0} rows from {RefreshToken.__name__} table'
|
||||
),
|
||||
(
|
||||
LOGGER_NAME,
|
||||
'INFO',
|
||||
f'Cleaned {0} rows from {RefreshToken.__name__} table'
|
||||
f'Final deletion count: Cleaned {0} rows from {RefreshToken.__name__} table'
|
||||
),
|
||||
(
|
||||
LOGGER_NAME,
|
||||
'INFO',
|
||||
f'Cleaned {0} rows from {AccessToken.__name__} table',
|
||||
f'Final deletion count: Cleaned {0} rows from {AccessToken.__name__} table',
|
||||
),
|
||||
(
|
||||
LOGGER_NAME,
|
||||
'INFO',
|
||||
f'Cleaned 0 rows from {Grant.__name__} table',
|
||||
f'Final deletion count: Cleaned 0 rows from {Grant.__name__} table',
|
||||
)
|
||||
)
|
||||
assert RefreshToken.objects.filter(application=application).exists()
|
||||
|
||||
Reference in New Issue
Block a user