forked from learningequality/kolibri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.py
75 lines (60 loc) · 1.83 KB
/
lock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from contextlib import contextmanager
from sqlite3 import OperationalError
from django.db import connection
from django.db import transaction
class DummyOperation(object):
def __init__(self):
self.obj = None
def execute(self):
from kolibri.core.device.models import SQLiteLock
self.obj = SQLiteLock.objects.create()
def revert(self):
if self.obj:
self.obj.delete()
class PostgresLock(object):
def __init__(self, key=None):
self.key = key
def execute(self):
query = "SELECT pg_advisory_xact_lock({key}) AS lock;".format(key=self.key)
with connection.cursor() as c:
c.execute(query)
@contextmanager
def db_lock_sqlite_only():
if connection.vendor == "sqlite":
while True:
try:
with transaction.atomic():
operation = DummyOperation()
operation.execute()
yield
operation.revert()
break
except OperationalError as e:
if "database is locked" not in str(e):
raise e
else:
yield
@contextmanager
def db_lock_postgresql_only():
lock_id = 1
if connection.vendor == "postgresql":
with transaction.atomic():
operation = PostgresLock(key=lock_id)
operation.execute()
yield
else:
yield
@contextmanager
def db_lock():
if connection.vendor == "sqlite":
with db_lock_sqlite_only():
yield
elif connection.vendor == "postgresql":
with db_lock_postgresql_only():
yield
else:
raise NotImplementedError(
"kolibri.core.utils.cache.DatabaseLock not implemented for vendor {vendor}".format(
vendor=connection.vendor
)
)