Skip to content

Commit 80b7eb5

Browse files
author
Gordon B. McMillan
committed
Lock the database, which means another round of making sure there's only one.
1 parent a5d5141 commit 80b7eb5

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

roundup/backends/back_metakit.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,26 @@
22
import metakit
33
from sessions import Sessions
44
import re, marshal, os, sys, weakref, time, calendar
5-
from roundup import indexer
5+
from roundup import indexer
6+
import locking
67

7-
class Database(hyperdb.Database):
8+
_dbs = {}
9+
10+
def Database(config, journaltag=None):
11+
db = _dbs.get(config.DATABASE, None)
12+
if db is None or db._db is None:
13+
db = _Database(config, journaltag)
14+
_dbs[config.DATABASE] = db
15+
return db
16+
17+
class _Database(hyperdb.Database):
818
def __init__(self, config, journaltag=None):
919
self.config = config
1020
self.journaltag = journaltag
1121
self.classes = {}
1222
self._classes = []
1323
self.dirty = 0
24+
self.lockfile = None
1425
self._db = self.__open()
1526
self.indexer = Indexer(self.config.DATABASE, self._db)
1627
self.sessions = Sessions(self.config)
@@ -60,13 +71,13 @@ def clear(self):
6071
for cl in self.classes.values():
6172
cl._clear()
6273
def hasnode(self, classname, nodeid):
63-
return self.getclass(clasname).hasnode(nodeid)
74+
return self.getclass(classname).hasnode(nodeid)
6475
def pack(self, pack_before):
6576
pass
6677
def addclass(self, cl):
6778
self.classes[cl.classname] = cl
68-
if self.tables.find(name=cl.classname) < 0:
69-
self.tables.append(name=cl.classname)
79+
if self.tables.find(name=cl.classname) < 0:
80+
self.tables.append(name=cl.classname)
7081
def addjournal(self, tablenm, nodeid, action, params):
7182
tblid = self.tables.find(name=tablenm)
7283
if tblid == -1:
@@ -102,12 +113,19 @@ def close(self):
102113
for cl in self.classes.values():
103114
cl.db = None
104115
self._db = None
116+
locking.release_lock(self.lockfile)
117+
del _dbs[self.config.DATABASE]
118+
self.lockfile.close()
105119
self.classes = {}
106120
self.indexer = None
107121

108122
# --- internal
109123
def __open(self):
110124
self.dbnm = db = os.path.join(self.config.DATABASE, 'tracker.mk4')
125+
lockfilenm = db[:-3]+'lck'
126+
self.lockfile = locking.acquire_lock(lockfilenm)
127+
self.lockfile.write(str(os.getpid()))
128+
self.lockfile.flush()
111129
self.fastopen = 0
112130
if os.path.exists(db):
113131
dbtm = os.path.getmtime(db)
@@ -153,7 +171,8 @@ def __open(self):
153171
class Class:
154172
privateprops = None
155173
def __init__(self, db, classname, **properties):
156-
self.db = weakref.proxy(db)
174+
#self.db = weakref.proxy(db)
175+
self.db = db
157176
self.classname = classname
158177
self.keyname = None
159178
self.ruprops = properties
@@ -831,10 +850,8 @@ def __getview(self):
831850
else:
832851
mkprop = None
833852
if mkprop is None:
834-
print "%s missing prop %s (%s)" % (self.classname, nm, rutyp.__class__.__name__)
835853
break
836854
if _typmap[rutyp.__class__] != mkprop.type:
837-
print "%s - prop %s (%s) has wrong mktyp (%s)" % (self.classname, nm, rutyp.__class__.__name__, mkprop.type)
838855
break
839856
else:
840857
return view.ordered(1)
@@ -923,9 +940,6 @@ def create(self, **propvalues):
923940
newid = Class.create(self, **propvalues)
924941
if not content:
925942
return newid
926-
if content.startswith('/tracker/download.php?'):
927-
self.set(newid, content='http://sourceforge.net'+content)
928-
return newid
929943
nm = bnm = '%s%s' % (self.classname, newid)
930944
sd = str(int(int(newid) / 1000))
931945
d = os.path.join(self.db.config.DATABASE, 'files', self.classname, sd)
@@ -1071,7 +1085,3 @@ def save_index(self):
10711085
if self.changed:
10721086
self.db.commit()
10731087
self.changed = 0
1074-
1075-
1076-
1077-

0 commit comments

Comments
 (0)