Fix double opening of dbs
This commit is contained in:
parent
9d777951dd
commit
8c51e81a0b
1 changed files with 37 additions and 27 deletions
|
@ -54,15 +54,14 @@ class SiteStorage(object):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Create new databaseobject with the site's schema
|
# Create new databaseobject with the site's schema
|
||||||
@util.Noparallel()
|
|
||||||
def openDb(self, close_idle=False):
|
def openDb(self, close_idle=False):
|
||||||
schema = self.getDbSchema()
|
schema = self.getDbSchema()
|
||||||
db_path = self.getPath(schema["db_file"])
|
db_path = self.getPath(schema["db_file"])
|
||||||
return Db(schema, db_path, close_idle=close_idle)
|
return Db(schema, db_path, close_idle=close_idle)
|
||||||
|
|
||||||
def closeDb(self):
|
def closeDb(self, reason="Unknown (SiteStorage)"):
|
||||||
if self.db:
|
if self.db:
|
||||||
self.db.close()
|
self.db.close(reason)
|
||||||
self.event_db_busy = None
|
self.event_db_busy = None
|
||||||
self.db = None
|
self.db = None
|
||||||
|
|
||||||
|
@ -73,37 +72,48 @@ class SiteStorage(object):
|
||||||
raise Exception("dbschema.json is not a valid JSON: %s" % err)
|
raise Exception("dbschema.json is not a valid JSON: %s" % err)
|
||||||
return schema
|
return schema
|
||||||
|
|
||||||
# Return db class
|
def loadDb(self):
|
||||||
def getDb(self):
|
self.log.debug("No database, waiting for dbschema.json...")
|
||||||
if not self.db:
|
self.site.needFile("dbschema.json", priority=3)
|
||||||
self.log.debug("No database, waiting for dbschema.json...")
|
self.log.debug("Got dbschema.json")
|
||||||
self.site.needFile("dbschema.json", priority=3)
|
self.has_db = self.isFile("dbschema.json") # Recheck if dbschema exist
|
||||||
self.has_db = self.isFile("dbschema.json") # Recheck if dbschema exist
|
if self.has_db:
|
||||||
if self.has_db:
|
schema = self.getDbSchema()
|
||||||
schema = self.getDbSchema()
|
db_path = self.getPath(schema["db_file"])
|
||||||
db_path = self.getPath(schema["db_file"])
|
if not os.path.isfile(db_path) or os.path.getsize(db_path) == 0:
|
||||||
if not os.path.isfile(db_path) or os.path.getsize(db_path) == 0:
|
|
||||||
try:
|
|
||||||
self.rebuildDb()
|
|
||||||
except Exception as err:
|
|
||||||
self.log.error(err)
|
|
||||||
pass
|
|
||||||
|
|
||||||
if self.db:
|
|
||||||
self.db.close()
|
|
||||||
self.db = self.openDb(close_idle=True)
|
|
||||||
try:
|
try:
|
||||||
changed_tables = self.db.checkTables()
|
self.rebuildDb(reason="Missing database")
|
||||||
if changed_tables:
|
except Exception as err:
|
||||||
self.rebuildDb(delete_db=False) # TODO: only update the changed table datas
|
self.log.error(err)
|
||||||
except sqlite3.OperationalError:
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
if self.db:
|
||||||
|
self.db.close("Gettig new db for SiteStorage")
|
||||||
|
self.db = self.openDb(close_idle=True)
|
||||||
|
try:
|
||||||
|
changed_tables = self.db.checkTables()
|
||||||
|
if changed_tables:
|
||||||
|
self.rebuildDb(delete_db=False, reason="Changed tables") # TODO: only update the changed table datas
|
||||||
|
except sqlite3.OperationalError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Return db class
|
||||||
|
@util.Noparallel()
|
||||||
|
def getDb(self):
|
||||||
|
if self.event_db_busy: # Db not ready for queries
|
||||||
|
self.log.debug("Wating for db...")
|
||||||
|
self.event_db_busy.get() # Wait for event
|
||||||
|
if not self.db:
|
||||||
|
self.loadDb()
|
||||||
return self.db
|
return self.db
|
||||||
|
|
||||||
def updateDbFile(self, inner_path, file=None, cur=None):
|
def updateDbFile(self, inner_path, file=None, cur=None):
|
||||||
path = self.getPath(inner_path)
|
path = self.getPath(inner_path)
|
||||||
return self.getDb().updateJson(path, file, cur)
|
if cur:
|
||||||
|
db = cur.db
|
||||||
|
else:
|
||||||
|
db = self.getDb()
|
||||||
|
return db.updateJson(path, file, cur)
|
||||||
|
|
||||||
# Return possible db files for the site
|
# Return possible db files for the site
|
||||||
@thread_pool_fs_read.wrap
|
@thread_pool_fs_read.wrap
|
||||||
|
|
Loading…
Reference in a new issue