From af39ed15aa79df3d3def48c0201a1b73042d6cd2 Mon Sep 17 00:00:00 2001 From: Dominik George Date: Wed, 5 Jan 2022 13:59:27 +0100 Subject: [PATCH] Add Celery task for cleantokens --- AUTHORS | 1 + CHANGELOG.md | 1 + docs/management_commands.rst | 5 +++++ oauth2_provider/tasks.py | 8 ++++++++ 4 files changed, 15 insertions(+) create mode 100644 oauth2_provider/tasks.py diff --git a/AUTHORS b/AUTHORS index 295bbe6e1..a6c6ef1d2 100644 --- a/AUTHORS +++ b/AUTHORS @@ -66,3 +66,4 @@ pySilver Shaheed Haque Vinay Karanam Eduardo Oliveira +Dominik George diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f09ff664..b5a70cd5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added * #651 Batch expired token deletions in `cleartokens` management command * Added pt-BR translations. +* #1070 Add a Celery task for clearing expired tokens, e.g. to be scheduled as a [periodic task](https://docs.celeryproject.org/en/stable/userguide/periodic-tasks.html) ### Fixed * #1012 Return status for introspecting a nonexistent token from 401 to the correct value of 200 per [RFC 7662](https://datatracker.ietf.org/doc/html/rfc7662#section-2.2). diff --git a/docs/management_commands.rst b/docs/management_commands.rst index 147a0bbe4..727ff9e98 100644 --- a/docs/management_commands.rst +++ b/docs/management_commands.rst @@ -21,3 +21,8 @@ To prevent the CPU and RAM high peaks during deletion process use ``CLEAR_EXPIRE Note: Refresh tokens need to expire before AccessTokens can be removed from the database. Using ``cleartokens`` without ``REFRESH_TOKEN_EXPIRE_SECONDS`` has limited effect. + +The ``cleartokens`` action can also be scheduled as a `Celery periodic task`_ +by using the ``clear_tokens`` task (automatically registered when using Celery). + +.. _Celery periodic task: https://docs.celeryproject.org/en/stable/userguide/periodic-tasks.html diff --git a/oauth2_provider/tasks.py b/oauth2_provider/tasks.py new file mode 100644 index 000000000..d86c33720 --- /dev/null +++ b/oauth2_provider/tasks.py @@ -0,0 +1,8 @@ +from celery import shared_task + + +@shared_task +def clear_tokens(): + from ...models import clear_expired # noqa + + clear_expired()