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. 13
      Basis/Process.py

15
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

8
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

13
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
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

Loading…
Cancel
Save