1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-02 23:48:47 +00:00

Catch exception in thread pool when migrate repos (#7320)

* Catch exception in thread pool when migrate repos

* Add comment and use different exit code

* Check exception only

---------

Co-authored-by: 杨赫然 <heran.yang@seafile.com>
This commit is contained in:
feiniks
2025-01-13 16:58:56 +08:00
committed by GitHub
parent de75571ae6
commit dd3003a693
2 changed files with 21 additions and 8 deletions

View File

@@ -16,10 +16,11 @@ from seafobj.objstore_factory import SeafObjStoreFactory
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
class Worker(Thread):
def __init__(self, do_work, task_queue):
def __init__(self, do_work, task_queue, pool):
Thread.__init__(self)
self.do_work = do_work
self.task_queue = task_queue
self.pool = pool
def run(self):
while True:
@@ -29,6 +30,7 @@ class Worker(Thread):
break
self.do_work(task)
except Exception as e:
self.pool.exception = e
logging.warning('Failed to execute task: %s' % e)
finally:
self.task_queue.task_done()
@@ -37,11 +39,13 @@ class ThreadPool(object):
def __init__(self, do_work, nworker=20):
self.do_work = do_work
self.nworker = nworker
# The pool's exception will be set when an exception occurs in the worker processing the migration object.
self.exception = None
self.task_queue = queue.Queue(maxsize = 2000)
def start(self):
for i in range(self.nworker):
Worker(self.do_work, self.task_queue).start()
Worker(self.do_work, self.task_queue, self).start()
def put_task(self, task):
self.task_queue.put(task)
@@ -72,7 +76,6 @@ class ObjMigrateWorker(Thread):
self.dest_objs = {}
self.object_list_file_path = ''
self.fd = None
self.exit_code = 0
self.exception = None
self.decrypt = decrypt
@@ -80,7 +83,6 @@ class ObjMigrateWorker(Thread):
try:
self._run()
except Exception as e:
self.exit_code = 1
self.exception = e
def _run(self):
@@ -133,6 +135,7 @@ class ObjMigrateWorker(Thread):
self.thread_pool.start()
self.migrate()
self.thread_pool.join()
self.exception = self.thread_pool.exception
if self.object_list_file_path:
self.fd.close()
logging.info('Complete migrate [%s] object' % self.dtype)