Browse Source

feat: global task manager

master
dnomd343 2 years ago
parent
commit
b5c71fe37c
  1. 4
      Basis/Logger.py
  2. 87
      Basis/Manage.py
  3. 56
      manageDemo.py

4
Basis/Logger.py

@ -6,8 +6,8 @@ import logging
from colorlog import ColoredFormatter from colorlog import ColoredFormatter
logFile = 'runtime.log' logFile = 'runtime.log'
# logLevel = logging.DEBUG logLevel = logging.DEBUG
logLevel = logging.WARNING # logLevel = logging.WARNING
dateFormat = '%Y-%m-%d %H:%M:%S' dateFormat = '%Y-%m-%d %H:%M:%S'
logFormat = '[%(asctime)s] [%(levelname)s] %(message)s (%(module)s.%(funcName)s)' logFormat = '[%(asctime)s] [%(levelname)s] %(message)s (%(module)s.%(funcName)s)'
logging.basicConfig( logging.basicConfig(

87
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

56
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()))
Loading…
Cancel
Save