Implementation of django-crontab to run flushexpiredtokens
Table of Contents
In this project we will see the Implementation of django-crontab to execute flushexpiredtokens and eliminate the tokens from the Outstanding tokens and Blacklisted tokens generated with Simple JWT list.
Frameworks and libraries. Framewrok:
Libraries:
With their streamlined API client, you can quickly and easily send REST, SOAP, GraphQL, and GRPC requests directly within Insomnia. Link to visit insomnia website: - Link
Postman is a collaboration platform for API development. Postman's features simplify each step of building an API and streamline collaboration so you can create better APIsβfaster. Link to visit postman website: - Link
In this example we use simple jwt with django rest framework, you must have a jwt implementation to generate the tokens that will be sent to the Blacklisted token and Outstanding tokens
-
To obtain my repository you must create a folder in a desired directory and within this folder open a terminal or use cmd in the case of windows.
-
Clone the repo
git clone [email protected]:aldomatus/django-crontab-to-run-flushexpiredtokens.git
-
Make the pull request from a branch called main
git fetch git checkout origin master git pull origin master
http://localhost:8000/admin/
In our requirements file we write the flask libraries, the connection libraries for msql and with which we are going to manage the SQL data
asgiref==3.4.1
Django==3.2.7
django-common-helpers==0.9.2
django-cors-headers==3.8.0
django-cron==0.5.1
django-crontab==0.7.1
django-environ==0.7.0
django-rest-auth==0.9.5
djangorestframework==3.12.4
djangorestframework-simplejwt==4.8.0
mysqlclient==2.0.3
Pillow==8.3.2
PyJWT==2.1.0
pytz==2021.1
six==1.16.0
sqlparse==0.4.2
This file contains our environment variables that will be read by the dockerfile.
FLASK_DEBUG=True
FLASK_APP=src/main.py
FLASK_RUN_HOST=0.0.0.0
ALLOWED_HOSTS=127.0.0.1,localhost
install via pip:
pip install django-crontab
add it to installed apps in django settings.py:
INSTALLED_APPS = (
'django_crontab',
...
)
now create a new method that should be executed by cron every 5 minutes, f.e. in myapp/cron.py:
def my_scheduled_job():
pass
# Example
import os
import sys
def flushexpiredtokens():
os.system('GREPDB="cd /home/aldo/Documents/independent_projects/wpo-back/ && source .venv/bin/activate && python3 manage.py flushexpiredtokens > /dev/null 2>&1"; /bin/bash -c "$GREPDB"')
now add this to your settings.py:
CRONJOBS = [
('*/5 * * * *', 'myapp.cron.my_scheduled_job')
]
# Example
CRONJOBS = [
('*/1 * * * *', 'Apps.users.cron.flushexpiredtokens')
]
Distributed under the MIT License. See LICENSE
for more information.
Aldo Matus - Linkedin Facebook
Project Link: Repository