Simple Linux Panel
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.
mdserver-web/class/core/task_api.py

104 lines
3.3 KiB

6 years ago
# coding: utf-8
3 years ago
# ---------------------------------------------------------------------------------
# MW-Linux面板
# ---------------------------------------------------------------------------------
# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved.
# ---------------------------------------------------------------------------------
# Author: midoks <midoks@163.com>
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# 任务页操作
# ---------------------------------------------------------------------------------
6 years ago
import psutil
import time
import os
import sys
import mw
6 years ago
import re
import json
import pwd
6 years ago
from flask import request
6 years ago
class task_api:
def __init__(self):
pass
6 years ago
def countApi(self):
c = mw.M('tasks').where("status!=?", ('1',)).count()
6 years ago
return str(c)
6 years ago
def listApi(self):
6 years ago
4 years ago
p = request.form.get('p', '1')
6 years ago
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)
_list = mw.M('tasks').where('', ()).field(
6 years ago
'id,name,type,status,addtime,start,end').limit(limit_str).order('id desc').select()
6 years ago
_ret = {}
_ret['data'] = _list
count = mw.M('tasks').where('', ()).count()
6 years ago
_page = {}
_page['count'] = count
_page['tojs'] = 'remind'
6 years ago
_page['p'] = p
6 years ago
3 years ago
# return data
_ret['count'] = count
_ret['page'] = mw.getPage(_page)
return mw.getJson(_ret)
6 years ago
6 years ago
def getExecLogApi(self):
6 years ago
file = os.getcwd() + "/tmp/panelExec.log"
v = mw.getLastLine(file, 100)
6 years ago
return v
6 years ago
def getTaskSpeedApi(self):
tempFile = mw.getRunDir() + '/tmp/panelExec.log'
freshFile = mw.getRunDir() + '/tmp/panelFresh'
6 years ago
find = mw.M('tasks').where('status=? OR status=?',
('-1', '0')).field('id,type,name,execstr').find()
6 years ago
if not len(find):
return mw.returnJson(False, '当前没有任务队列在执行-2!')
6 years ago
mw.triggerTask()
6 years ago
data = {}
data['name'] = find['name']
data['execstr'] = find['execstr']
6 years ago
if find['type'] == 'download':
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)
6 years ago
else:
data['msg'] = mw.getLastLine(tempFile, 10)
data['isDownload'] = False
6 years ago
data['task'] = mw.M('tasks').where("status!=?", ('1',)).field(
6 years ago
'id,status,name,type').order("id asc").select()
return mw.getJson(data)