mirror of https://github.com/dnomd343/ProxyC
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
4.4 KiB
107 lines
4.4 KiB
2 years ago
|
#!/usr/bin/env python3
|
||
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
import copy
|
||
2 years ago
|
from enum import Enum
|
||
|
from Utils.Logger import logger
|
||
|
from Utils.Common import genFlag
|
||
|
from Utils.Exception import managerException
|
||
|
|
||
|
|
||
|
class TASK(Enum): # task status
|
||
|
LOADED = -1
|
||
|
RUNNING = 0
|
||
|
FINISH = 1
|
||
|
|
||
2 years ago
|
|
||
|
class Task(object):
|
||
|
""" Manage global check task.
|
||
|
"""
|
||
|
__tasks = {} # task status -> loaded / running / complete
|
||
|
__unions = {} # one union include multi tasks
|
||
|
|
||
|
def __init__(self):
|
||
2 years ago
|
logger.info('Manager start')
|
||
2 years ago
|
|
||
|
def listUnion(self) -> list: # get all union ids
|
||
|
return [x for x in self.__unions]
|
||
|
|
||
|
def isUnion(self, unionId: str) -> bool: # check if the union id exist
|
||
|
return unionId in self.__unions
|
||
|
|
||
|
def addUnion(self, union: list) -> str: # add union to manager (include multi tasks)
|
||
|
tasks = {} # temporary task storage
|
||
|
taskIds = [] # task id list for manage union
|
||
2 years ago
|
unionId = genFlag(length = 12) # generate union id (12 bytes)
|
||
2 years ago
|
logger.debug('Manager start to load union [%s]' % unionId)
|
||
2 years ago
|
for task in union:
|
||
2 years ago
|
taskId = genFlag(length = 16) # generate task id (16 bytes)
|
||
2 years ago
|
taskIds.append(taskId)
|
||
|
tasks[taskId] = {
|
||
2 years ago
|
'status': TASK.LOADED, # task status -> loaded
|
||
2 years ago
|
'data': copy.deepcopy(task) # save task info
|
||
|
}
|
||
2 years ago
|
logger.info('Manager add task [%s] -> %s' % (taskId, task))
|
||
2 years ago
|
self.__tasks.update(tasks) # load into task list
|
||
|
self.__unions[unionId] = {
|
||
2 years ago
|
'finish': False,
|
||
2 years ago
|
'items': taskIds # record task items
|
||
|
}
|
||
2 years ago
|
logger.info('Manager add union [%s] -> %s' % (unionId, taskIds))
|
||
2 years ago
|
return unionId
|
||
|
|
||
2 years ago
|
def delUnion(self, unionId) -> None: # remove union
|
||
|
if unionId not in self.__unions:
|
||
2 years ago
|
logger.error('Manager union [%s] not found' % unionId)
|
||
2 years ago
|
raise managerException('Union id not found')
|
||
|
if not self.__unions[unionId]['finish']: # some tasks are still running
|
||
|
raise managerException('Couldn\'t remove working union')
|
||
|
self.__unions.pop(unionId)
|
||
|
|
||
2 years ago
|
def getUnion(self, unionId: str) -> dict: # get union status (remove tasks when all completed)
|
||
|
if unionId not in self.__unions:
|
||
2 years ago
|
logger.error('Manager union [%s] not found' % unionId)
|
||
2 years ago
|
raise managerException('Union id not found')
|
||
2 years ago
|
if self.__unions[unionId]['finish']: # all tasks are finished
|
||
|
return self.__unions[unionId]
|
||
2 years ago
|
tasks = self.__unions[unionId]['items']
|
||
|
finishNum = 0
|
||
|
for taskId in tasks:
|
||
2 years ago
|
if self.__tasks[taskId]['status'] == TASK.FINISH: # get number of completed task
|
||
2 years ago
|
finishNum += 1
|
||
2 years ago
|
logger.info('Manager statistics union [%s] -> %i/%i' % (unionId, finishNum, len(tasks)))
|
||
2 years ago
|
if finishNum < len(tasks): # some tasks are not yet completed
|
||
2 years ago
|
logger.debug('Manager union [%s] still working' % unionId)
|
||
2 years ago
|
return {
|
||
|
'finish': False,
|
||
2 years ago
|
'percent': round(finishNum / len(tasks), 2)
|
||
2 years ago
|
}
|
||
2 years ago
|
self.__unions[unionId]['result'] = []
|
||
2 years ago
|
for taskId in tasks:
|
||
|
task = self.__tasks[taskId]
|
||
|
self.__tasks.pop(taskId) # remove from task list
|
||
2 years ago
|
self.__unions[unionId]['result'].append(task['data'])
|
||
|
self.__unions[unionId]['finish'] = True
|
||
|
self.__unions[unionId].pop('items')
|
||
2 years ago
|
logger.info('Manager release union [%s] -> %s' % (unionId, self.__unions[unionId]['result']))
|
||
2 years ago
|
return self.__unions[unionId]
|
||
2 years ago
|
|
||
|
def popTask(self) -> tuple[str or None, any]: # fetch a loaded task
|
||
|
for taskId, task in self.__tasks.items():
|
||
2 years ago
|
if task['status'] != TASK.LOADED: continue # only get loaded task
|
||
|
task['status'] = TASK.RUNNING # set task status as running
|
||
|
logger.info('Manager pop task [%s] -> %s' % (taskId, task['data']))
|
||
2 years ago
|
return taskId, copy.deepcopy(task['data'])
|
||
2 years ago
|
logger.debug('Manager has no more task')
|
||
2 years ago
|
raise managerException('No more tasks')
|
||
2 years ago
|
|
||
|
def finishTask(self, taskId: str, taskData: dict) -> None: # update task data when completed
|
||
|
if taskId not in self.__tasks:
|
||
2 years ago
|
logger.error('Manager task [%s] not found' % taskId)
|
||
2 years ago
|
raise managerException('Task id not found')
|
||
2 years ago
|
self.__tasks[taskId]['data'] = copy.deepcopy(taskData)
|
||
2 years ago
|
self.__tasks[taskId]['status'] = TASK.FINISH # set task status as completed
|
||
2 years ago
|
|
||
|
|
||
|
Manager = Task() # global task manager
|