|
@ -2,21 +2,26 @@ |
|
|
# -*- coding: utf-8 -*- |
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
|
|
import copy |
|
|
import copy |
|
|
from Basis.Logger import logging |
|
|
from enum import Enum |
|
|
from Basis.Functions import genFlag |
|
|
from Utils.Logger import logger |
|
|
from Basis.Exception import managerException |
|
|
from Utils.Common import genFlag |
|
|
|
|
|
from Utils.Exception import managerException |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TASK(Enum): # task status |
|
|
|
|
|
LOADED = -1 |
|
|
|
|
|
RUNNING = 0 |
|
|
|
|
|
FINISH = 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Task(object): |
|
|
class Task(object): |
|
|
""" Manage global check task. |
|
|
""" Manage global check task. |
|
|
""" |
|
|
""" |
|
|
__tasks = {} # task status -> loaded / running / complete |
|
|
__tasks = {} # task status -> loaded / running / complete |
|
|
__unions = {} # one union include multi tasks |
|
|
__unions = {} # one union include multi tasks |
|
|
__TASK_LOADED = -1 |
|
|
|
|
|
__TASK_RUNNING = 0 |
|
|
|
|
|
__TASK_FINISH = 1 |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
def __init__(self): |
|
|
logging.info('Manager start') |
|
|
logger.info('Manager start') |
|
|
|
|
|
|
|
|
def listUnion(self) -> list: # get all union ids |
|
|
def listUnion(self) -> list: # get all union ids |
|
|
return [x for x in self.__unions] |
|
|
return [x for x in self.__unions] |
|
@ -28,26 +33,26 @@ class Task(object): |
|
|
tasks = {} # temporary task storage |
|
|
tasks = {} # temporary task storage |
|
|
taskIds = [] # task id list for manage union |
|
|
taskIds = [] # task id list for manage union |
|
|
unionId = genFlag(length = 12) # generate union id (12 bytes) |
|
|
unionId = genFlag(length = 12) # generate union id (12 bytes) |
|
|
logging.debug('Manager start to load union [%s]' % unionId) |
|
|
logger.debug('Manager start to load union [%s]' % unionId) |
|
|
for task in union: |
|
|
for task in union: |
|
|
taskId = genFlag(length = 16) # generate task id (16 bytes) |
|
|
taskId = genFlag(length = 16) # generate task id (16 bytes) |
|
|
taskIds.append(taskId) |
|
|
taskIds.append(taskId) |
|
|
tasks[taskId] = { |
|
|
tasks[taskId] = { |
|
|
'status': self.__TASK_LOADED, # task status -> loaded |
|
|
'status': TASK.LOADED, # task status -> loaded |
|
|
'data': copy.deepcopy(task) # save task info |
|
|
'data': copy.deepcopy(task) # save task info |
|
|
} |
|
|
} |
|
|
logging.info('Manager add task [%s] -> %s' % (taskId, task)) |
|
|
logger.info('Manager add task [%s] -> %s' % (taskId, task)) |
|
|
self.__tasks.update(tasks) # load into task list |
|
|
self.__tasks.update(tasks) # load into task list |
|
|
self.__unions[unionId] = { |
|
|
self.__unions[unionId] = { |
|
|
'finish': False, |
|
|
'finish': False, |
|
|
'items': taskIds # record task items |
|
|
'items': taskIds # record task items |
|
|
} |
|
|
} |
|
|
logging.info('Manager add union [%s] -> %s' % (unionId, taskIds)) |
|
|
logger.info('Manager add union [%s] -> %s' % (unionId, taskIds)) |
|
|
return unionId |
|
|
return unionId |
|
|
|
|
|
|
|
|
def delUnion(self, unionId) -> None: # remove union |
|
|
def delUnion(self, unionId) -> None: # remove union |
|
|
if unionId not in self.__unions: |
|
|
if unionId not in self.__unions: |
|
|
logging.error('Manager union [%s] not found' % unionId) |
|
|
logger.error('Manager union [%s] not found' % unionId) |
|
|
raise managerException('Union id not found') |
|
|
raise managerException('Union id not found') |
|
|
if not self.__unions[unionId]['finish']: # some tasks are still running |
|
|
if not self.__unions[unionId]['finish']: # some tasks are still running |
|
|
raise managerException('Couldn\'t remove working union') |
|
|
raise managerException('Couldn\'t remove working union') |
|
@ -55,18 +60,18 @@ 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) |
|
|
logger.error('Manager union [%s] not found' % unionId) |
|
|
raise managerException('Union id not found') |
|
|
raise managerException('Union id not found') |
|
|
if self.__unions[unionId]['finish']: # all tasks are finished |
|
|
if self.__unions[unionId]['finish']: # all tasks are finished |
|
|
return self.__unions[unionId] |
|
|
return self.__unions[unionId] |
|
|
tasks = self.__unions[unionId]['items'] |
|
|
tasks = self.__unions[unionId]['items'] |
|
|
finishNum = 0 |
|
|
finishNum = 0 |
|
|
for taskId in tasks: |
|
|
for taskId in tasks: |
|
|
if self.__tasks[taskId]['status'] == self.__TASK_FINISH: # get number of completed task |
|
|
if self.__tasks[taskId]['status'] == TASK.FINISH: # get number of completed task |
|
|
finishNum += 1 |
|
|
finishNum += 1 |
|
|
logging.info('Manager statistics union [%s] -> %i/%i' % (unionId, finishNum, len(tasks))) |
|
|
logger.info('Manager statistics union [%s] -> %i/%i' % (unionId, finishNum, len(tasks))) |
|
|
if finishNum < len(tasks): # some tasks are not yet completed |
|
|
if finishNum < len(tasks): # some tasks are not yet completed |
|
|
logging.debug('Manager union [%s] still working' % unionId) |
|
|
logger.debug('Manager union [%s] still working' % unionId) |
|
|
return { |
|
|
return { |
|
|
'finish': False, |
|
|
'finish': False, |
|
|
'percent': round(finishNum / len(tasks), 2) |
|
|
'percent': round(finishNum / len(tasks), 2) |
|
@ -78,24 +83,24 @@ class Task(object): |
|
|
self.__unions[unionId]['result'].append(task['data']) |
|
|
self.__unions[unionId]['result'].append(task['data']) |
|
|
self.__unions[unionId]['finish'] = True |
|
|
self.__unions[unionId]['finish'] = True |
|
|
self.__unions[unionId].pop('items') |
|
|
self.__unions[unionId].pop('items') |
|
|
logging.info('Manager release union [%s] -> %s' % (unionId, self.__unions[unionId]['result'])) |
|
|
logger.info('Manager release union [%s] -> %s' % (unionId, self.__unions[unionId]['result'])) |
|
|
return self.__unions[unionId] |
|
|
return self.__unions[unionId] |
|
|
|
|
|
|
|
|
def popTask(self) -> tuple[str or None, any]: # fetch a loaded task |
|
|
def popTask(self) -> tuple[str or None, any]: # fetch a loaded task |
|
|
for taskId, task in self.__tasks.items(): |
|
|
for taskId, task in self.__tasks.items(): |
|
|
if task['status'] != self.__TASK_LOADED: continue # only get loaded task |
|
|
if task['status'] != TASK.LOADED: continue # only get loaded task |
|
|
task['status'] = self.__TASK_RUNNING # set task status as running |
|
|
task['status'] = TASK.RUNNING # set task status as running |
|
|
logging.info('Manager pop task [%s] -> %s' % (taskId, task['data'])) |
|
|
logger.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') |
|
|
logger.debug('Manager has no more task') |
|
|
raise managerException('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) |
|
|
logger.error('Manager task [%s] not found' % taskId) |
|
|
raise managerException('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'] = TASK.FINISH # set task status as completed |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Manager = Task() # global task manager |
|
|
Manager = Task() # global task manager |