From b5c71fe37cfba37d33b7a1e289eb1b91ea328789 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Mon, 1 Aug 2022 20:53:33 +0800 Subject: [PATCH] feat: global task manager --- Basis/Logger.py | 4 +-- Basis/Manage.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ manageDemo.py | 56 +++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 Basis/Manage.py create mode 100755 manageDemo.py diff --git a/Basis/Logger.py b/Basis/Logger.py index 764bf9e..383cee2 100644 --- a/Basis/Logger.py +++ b/Basis/Logger.py @@ -6,8 +6,8 @@ import logging from colorlog import ColoredFormatter logFile = 'runtime.log' -# logLevel = logging.DEBUG -logLevel = logging.WARNING +logLevel = logging.DEBUG +# logLevel = logging.WARNING dateFormat = '%Y-%m-%d %H:%M:%S' logFormat = '[%(asctime)s] [%(levelname)s] %(message)s (%(module)s.%(funcName)s)' logging.basicConfig( diff --git a/Basis/Manage.py b/Basis/Manage.py new file mode 100644 index 0000000..7f5ee2b --- /dev/null +++ b/Basis/Manage.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import copy +from Basis.Logger import logging +from Basis.Functions import genFlag + + +class Task(object): + """ Manage global check task. + """ + __tasks = {} + __subTasks = {} # sub task status -> loaded / running / complete + + def __init__(self): + logging.info('task manager start') + + def addTask(self, tasks: list) -> str: # add task to manager (multi sub tasks) + subTasks = {} + subTaskIds = [] + for subTask in tasks: + subTaskId = genFlag(length = 24) # generate sub task id (24 bytes) + subTaskIds.append(subTaskId) + subTasks[subTaskId] = { + 'status': 'loaded', + 'data': copy.deepcopy(subTask) + } + logging.info('add sub task %s -> %s' % (subTaskId, subTasks[subTaskId]['data'])) + taskId = genFlag(length = 16) # generate task id (16 bytes) + self.__tasks[taskId] = { # load task + 'sub': subTaskIds + } + self.__subTasks.update(subTasks) # load sub tasks + logging.info('task %s loaded' % taskId) + return taskId + + def isTask(self, taskId: str) -> bool: # check if the task id exist + return taskId in self.__tasks + + def getTask(self, taskId: str) -> dict: # get task status (remove sub tasks when all completed) + if taskId not in self.__tasks: + logging.error('task id %s not found' % taskId) + raise RuntimeError('task id not found') + subList = self.__tasks[taskId]['sub'] + completed = 0 + for subTaskId in subList: + if self.__subTasks[subTaskId]['status'] == 'complete': # get number of completed sub task + completed += 1 + logging.debug('[%s] statistics sub task status -> %i/%i' % (taskId, completed, len(subList))) + if completed < len(subList): # some sub tasks are not completed + logging.debug('[%s] task still running' % taskId) + return { + 'done': False, + 'total': len(subList), + 'finish': completed, + } + logging.debug('[%s] task work complete' % taskId) # all sub tasks completed + result = [] + for subTaskId in subList: + subTask = self.__subTasks[subTaskId] + self.__subTasks.pop(subTaskId) + result.append(subTask['data']) + logging.debug('release sub tasks -> %s' % result) + return { + 'done': True, + 'result': result + } + + def listTask(self) -> list: # get all task ids + return [x for x in self.__tasks] + + def popSubTask(self) -> tuple[str or None, any]: # fetch a loaded sub task + for subTaskId, subTask in self.__subTasks.items(): + if subTask['status'] != 'loaded': continue # only get loaded sub task + subTask['status'] = 'running' # set status as running + return subTaskId, copy.deepcopy(subTask['data']) + return None, None + + def updateSubTask(self, subTaskId: str, subTaskData: dict) -> None: # update sub task data when completed + if subTaskId not in self.__subTasks: + logging.error('sub task id %s not found' % subTaskId) + raise RuntimeError('sub task id not found') + self.__subTasks[subTaskId]['data'] = copy.deepcopy(subTaskData) + self.__subTasks[subTaskId]['status'] = 'complete' + + +Manage = Task() # global task manager diff --git a/manageDemo.py b/manageDemo.py new file mode 100755 index 0000000..fb71a1d --- /dev/null +++ b/manageDemo.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from Basis.Manage import Manage +from Basis.Logger import logging + + +taskId_1 = Manage.addTask([ + {'test': 1}, + {'test': 2}, + {'test': 3}, +]) +logging.critical('task id 1 -> %s' % taskId_1) + +taskId_2 = Manage.addTask([ + {'demo': 1}, + {'demo': 2}, + {'demo': 3}, +]) +logging.critical('task id 2 -> %s' % taskId_2) + +logging.critical('list task -> %s' % Manage.listTask()) + +logging.critical('is task 1234 -> %s' % Manage.isTask('1234')) +logging.critical('is task %s -> %s' % (taskId_1, Manage.isTask(taskId_1))) + +logging.critical('get task %s -> %s' % (taskId_1, Manage.getTask(taskId_1))) + +subTaskId_1, subTask_1 = Manage.popSubTask() +logging.critical('pop sub task %s -> %s' % (subTaskId_1, subTask_1)) +subTaskId_2, subTask_2 = Manage.popSubTask() +logging.critical('pop sub task %s -> %s' % (subTaskId_2, subTask_2)) +subTaskId_3, subTask_3 = Manage.popSubTask() +logging.critical('pop sub task %s -> %s' % (subTaskId_3, subTask_3)) + +Manage.updateSubTask(subTaskId_3, {'test': 33}) +logging.critical('update sub task %s' % subTaskId_3) +Manage.updateSubTask(subTaskId_2, {'test': 22}) +logging.critical('update sub task %s' % subTaskId_2) +Manage.updateSubTask(subTaskId_1, {'test': 11}) +logging.critical('update sub task %s' % subTaskId_1) + +logging.critical('get task %s -> %s' % (taskId_1, Manage.getTask(taskId_1))) + +subTaskId_4, subTask_4 = Manage.popSubTask() +logging.critical('pop sub task %s -> %s' % (subTaskId_4, subTask_4)) + +Manage.updateSubTask(subTaskId_4, {'demo': 2333}) +logging.critical('update sub task %s' % subTaskId_4) + +logging.critical('get task %s -> %s' % (taskId_2, Manage.getTask(taskId_2))) + +logging.critical('sub task pop -> ' + str(Manage.popSubTask())) +logging.critical('sub task pop -> ' + str(Manage.popSubTask())) +logging.critical('sub task pop -> ' + str(Manage.popSubTask())) +