reset_locks

The reset_locks management command allows you to delete ALL LOCKS set by django-lockmgr in your application’s database, regardless of their expiration time, name, or who/what created them.

You may encounter stagnant locks if you’re using locking functions such as get_lock(), instead of using the context manager LockMgr (or in rare events where your application exits unexpectedly, without time to cleanup locks).

Below is an excerpt from the manage.py help ./manage.py reset_locks --help:

Clears ALL locks that were set using Privex's django-lockmgr package

optional arguments:
  -h, --help            show this help message and exit
  -f, --force           Do not show warning / ask if you are sure before deleting ALL locks

Example usage

First let’s create two locks using lockmgr.management.commands.set_lock

# Create the two locks 'hello' and 'world'
./manage.py set_lock hello world

    Finished creating / renewing 2 locks.

Now we’ll run reset_locks without any arguments. You can see it requires confirmation, since it can be dangerous to clear all locks if there are any applications running (or scheduled on a cron) that depend on the locks to avoid conflicts.

./manage.py reset_locks

    WARNING: You are about to clear ALL locks set using Privex LockMgr.
    You should only do this if you know what you're doing, and have made sure to stop any running
    instances of your application, to ensure no conflicts are caused by removing ALL LOCKS.
    
    
    The following 2 locks would be removed:
    
    =========================================================
    
    <Lock name='hello' locked_by='example.org' locked_until='2019-11-22 00:49:02.264729+00:00'>
    <Lock name='world' locked_by='example.org' locked_until='2019-11-22 00:49:02.267728+00:00'>
    
    =========================================================
    
    Are you SURE you want to clear all locks?
    Type YES in all capitals if you are sure > YES
    
    =========================================================

    Please wait... Removing all locks regardless of their status or expiration.
    
    A total of 2 lock rows were deleted. All locks should now be removed.
    
    =========================================================
    
    Finished clearing locks.
    
    =========================================================

Example 2 - Using the FORCE argument to skip the prompt

Let’s re-create those locks, and now run reset_locks with -f (force).

# Create the two locks 'hello' and 'world'
./manage.py set_lock hello world

    Finished creating / renewing 2 locks.

# Run 'reset_locks' with option '-f' (force / do not ask for confirmation)
./manage.py reset_locks -f
    
    The following 2 locks would be removed:
    
    =========================================================
    
    <Lock name='hello' locked_by='example.org' locked_until='2019-11-22 00:58:00.042322+00:00'>
    <Lock name='world' locked_by='example.org' locked_until='2019-11-22 00:58:00.045513+00:00'>
    
    =========================================================
    
    Option 'force' (-f / --force) was specified. Skipping confirmation prompt.
    Please wait... Removing all locks regardless of their status or expiration.
    
    A total of 2 lock rows were deleted. All locks should now be removed.
    
    =========================================================
    
    Finished clearing locks.
    
    =========================================================

Classes

Command()

class lockmgr.management.commands.reset_locks.Command[source]
add_arguments(parser: django.core.management.base.CommandParser)[source]

Entry point for subclassed commands to add custom arguments.

handle(*args, **options)[source]

The actual logic of the command. Subclasses must implement this method.