From 8a3ad7dc834f5d0750fa41c5724daf05934453fe Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Mon, 8 Aug 2022 15:25:57 +0800 Subject: [PATCH] feat: add more Exception --- Basis/Exception.py | 15 +++++++++++++++ Basis/Manager.py | 8 ++++---- Basis/Process.py | 23 ++++++++++++++--------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/Basis/Exception.py b/Basis/Exception.py index 8192bea..aa841e5 100644 --- a/Basis/Exception.py +++ b/Basis/Exception.py @@ -9,3 +9,18 @@ class buildException(Exception): # for build error class filterException(Exception): # for filter error def __init__(self, reason): self.reason = reason + + +class processException(Exception): # for process error + def __init__(self, reason): + self.reason = reason + + +class managerException(Exception): # for manager error + def __init__(self, reason): + self.reason = reason + + +class checkException(Exception): # for check error + def __init__(self, reason): + self.reason = reason diff --git a/Basis/Manager.py b/Basis/Manager.py index 3d272be..25d25b2 100644 --- a/Basis/Manager.py +++ b/Basis/Manager.py @@ -4,7 +4,7 @@ import copy from Basis.Logger import logging from Basis.Functions import genFlag - +from Basis.Exception import managerException class Task(object): """ Manage global check task. @@ -47,7 +47,7 @@ class Task(object): def getUnion(self, unionId: str) -> dict: # get union status (remove tasks when all completed) if unionId not in self.__unions: logging.error('Manager union [%s] not found' % unionId) - raise RuntimeError('Union id not found') + raise managerException('Union id not found') tasks = self.__unions[unionId]['items'] finishNum = 0 for taskId in tasks: @@ -79,12 +79,12 @@ class Task(object): logging.info('Manager pop task [%s] -> %s' % (taskId, task['data'])) return taskId, copy.deepcopy(task['data']) logging.debug('Manager has no more task') - raise RuntimeError('No more tasks') + raise managerException('No more tasks') def finishTask(self, taskId: str, taskData: dict) -> None: # update task data when completed if taskId not in self.__tasks: logging.error('Manager task [%s] not found' % taskId) - raise RuntimeError('Task id not found') + raise managerException('Task id not found') self.__tasks[taskId]['data'] = copy.deepcopy(taskData) self.__tasks[taskId]['status'] = self.__TASK_FINISH # set task status as completed diff --git a/Basis/Process.py b/Basis/Process.py index 3e3b2a0..5b942e7 100644 --- a/Basis/Process.py +++ b/Basis/Process.py @@ -8,6 +8,7 @@ import ctypes import signal from Basis.Logger import logging from Basis.Functions import genFlag +from Basis.Exception import processException from subprocess import Popen, STDOUT, DEVNULL libcPaths = [ @@ -73,7 +74,7 @@ class Process(object): logging.error('[%s] %s already exist but not folder' % (self.id, self.workDir)) else: logging.error('[%s] Unable to create new folder -> %s' % (self.id, self.workDir)) - raise RuntimeError('Working directory error') # fatal error + raise processException('Working directory error') # fatal error def __killProcess(self, killSignal: int) -> None: try: @@ -134,10 +135,10 @@ class Process(object): )) if self.cmd is None: # ERROR CASE logging.error('[%s] Process miss start command' % self.id) - raise RuntimeError('Miss start command') + raise processException('Miss start command') if self.__process is not None and self.__process.poll() is None: # ERROR CASE - logging.error('[%s] Process is still running' % self.id) - raise RuntimeError('Process is still running') + logging.error('[%s] Process try to start but it is running' % self.id) + raise processException('Process is still running') if self.env is not None and 'PATH' not in self.env and '/' not in self.cmd[0]: # WARNING CASE logging.warning('[%s] Executable file in relative path but miss PATH in environ' % self.id) if self.file is not None: # create and write file contents @@ -153,11 +154,15 @@ class Process(object): else: # discard all the output of sub process stdout = DEVNULL stderr = DEVNULL - self.__process = Popen( - self.cmd, env = self.env, - stdout = stdout, stderr = stderr, - preexec_fn = None if libcPath is None else Process.__preExec - ) + try: + self.__process = Popen( + self.cmd, env = self.env, + stdout = stdout, stderr = stderr, + preexec_fn = None if libcPath is None else Process.__preExec + ) + except Exception as exp: + logging.error('[%s] Process unable to start -> %s' % (self.id, exp)) + raise processException('Unable to start process') logging.info('[%s] Process running -> PID = %i' % (self.id, self.__process.pid)) def signal(self, signalNum: int) -> None: # send specified signal to sub process