Skip to content

Commit

Permalink
Update migrate_with_timeouts MC documentation
Browse files Browse the repository at this point in the history
To include input fields for the retry mechanism.
  • Loading branch information
marcelofern committed Sep 19, 2024
1 parent 1d1c920 commit c482024
Showing 1 changed file with 53 additions and 12 deletions.
65 changes: 53 additions & 12 deletions docs/usage/management_commands.rst
Original file line number Diff line number Diff line change
@@ -1,27 +1,61 @@
Management Commands
===================

migrate_with_timeouts
---------------------
``migrate_with_timeouts``
-------------------------

Runs database migrations with timeouts.

This command wraps the :ref:`apply_timeouts() <apply_timeouts>` around
Django's ``migrate`` command, exposing two extra arguments:
Django's ``migrate`` command, exposing extra arguments to handle retries with
backoff propagation.

- ``--lock-timeout-in-ms``
- ``--statement-timeout-in-ms``
+++++++++++++++++++++++
lock_timeout arguments:
+++++++++++++++++++++++

.. important::
* ``--lock-timeout-in-ms``: Sets the lock_timeout value for the migration.
* ``--lock-timeout-max-retries``: How many times to retry after getting a
lock_timeout. Defaults to zero, which means no retries.
* ``--lock-timeout-retry-exp``: The exponential to use for exponential backoff
retry if retries are enabled. Defaults to 2.
* ``--lock-timeout-retry-min-wait-in-ms``: minimum amount of time to wait
between lock_timeout retries in milliseconds. Defaults to 3s.
* ``--lock-timeout-retry-max-wait-in-ms``: Maximum amount of time to wait
between lock_timeout retries in milliseconds. Defaults to 10s.

Both arguments are optional, but at least one must be provided!
++++++++++++++++++++++++++++
statement_timeout arguments:
++++++++++++++++++++++++++++

These arguments will set the value of Postgres' ``lock_timeout`` and
``statement_timeout`` for the duration of the migration.
* ``--statement-timeout-in-ms``: Sets statement_timeout for the migration.

==========
Note: Retry configurations are not available for statement timeouts at this
stage. If you have a use case where statement retries would be useful please
open an issue for discussion.

+++++++++++++++
retry callback:
+++++++++++++++

* ``--retry-callback-path``: Sets a callback to be called after a timeout
event has happened. An example is set below:

.. code-block:: python
import logging
from django_pg_migration_tools import timeouts
from django_pg_migration_tools.management.commands import migrate_with_timeouts
def timeout_callback(state: migrate_with_timeouts.RetryState) -> None:
logging.info("A lock timeout just happened!")
logging.info(f"{state.lock_timeouts_count} lock timeouts so far!")
++++++++++
How to use
==========
++++++++++

1. Include ``django_pg_migration_tools`` in your ``INSTALLED_APPS``.

Expand All @@ -37,4 +71,11 @@ How to use

.. code-block:: bash
./manage.py migrate_with_timeouts --lock-timeout-in-ms=10000 --statement-timeout-in-ms=60000
./manage.py migrate_with_timeouts \
--lock-timeout-in-ms=10000 \
--lock-timeout-max-retries=3 \
--lock-timeout-retry-exp=2 \
--lock-timeout-retry-min-wait-in-ms=3000 \
--lock-timeout-retry-max-wait-in-ms=10000 \
--statement-timeout-in-ms=10000 \
--retry-callback-path="dotted.path.to.callback.function"

0 comments on commit c482024

Please sign in to comment.