mdserver-web/class/core/task_api.py

87 lines
2.6 KiB
Python
Raw Permalink Normal View History

2018-12-25 02:41:46 -05:00
# coding: utf-8
import psutil
import time
import os
import sys
2020-07-10 03:57:25 -04:00
import mw
2018-12-25 02:41:46 -05:00
import re
import json
import pwd
2019-01-14 02:28:27 -05:00
from flask import request
2018-12-25 02:41:46 -05:00
class task_api:
def __init__(self):
pass
2018-12-25 04:37:08 -05:00
def countApi(self):
2020-07-10 03:57:25 -04:00
c = mw.M('tasks').where("status!=?", ('1',)).count()
2018-12-25 02:41:46 -05:00
return str(c)
2018-12-25 04:43:28 -05:00
def listApi(self):
2019-01-14 02:28:27 -05:00
p = request.form.get('p', '1').encode('utf-8')
limit = request.form.get('limit', '10').strip()
search = request.form.get('search', '').strip()
start = (int(p) - 1) * int(limit)
limit_str = str(start) + ',' + str(limit)
2020-07-10 03:57:25 -04:00
_list = mw.M('tasks').where('', ()).field(
2019-01-14 02:28:27 -05:00
'id,name,type,status,addtime,start,end').limit(limit_str).order('id desc').select()
2018-12-25 02:41:46 -05:00
_ret = {}
_ret['data'] = _list
2020-07-10 03:57:25 -04:00
count = mw.M('tasks').where('', ()).count()
2018-12-25 02:41:46 -05:00
_page = {}
_page['count'] = count
_page['tojs'] = 'remind'
2019-01-14 02:28:27 -05:00
_page['p'] = p
2018-12-25 02:41:46 -05:00
2020-07-10 03:57:25 -04:00
_ret['page'] = mw.getPage(_page)
return mw.getJson(_ret)
2018-12-25 02:41:46 -05:00
2018-12-25 04:43:28 -05:00
def getExecLogApi(self):
2018-12-25 02:41:46 -05:00
file = os.getcwd() + "/tmp/panelExec.log"
2020-07-10 03:57:25 -04:00
v = mw.getLastLine(file, 100)
2018-12-25 02:41:46 -05:00
return v
2018-12-25 04:43:28 -05:00
def getTaskSpeedApi(self):
2018-12-25 02:41:46 -05:00
tempFile = os.getcwd() + '/tmp/panelExec.log'
freshFile = os.getcwd() + '/tmp/panelFresh'
2020-07-10 03:57:25 -04:00
find = mw.M('tasks').where('status=? OR status=?',
('-1', '0')).field('id,type,name,execstr').find()
2018-12-25 02:41:46 -05:00
if not len(find):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '当前没有任务队列在执行-2!')
2018-12-25 02:41:46 -05:00
isTask = os.getcwd() + '/tmp/panelTask.pl'
2020-07-10 03:57:25 -04:00
mw.writeFile(isTask, 'True')
2018-12-25 02:41:46 -05:00
echoMsg = {}
echoMsg['name'] = find['name']
echoMsg['execstr'] = find['execstr']
if find['type'] == 'download':
import json
try:
2020-07-10 03:57:25 -04:00
tmp = mw.readFile(tempFile)
2018-12-25 02:41:46 -05:00
if len(tmp) < 10:
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '当前没有任务队列在执行-3!')
2018-12-25 02:41:46 -05:00
echoMsg['msg'] = json.loads(tmp)
echoMsg['isDownload'] = True
except:
2020-07-10 03:57:25 -04:00
mw.M('tasks').where(
2018-12-25 02:41:46 -05:00
"id=?", (find['id'],)).save('status', ('0',))
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '当前没有任务队列在执行-4!')
2018-12-25 02:41:46 -05:00
else:
2020-07-10 03:57:25 -04:00
echoMsg['msg'] = mw.getLastLine(tempFile, 10)
2018-12-25 02:41:46 -05:00
echoMsg['isDownload'] = False
2020-07-10 03:57:25 -04:00
echoMsg['task'] = mw.M('tasks').where("status!=?", ('1',)).field(
2018-12-25 02:41:46 -05:00
'id,status,name,type').order("id asc").select()
2020-07-10 03:57:25 -04:00
return mw.getJson(echoMsg)