Browse Source

feat: add more Exception

master^2
dnomd343 2 years ago
parent
commit
8a3ad7dc83
  1. 15
      Basis/Exception.py
  2. 8
      Basis/Manager.py
  3. 23
      Basis/Process.py

15
Basis/Exception.py

@ -9,3 +9,18 @@ class buildException(Exception): # for build error
class filterException(Exception): # for filter error class filterException(Exception): # for filter error
def __init__(self, reason): def __init__(self, reason):
self.reason = 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

8
Basis/Manager.py

@ -4,7 +4,7 @@
import copy import copy
from Basis.Logger import logging from Basis.Logger import logging
from Basis.Functions import genFlag from Basis.Functions import genFlag
from Basis.Exception import managerException
class Task(object): class Task(object):
""" Manage global check task. """ 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) def getUnion(self, unionId: str) -> dict: # get union status (remove tasks when all completed)
if unionId not in self.__unions: if unionId not in self.__unions:
logging.error('Manager union [%s] not found' % unionId) 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'] tasks = self.__unions[unionId]['items']
finishNum = 0 finishNum = 0
for taskId in tasks: for taskId in tasks:
@ -79,12 +79,12 @@ class Task(object):
logging.info('Manager pop task [%s] -> %s' % (taskId, task['data'])) logging.info('Manager pop task [%s] -> %s' % (taskId, task['data']))
return taskId, copy.deepcopy(task['data']) return taskId, copy.deepcopy(task['data'])
logging.debug('Manager has no more task') 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 def finishTask(self, taskId: str, taskData: dict) -> None: # update task data when completed
if taskId not in self.__tasks: if taskId not in self.__tasks:
logging.error('Manager task [%s] not found' % taskId) 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]['data'] = copy.deepcopy(taskData)
self.__tasks[taskId]['status'] = self.__TASK_FINISH # set task status as completed self.__tasks[taskId]['status'] = self.__TASK_FINISH # set task status as completed

23
Basis/Process.py

@ -8,6 +8,7 @@ import ctypes
import signal import signal
from Basis.Logger import logging from Basis.Logger import logging
from Basis.Functions import genFlag from Basis.Functions import genFlag
from Basis.Exception import processException
from subprocess import Popen, STDOUT, DEVNULL from subprocess import Popen, STDOUT, DEVNULL
libcPaths = [ libcPaths = [
@ -73,7 +74,7 @@ class Process(object):
logging.error('[%s] %s already exist but not folder' % (self.id, self.workDir)) logging.error('[%s] %s already exist but not folder' % (self.id, self.workDir))
else: else:
logging.error('[%s] Unable to create new folder -> %s' % (self.id, self.workDir)) 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: def __killProcess(self, killSignal: int) -> None:
try: try:
@ -134,10 +135,10 @@ class Process(object):
)) ))
if self.cmd is None: # ERROR CASE if self.cmd is None: # ERROR CASE
logging.error('[%s] Process miss start command' % self.id) 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 if self.__process is not None and self.__process.poll() is None: # ERROR CASE
logging.error('[%s] Process is still running' % self.id) logging.error('[%s] Process try to start but it is running' % self.id)
raise RuntimeError('Process is still running') 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 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) 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 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 else: # discard all the output of sub process
stdout = DEVNULL stdout = DEVNULL
stderr = DEVNULL stderr = DEVNULL
self.__process = Popen( try:
self.cmd, env = self.env, self.__process = Popen(
stdout = stdout, stderr = stderr, self.cmd, env = self.env,
preexec_fn = None if libcPath is None else Process.__preExec 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)) logging.info('[%s] Process running -> PID = %i' % (self.id, self.__process.pid))
def signal(self, signalNum: int) -> None: # send specified signal to sub process def signal(self, signalNum: int) -> None: # send specified signal to sub process

Loading…
Cancel
Save