improve error handling in FileManagerScheduler
This commit is contained in:
@@ -117,6 +117,16 @@ class DaemonInstance:
|
||||
self._shutdown = True
|
||||
self._log.info(f"got signal {signame}, shutdown")
|
||||
self.stop()
|
||||
|
||||
pending = self._get_pending_tasks()
|
||||
for task in pending:
|
||||
task.cancel()
|
||||
|
||||
try:
|
||||
await asyncio.gather(*pending)
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
pending = self._get_pending_tasks()
|
||||
if pending:
|
||||
tasks_done = False
|
||||
@@ -133,14 +143,8 @@ class DaemonInstance:
|
||||
future.exception()
|
||||
|
||||
if not tasks_done:
|
||||
self._log.warning("terminate remaining task(s)")
|
||||
for task in pending:
|
||||
task.cancel()
|
||||
|
||||
try:
|
||||
await asyncio.gather(*pending)
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
self._log.warning(
|
||||
f"terminate {len(pending)} remaining task(s)")
|
||||
|
||||
asyncio.get_event_loop().stop()
|
||||
self._shutdown = False
|
||||
|
||||
@@ -302,12 +302,12 @@ class FileManagerScheduler(TaskScheduler):
|
||||
dst = rule.src_re.sub(rule.dst_re, path)
|
||||
if not dst:
|
||||
raise RuntimeError(
|
||||
f"{task_id}: unable to {rule.action} '{path}', "
|
||||
f"unable to {rule.action} '{path}', "
|
||||
f"resulting destination path is empty")
|
||||
|
||||
if os.path.exists(dst):
|
||||
raise RuntimeError(
|
||||
f"{task_id}: unable to move file from '{path} "
|
||||
f"unable to move file from '{path} "
|
||||
f"to '{dst}', dstination path exists already")
|
||||
|
||||
dst_dir = os.path.dirname(dst)
|
||||
@@ -321,12 +321,19 @@ class FileManagerScheduler(TaskScheduler):
|
||||
first_subdir = parent
|
||||
else:
|
||||
break
|
||||
|
||||
try:
|
||||
os.makedirs(dst_dir)
|
||||
await asyncio.shield(
|
||||
self._set_mode_and_owner(first_subdir, rule, task_id))
|
||||
self._set_mode_and_owner(
|
||||
first_subdir, rule, task_id))
|
||||
except Exception as e:
|
||||
raise RuntimeError(e)
|
||||
|
||||
self._log.info(
|
||||
f"{task_id}: {rule.action} '{path}' to '{dst}'")
|
||||
|
||||
try:
|
||||
if rule.action == "copy":
|
||||
if os.path.isdir(path):
|
||||
shutil.copytree(path, dst)
|
||||
@@ -338,10 +345,13 @@ class FileManagerScheduler(TaskScheduler):
|
||||
|
||||
await asyncio.shield(
|
||||
self._set_mode_and_owner(dst, rule, task_id))
|
||||
except Exception as e:
|
||||
raise RuntimeError(e)
|
||||
|
||||
elif rule.action == "delete":
|
||||
self._log.info(
|
||||
f"{task_id}: {rule.action} '{path}'")
|
||||
try:
|
||||
if os.path.isdir(path):
|
||||
if rule.rec:
|
||||
shutil.rmtree(path)
|
||||
@@ -350,6 +360,8 @@ class FileManagerScheduler(TaskScheduler):
|
||||
|
||||
else:
|
||||
os.remove(path)
|
||||
except Exception as e:
|
||||
raise RuntimeError(e)
|
||||
|
||||
except RuntimeError as e:
|
||||
self._log.error(f"{task_id}: {e}")
|
||||
|
||||
Reference in New Issue
Block a user