3ec5fee43b
- init project then create folder
181 lines
6.0 KiB
Python
181 lines
6.0 KiB
Python
from json import loads as json_parse, dumps as _json_stringify
|
|
from socket import if_nametoindex
|
|
from typing import Dict, List, Optional, Any
|
|
from xml.dom.expatbuilder import parseString
|
|
import paho.mqtt.client as mqtt
|
|
from biopro.text import *
|
|
from .task import Task
|
|
from biopro.db.collection import Collection
|
|
|
|
_RUNTIME_COMPILE = False
|
|
|
|
def json_stringify(o: Any) -> str:
|
|
return _json_stringify(o, separators=(',', ':'))
|
|
|
|
class TaskManager():
|
|
def __init__(self, task_list, cycle_list):
|
|
self._task_list = []
|
|
self._next_task = []
|
|
self._prev_task = None
|
|
self._running_task = None
|
|
self._cycle_list = cycle_list
|
|
|
|
self.load_task_list(task_list)
|
|
|
|
# self.set_running_task(self._task_list[0])
|
|
# self._running_task.run()
|
|
self._next_task.append(self._task_list[0])
|
|
|
|
@property
|
|
def task_list(self):
|
|
return self._task_list
|
|
|
|
@property
|
|
def export_task_list(self):
|
|
return [d.as_json() for d in self._task_list]
|
|
|
|
@property
|
|
def prev_task(self):
|
|
return self._prev_task
|
|
|
|
@property
|
|
def running_task(self):
|
|
return self._running_task
|
|
|
|
@property
|
|
def next_task(self):
|
|
return self._next_task
|
|
|
|
def get_task_by_index(self, index):
|
|
return self._task_list[index]
|
|
|
|
def get_task_by_uuid(self, uuid):
|
|
return next((x for x in self.task_list if x.uuid == uuid), None)
|
|
|
|
def get_index_by_task(self, task):
|
|
return next((i for i, x in enumerate(self.task_list) if x.uuid == task.uuid), None)
|
|
|
|
def get_index_by_uuid(self, uuid):
|
|
return next((i for i, x in enumerate(self.task_list) if x.uuid == uuid), None)
|
|
|
|
def check_task_is_cycle_start(self, task):
|
|
for index, cycle in enumerate(self._cycle_list):
|
|
if cycle['range'][0] == task.uuid:
|
|
return index
|
|
|
|
def check_task_is_cycle_end(self, task):
|
|
for index, cycle in enumerate(self._cycle_list):
|
|
if cycle['range'][1] == task.uuid:
|
|
return index
|
|
|
|
def check_task_in_cycle(self, task_index):
|
|
for index, cycle in enumerate(self._cycle_list):
|
|
if self.get_index_by_uuid(cycle['range'][0]) <= task_index:
|
|
if self.get_index_by_uuid(cycle['range'][1]) >= task_index:
|
|
return True
|
|
return False
|
|
|
|
@property
|
|
def check_list(self):
|
|
return [self._running_task, *self._next_task]
|
|
|
|
@property
|
|
def check_list(self):
|
|
return [self._running_task, *self._next_task]
|
|
|
|
def load_task_list(self, task_list):
|
|
for task in task_list:
|
|
task = Task(task)
|
|
self._task_list.append(task)
|
|
|
|
# original version
|
|
""" def set_running_task(self, task):
|
|
try:
|
|
# if there is task running & same task active ,then reject
|
|
if self._running_task != None and self._running_task.uuid == task.uuid:
|
|
return False
|
|
|
|
# save running task
|
|
self._prev_task = self._running_task
|
|
# clear next task list
|
|
self._next_task.clear()
|
|
self._running_task = task
|
|
self._running_task.run()
|
|
|
|
if self._prev_task != None:
|
|
# if previous task is still running, then need to close
|
|
if self._prev_task.status == 1:
|
|
self._prev_task.stop()
|
|
print('prev', 'run', self._prev_task.name, self._running_task.name)
|
|
|
|
for task_uuid in self._running_task.next:
|
|
_task = next((task for task in self._task_list if task.uuid == task_uuid), None)
|
|
if _task != None:
|
|
self._next_task.append(_task)
|
|
except RuntimeError as e:
|
|
print(e) """
|
|
|
|
def set_running_task(self, task):
|
|
try:
|
|
# if there is task running & same task active ,then reject
|
|
if self._running_task != None and self._running_task.uuid == task.uuid:
|
|
return False
|
|
|
|
# save task
|
|
self._prev_task = self._running_task
|
|
# clear next task list
|
|
self._next_task.clear()
|
|
# set task to running_task
|
|
self._running_task = task
|
|
self._running_task.run()
|
|
|
|
# if previous task is still running, then need to close
|
|
if self._prev_task != None:
|
|
if self._prev_task.status == 1:
|
|
self._prev_task.stop()
|
|
# if reach cycle end, count < max, then go to cycle start
|
|
cycle_start_index = self.check_task_is_cycle_start(task)
|
|
cycle_index = self.check_task_is_cycle_end(task)
|
|
if cycle_index is None:
|
|
# print('next', self._running_task.name, self._running_task.next)
|
|
for task_uuid in self._running_task.next:
|
|
_task = next((task for task in self._task_list if task.uuid == task_uuid), None)
|
|
if _task != None:
|
|
_task.reset()
|
|
self._next_task.append(_task)
|
|
# print('self._next_task', self._next_task)
|
|
else:
|
|
# print('cycle', self._cycle_list[cycle_index])
|
|
cycle = self._cycle_list[cycle_index]
|
|
if int(cycle['count']) < int(cycle['max']):
|
|
self._next_task.append(self.get_task_by_uuid(cycle['range'][0]))
|
|
# cycle['count'] += 1
|
|
else:
|
|
for task_uuid in self._running_task.next:
|
|
_task = next((task for task in self._task_list if task.uuid == task_uuid), None)
|
|
if _task != None:
|
|
_task.reset()
|
|
self._next_task.append(_task)
|
|
# cycle['count'] = 1
|
|
|
|
if cycle_start_index != None:
|
|
cycle = self._cycle_list[cycle_start_index]
|
|
if int(cycle['count']) < int(cycle['max']):
|
|
cycle['count'] += 1
|
|
else:
|
|
cycle['count'] = 1
|
|
|
|
except RuntimeError as e:
|
|
print(e)
|
|
|
|
def get_task(self, task_id):
|
|
return self._task_list[task_id]
|
|
|
|
def create_collection(self, parent):
|
|
for index, task in enumerate(self._task_list):
|
|
if self.check_task_in_cycle(index) == True:
|
|
if task.type == '':
|
|
collection = Collection.create_collection(task.name, {"folder": [parent.id]})
|
|
task.parent = {"folder": [collection.id]}
|
|
else:
|
|
task.parent = {"folder": [parent.id]} |