Skip to content

Commit

Permalink
Try and catch block for dbRebuild (HelloZeroNet#2047)
Browse files Browse the repository at this point in the history
* Try and catch block for dbRebuild

* Use self.log.error and not logging

* Use self.log.error and not logging in SiteStorage also

* Check if the rebuild is working
  • Loading branch information
rllola authored and HelloZeroNet committed Jun 23, 2019
1 parent 9a267ff commit 753396a
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 23 deletions.
7 changes: 4 additions & 3 deletions plugins/Sidebar/SidebarPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,9 +786,10 @@ def actionDbRebuild(self, to):
if "ADMIN" not in permissions:
return self.response(to, "You don't have permission to run this command")

result = self.site.storage.rebuildDb()
try:
self.site.storage.rebuildDb()
except Exception as err:
return self.response(to, {"error": str(err)})

if not result:
return self.response(to, {"error": "Failed to rebuild database"})

return self.response(to, "ok")
1 change: 1 addition & 0 deletions plugins/Sidebar/media/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ window.initScrollable = function () {
this.tag.find("#button-dbrebuild").off("click touchend").on("click touchend", (function(_this) {
return function() {
_this.wrapper.notifications.add("done-dbrebuild", "info", "Database rebuilding....");

_this.wrapper.ws.cmd("dbRebuild", [], function(response) {

if (response !== "ok") {
Expand Down
12 changes: 9 additions & 3 deletions src/Content/ContentDb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
import os

from Db.Db import Db
from Db.Db import Db, DbTableError
from Config import config
from Plugin import PluginManager
from Debug import Debug
Expand All @@ -14,7 +14,10 @@ def __init__(self, path):
self.foreign_keys = True
try:
self.schema = self.getSchema()
self.checkTables()
try:
self.checkTables()
except DbTableError:
pass
self.log.debug("Checking foreign keys...")
foreign_key_error = self.execute("PRAGMA foreign_key_check").fetchone()
if foreign_key_error:
Expand All @@ -26,7 +29,10 @@ def __init__(self, path):
Db.__init__(self, {"db_name": "ContentDb", "tables": {}}, path)
self.foreign_keys = True
self.schema = self.getSchema()
self.checkTables()
try:
self.checkTables()
except DbTableError:
pass
self.site_ids = {}
self.sites = {}

Expand Down
11 changes: 9 additions & 2 deletions src/Db/Db.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def dbCloseAll():
gevent.spawn(dbCommitCheck)
atexit.register(dbCloseAll)

class DbTableError(Exception):
def __init__(self, message, table):
super().__init__(message)
self.table = table

class Db(object):

Expand Down Expand Up @@ -256,15 +260,18 @@ def checkTables(self):
# Check schema tables
for table_name, table_settings in self.schema.get("tables", {}).items():
try:
indexes = table_settings.get("indexes", [])
version = table_settings.get("schema_changed", 0)
changed = cur.needTable(
table_name, table_settings["cols"],
table_settings.get("indexes", []), version=table_settings.get("schema_changed", 0)
indexes, version=version
)
if changed:
changed_tables.append(table_name)
except Exception as err:
self.log.error("Error creating table %s: %s" % (table_name, Debug.formatException(err)))
return False
raise DbTableError(err, table_name)
#return False

self.log.debug("Db check done in %.3fs, changed tables: %s" % (time.time() - s, changed_tables))
if changed_tables:
Expand Down
5 changes: 4 additions & 1 deletion src/Site/Site.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,10 @@ def clone(self, address, privatekey=None, address_index=None, root_inner_path=""
# Rebuild DB
if new_site.storage.isFile("dbschema.json"):
new_site.storage.closeDb()
new_site.storage.rebuildDb()
try:
new_site.storage.rebuildDb()
except Exception as err:
self.log.error(err)

return new_site

Expand Down
32 changes: 20 additions & 12 deletions src/Site/SiteStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import util
from util import SafeRe
from Db.Db import Db
from Db.Db import Db, DbTableError
from Debug import Debug
from Config import config
from util import helper
Expand Down Expand Up @@ -73,15 +73,21 @@ def getDb(self):
schema = self.getDbSchema()
db_path = self.getPath(schema["db_file"])
if not os.path.isfile(db_path) or os.path.getsize(db_path) == 0:
self.rebuildDb()
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)

changed_tables = self.db.checkTables()
if changed_tables:
self.rebuildDb(delete_db=False) # TODO: only update the changed table datas
try:
changed_tables = self.db.checkTables()
if changed_tables:
self.rebuildDb(delete_db=False) # TODO: only update the changed table datas
except sqlite3.OperationalError:
pass

return self.db

Expand Down Expand Up @@ -138,11 +144,10 @@ def rebuildDb(self, delete_db=True):
self.event_db_busy = gevent.event.AsyncResult()

self.log.info("Creating tables...")
changed_tables = self.db.checkTables()
if not changed_tables:
# It failed
# "Error creating table..."
return False

# raise DbTableError if not valid
self.db.checkTables()

cur = self.db.getCursor()
cur.logging = False
s = time.time()
Expand Down Expand Up @@ -195,7 +200,10 @@ def query(self, query, params=None):
except sqlite3.DatabaseError as err:
if err.__class__.__name__ == "DatabaseError":
self.log.error("Database error: %s, query: %s, try to rebuilding it..." % (err, query))
self.rebuildDb()
try:
self.rebuildDb()
except sqlite3.OperationalError:
pass
res = self.db.cur.execute(query, params)
else:
raise err
Expand Down
3 changes: 3 additions & 0 deletions src/Test/TestSiteStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ def testList(self, site):

# Subdir
assert set(site.storage.list("data-default")) == set(["data.json", "users"])

def testDbRebuild(self, site):
assert site.storage.rebuildDb()
7 changes: 5 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,11 @@ def dbRebuild(self, address):
logging.info("Rebuilding site sql cache: %s..." % address)
site = SiteManager.site_manager.get(address)
s = time.time()
site.storage.rebuildDb()
logging.info("Done in %.3fs" % (time.time() - s))
try:
site.storage.rebuildDb()
logging.info("Done in %.3fs" % (time.time() - s))
except Exception as err:
logging.error(err)

def dbQuery(self, address, query):
from Site.Site import Site
Expand Down

0 comments on commit 753396a

Please sign in to comment.