Skip to content

Commit

Permalink
Add retry decorator for DB locked errors, remove coach hook cleaning …
Browse files Browse the repository at this point in the history
…up syncs
  • Loading branch information
bjester committed Nov 7, 2023
1 parent 974a15e commit cfda417
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
22 changes: 22 additions & 0 deletions kolibri/core/utils/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from django.db import connection
from django.db import transaction
from django.utils.functional import wraps


class DummyOperation(object):
Expand Down Expand Up @@ -73,3 +74,24 @@ def db_lock():
vendor=connection.vendor
)
)


def retry_on_db_lock(func, retries=5):
"""
Decorator that retries a function if it fails due to a database lock.
"""

@wraps(func)
def wrapper(*args, **kwargs):
attempts = 0
while True:
try:
attempts += 1
result = func(*args, **kwargs)
break
except OperationalError as e:
if "database is locked" not in str(e) or attempts >= retries:
raise e
return result

return wrapper
18 changes: 0 additions & 18 deletions kolibri/plugins/coach/kolibri_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import logging

from kolibri.core.auth.constants.user_kinds import COACH
from kolibri.core.device.utils import get_device_setting
from kolibri.core.discovery.hooks import NetworkLocationDiscoveryHook
from kolibri.core.hooks import NavigationHook
from kolibri.core.hooks import RoleBasedRedirectHook
from kolibri.core.webpack import hooks as webpack_hooks
Expand Down Expand Up @@ -59,19 +57,3 @@ def plugin_data(self):
return {
"practice_quizzes_exist": practice_quizzes_exist,
}


@register_hook
class NetworkDiscoveryForSoUDHook(NetworkLocationDiscoveryHook):
def on_disconnect(self, network_location):
"""
:type network_location: kolibri.core.discovery.models.NetworkLocation
"""
from kolibri.core.auth.tasks import queue_soud_server_sync_cleanup

if (
not get_device_setting("subset_of_users_device")
and network_location.subset_of_users_device
):
logger.debug("SoUD listener: triggering cleanup of SoUD sync")
queue_soud_server_sync_cleanup(network_location.instance_id)
2 changes: 2 additions & 0 deletions kolibri/plugins/learn/kolibri_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from kolibri.core.discovery.hooks import NetworkLocationDiscoveryHook
from kolibri.core.hooks import NavigationHook
from kolibri.core.hooks import RoleBasedRedirectHook
from kolibri.core.utils.lock import retry_on_db_lock
from kolibri.core.webpack import hooks as webpack_hooks
from kolibri.plugins import KolibriPluginBase
from kolibri.plugins.hooks import register_hook
Expand Down Expand Up @@ -109,6 +110,7 @@ def node_url(self, node):
)


@retry_on_db_lock
def request_soud_sync(network_location):
"""
:type network_location: kolibri.core.discovery.models.NetworkLocation
Expand Down

0 comments on commit cfda417

Please sign in to comment.