TaskWriter.py
2.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# coding=utf-8
#author: 4N
#createtime: 2021/12/3
#email: nheweijun@sina.com
import datetime
import uuid
from app.modules.data.models import Process,Task,Table
import configure
from app.util.component.PGUtil import PGUtil
def check_session(fun):
'''
检查session的装饰器
:param fun:
:return:
'''
def wrap(self,*args, **kwargs):
if not self.sys_session.is_active:
self.sys_session = PGUtil.get_db_session(configure.SQLALCHEMY_DATABASE_URI)
fun(self,*args, **kwargs)
return wrap
class TaskWriter:
def __init__(self,task_guid):
self.sys_session = PGUtil.get_db_session(configure.SQLALCHEMY_DATABASE_URI)
self.task_guid = task_guid
@check_session
def update_task(self,update_info,commit=True):
self.sys_session.query(Task).filter_by(guid=self.task_guid).update(update_info)
if commit:
self.sys_session.commit()
@check_session
def update_process(self,msg,commit=True):
message = "{} {}".format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), msg)
task_process_guid = uuid.uuid1().__str__()
task_process = Process(guid=task_process_guid, message=message, time=datetime.datetime.now(),
task_guid=self.task_guid)
self.sys_session.add(task_process)
if commit:
self.sys_session.commit()
@check_session
def update_table(self,table_guid,update_info,commit=True):
self.sys_session.query(Table).filter_by(guid=table_guid).update(update_info)
if commit:
self.sys_session.commit()
def close(self):
try:
self.sys_session.commit()
self.sys_session.close()
except:
pass
@property
def session(self):
if not self.sys_session.is_active:
self.sys_session = PGUtil.get_db_session(configure.SQLALCHEMY_DATABASE_URI)
return self.sys_session