mdserver-web/class/core/task_api.py

104 lines
3.3 KiB
Python
Raw Normal View History

2018-12-25 02:41:46 -05:00
# coding: utf-8
2022-11-17 21:34:18 -05:00
# ---------------------------------------------------------------------------------
# MW-Linux面板
# ---------------------------------------------------------------------------------
# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved.
# ---------------------------------------------------------------------------------
# Author: midoks <midoks@163.com>
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# 任务页操作
# ---------------------------------------------------------------------------------
2018-12-25 02:41:46 -05:00
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
2021-05-01 22:23:02 -04:00
p = request.form.get('p', '1')
2019-01-14 02:28:27 -05:00
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
2022-06-23 06:40:03 -04:00
# return data
_ret['count'] = count
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):
2022-08-10 00:20:57 -04:00
tempFile = mw.getRunDir() + '/tmp/panelExec.log'
freshFile = mw.getRunDir() + '/tmp/panelFresh'
2018-12-25 02:41:46 -05:00
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
2022-07-12 00:21:48 -04:00
mw.triggerTask()
2018-12-25 02:41:46 -05:00
2022-08-10 00:20:57 -04:00
data = {}
data['name'] = find['name']
data['execstr'] = find['execstr']
2018-12-25 02:41:46 -05:00
if find['type'] == 'download':
2022-08-10 00:20:57 -04:00
readLine = ""
for i in range(3):
try:
readLine = mw.readFile(tempFile)
if len(readLine) > 10:
data['msg'] = json.loads(readLine)
data['isDownload'] = True
break
except Exception as e:
if i == 2:
mw.M('tasks').where("id=?", (find['id'],)).save(
'status', ('0',))
return mw.returnJson(False, '当前没有任务队列在执行-4:' + str(e))
time.sleep(0.5)
2018-12-25 02:41:46 -05:00
else:
2022-08-10 00:20:57 -04:00
data['msg'] = mw.getLastLine(tempFile, 10)
data['isDownload'] = False
2018-12-25 02:41:46 -05:00
2022-08-10 00:20:57 -04:00
data['task'] = mw.M('tasks').where("status!=?", ('1',)).field(
2018-12-25 02:41:46 -05:00
'id,status,name,type').order("id asc").select()
2022-08-10 00:20:57 -04:00
return mw.getJson(data)