Skip to content

Commit

Permalink
Add mutex to page tree move function, fix #1518
Browse files Browse the repository at this point in the history
* Use django-db-mutex module to implement a mutex that allows only
  one page tree move operation at a time.
  • Loading branch information
svenseeberg committed Feb 18, 2023
1 parent 35ab8e8 commit 99237f5
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 61 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ UNRELEASED
* [ [#2056](https://github.com/digitalfabrik/integreat-cms/issues/2056) ] Change restriction of event duration to 28 days
* [ [#2032](https://github.com/digitalfabrik/integreat-cms/issues/2032) ] Remove gap between sidebar boxes in location form
* [ [#1989](https://github.com/digitalfabrik/integreat-cms/issues/1989) ] Improve load time of locations endpoint
* [ [#1518](https://github.com/digitalfabrik/integreat-cms/issues/1518) ] Prevent database corruption when moving 2 pages at the same time


2023.2.0
Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ twine = "*"

[packages]
integreat-cms = {editable = true, path = "."}
django-db-mutex = "*"

[requires]
python_version = "3.9"
124 changes: 65 additions & 59 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions integreat_cms/cms/models/pages/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

from treebeard.ns_tree import NS_NodeQuerySet

from db_mutex import DBMutexError, DBMutexTimeoutError
from db_mutex.db_mutex import db_mutex

from ...utils.translation_utils import gettext_many_lazy as __
from ..abstract_content_model import ContentQuerySet
from ..abstract_tree_node import AbstractTreeNode
Expand Down Expand Up @@ -318,9 +321,19 @@ def move(self, target, pos=None):
:type pos: str
:raises ~treebeard.exceptions.InvalidPosition: If the node is moved to another region
:raises ~db_mutex.DBMutexError: If the DB mutex could not be retrieved
:raises ~db_mutex.DBMutexTimeoutError: If waiting for the DB mutex timed out
"""
super().move(target, pos)
invalidate_model(PageTranslation)
try:
with db_mutex(f"tree-page-{self.region}"):
super().move(target, pos)
invalidate_model(PageTranslation)
except DBMutexError as e:
logger.error("Could not retrieve database mutex to save %s", self)
raise DBMutexError from e
except DBMutexTimeoutError as e:
logger.error("Retrieving database mutex timed out while saving %s", self)
raise DBMutexTimeoutError from e

def archive(self):
"""
Expand Down
1 change: 1 addition & 0 deletions integreat_cms/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
"django.contrib.staticfiles",
# Installed third-party-apps
"corsheaders",
"db_mutex",
"linkcheck",
"polymorphic",
"rules.apps.AutodiscoverRulesConfig",
Expand Down

0 comments on commit 99237f5

Please sign in to comment.