GithubHelp home page GithubHelp logo

stefanfoulis / django-graceful-session-cleanup Goto Github PK

View Code? Open in Web Editor NEW
16.0 2.0 3.0 114 KB

A simple management command that can delete expired sessions from large session tables without killing the site.

Python 100.00%

django-graceful-session-cleanup's Introduction

django-graceful-session-cleanup

Warning: currently only works with mysql

A simple management command that can delete expired sessions from large session tables without killing the site.

The main use case is if you have a live application with millions of stale sessions in the database. Runnung the regular python manage.py cleanup will potentially lock your database for hours, essentially killing your site.

With django-graceful-session-cleanup you can gradually delete all the sessions.

To achieve this it will always only delete a small amount of the expired sessions per query and then sleep for a few seconds. There are commandline options to control interval, sleep time and amount of sessions to delete per interval.

Installation

install the package:

pip install django-graceful-session-cleanup

add it to INSTALLED_APPS:

INSTALLED_APPS = [
    ....
    'graceful_session_cleanup',
]

run the management command:

python manage.py graceful_session_cleanup

There are a few options you can use:

python manage.py graceful_session_cleanup --sleep-seconds 9 --delete-count 1000 --iteration-count 200

This will delete 1000 expired session entries, wait for 5 seconds so other processes can use the database and then repeat this 200 times. Depending on database load (assuming deleting takes 1s) this will take (9s + 1s) * 200 = 2000s.

there is help on the commandline:

$ python manage.py help graceful_session_cleanup
Usage: django graceful_session_cleanup [options]

Can be run as a cronjob or directly to clean out old data from the database (only expired sessions at the moment). Does this in a live db friendly way by never hogging the connection too long.

Options:
  -v VERBOSITY, --verbosity=VERBOSITY
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=all output
  --settings=SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath=PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Print traceback on exception
  -s SLEEP_SECONDS, --sleep-seconds=SLEEP_SECONDS
                        How long to sleep between each delete operation.
  -c DELETE_COUNT, --delete-count=DELETE_COUNT
                        How many expired sessions to delete per iteration.
  -i ITERATION_COUNT, --iteration-count=ITERATION_COUNT
                        How many iterations to run.
  --version             show program's version number and exit
  -h, --help            show this help message and exit

django-graceful-session-cleanup's People

Contributors

eedeep avatar stefanfoulis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

eedeep genba gvs666

django-graceful-session-cleanup's Issues

add a "do it forever" option

make the command runnable by supervisord or similar in an "endless" mode. running it from crontab adds the risk that multiple running processes may pile up.

provide command to show session count

add a command or option to display the amount of sessions in the database.

  • Session.objects.all().count() seems to work really fast, even with huge tables (tested on mysql)
  • Session.objects.filter(expire_date__lt=datetime.datetime.now()).count() took over 2 minutes on my test database with 22M records and 11M expired sessions. -> don't provide this or add a huge warning.

optimize query for low numbers of expired sessions

The less expired sessions are in the database, the longer the delete query takes. This is probably because the database backend has to go thorough the whole list of not expired sessions desperately looking for ones to delete.

Currently it takes 8s to delete just 40 Sessions with a table with 11M active Sessions on our testsystem. Probably this would be a good case to just use Redis for sessions.

transaction.commit_unless_managed() missing

It seems that a call to transaction.commit_unless_managed() is missing after the cursor.execute() call. Without this, for me, running Mysql with InnoDB as the database engine, nothing actually ends up getting deleted (even though the log output from graceful session cleanup reports that rows are being deleted).

Does this sound right? It seems like something obvious that would have been noticed by now.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.