Skip to content

Commit 5ac6354

Browse files
committed
Handle and log all exceptions when writing to databaes to free up the lock
1 parent 359e838 commit 5ac6354

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

jupyter_server_fileid/manager.py

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ def _move_recursive(self, old_path: str, new_path: str, path_mgr: Any = os.path)
121121
for record in records:
122122
id, old_recpath = record
123123
new_recpath = path_mgr.join(new_path, path_mgr.relpath(old_recpath, start=old_path))
124-
self.con.execute("UPDATE Files SET path = ? WHERE id = ?", (new_recpath, id))
124+
try:
125+
self.con.execute("UPDATE Files SET path = ? WHERE id = ?", (new_recpath, id))
126+
except Exception as err:
127+
self.log.error(err)
125128

126129
def _copy_recursive(self, from_path: str, to_path: str, path_mgr: Any = os.path) -> None:
127130
"""Copy all children of a given directory at `from_path` to a new
@@ -134,9 +137,12 @@ def _copy_recursive(self, from_path: str, to_path: str, path_mgr: Any = os.path)
134137
for record in records:
135138
(from_recpath,) = record
136139
to_recpath = path_mgr.join(to_path, path_mgr.relpath(from_recpath, start=from_path))
137-
self.con.execute(
138-
"INSERT INTO Files (id, path) VALUES (?, ?)", (self._uuid(), to_recpath)
139-
)
140+
try:
141+
self.con.execute(
142+
"INSERT INTO Files (id, path) VALUES (?, ?)", (self._uuid(), to_recpath)
143+
)
144+
except Exception as err:
145+
self.log.error(err)
140146

141147
def _delete_recursive(self, path: str, path_mgr: Any = os.path) -> None:
142148
"""Delete all children of a given directory, delimited by `sep`."""
@@ -313,7 +319,10 @@ def _from_normalized_path(self, path: Optional[str]) -> Optional[str]:
313319
def _create(self, path: str) -> str:
314320
path = self._normalize_path(path)
315321
id = self._uuid()
316-
self.con.execute("INSERT INTO Files (id, path) VALUES (?, ?)", (id, path))
322+
try:
323+
self.con.execute("INSERT INTO Files (id, path) VALUES (?, ?)", (id, path))
324+
except Exception as err:
325+
self.log.error(err)
317326
return id
318327

319328
def index(self, path: str) -> str:
@@ -346,7 +355,10 @@ def move(self, old_path: str, new_path: str) -> None:
346355
id = row and row[0]
347356

348357
if id:
349-
self.con.execute("UPDATE Files SET path = ? WHERE path = ?", (new_path, old_path))
358+
try:
359+
self.con.execute("UPDATE Files SET path = ? WHERE path = ?", (new_path, old_path))
360+
except Exception as err:
361+
self.log.error(err)
350362
self._move_recursive(old_path, new_path, posixpath)
351363
else:
352364
id = self._create(new_path)
@@ -673,10 +685,13 @@ def _create(self, path, stat_info):
673685
have a unique `ino`.
674686
"""
675687
id = self._uuid()
676-
self.con.execute(
677-
"INSERT INTO Files (id, path, ino, crtime, mtime, is_dir) VALUES (?, ?, ?, ?, ?, ?)",
678-
(id, path, stat_info.ino, stat_info.crtime, stat_info.mtime, stat_info.is_dir),
679-
)
688+
try:
689+
self.con.execute(
690+
"INSERT INTO Files (id, path, ino, crtime, mtime, is_dir) VALUES (?, ?, ?, ?, ?, ?)",
691+
(id, path, stat_info.ino, stat_info.crtime, stat_info.mtime, stat_info.is_dir),
692+
)
693+
except Exception as err:
694+
self.log.error(err)
680695
return id
681696

682697
def _update(self, id, stat_info=None, path=None):
@@ -694,26 +709,29 @@ def _update(self, id, stat_info=None, path=None):
694709
dangerous and may throw a runtime error if the file is not guaranteed to
695710
have a unique `ino`.
696711
"""
697-
if stat_info and path:
698-
self.con.execute(
699-
"UPDATE Files SET ino = ?, crtime = ?, mtime = ?, path = ? WHERE id = ?",
700-
(stat_info.ino, stat_info.crtime, stat_info.mtime, path, id),
701-
)
702-
return
703-
704-
if stat_info:
705-
self.con.execute(
706-
"UPDATE Files SET ino = ?, crtime = ?, mtime = ? WHERE id = ?",
707-
(stat_info.ino, stat_info.crtime, stat_info.mtime, id),
708-
)
709-
return
710-
711-
if path:
712-
self.con.execute(
713-
"UPDATE Files SET path = ? WHERE id = ?",
714-
(path, id),
715-
)
716-
return
712+
try:
713+
if stat_info and path:
714+
self.con.execute(
715+
"UPDATE Files SET ino = ?, crtime = ?, mtime = ?, path = ? WHERE id = ?",
716+
(stat_info.ino, stat_info.crtime, stat_info.mtime, path, id),
717+
)
718+
return
719+
720+
if stat_info:
721+
self.con.execute(
722+
"UPDATE Files SET ino = ?, crtime = ?, mtime = ? WHERE id = ?",
723+
(stat_info.ino, stat_info.crtime, stat_info.mtime, id),
724+
)
725+
return
726+
727+
if path:
728+
self.con.execute(
729+
"UPDATE Files SET path = ? WHERE id = ?",
730+
(path, id),
731+
)
732+
return
733+
except Exception as err:
734+
self.log.error(err)
717735

718736
def index(self, path, stat_info=None, commit=True):
719737
"""Returns the file ID for the file at `path`, creating a new file ID if

0 commit comments

Comments
 (0)