WebUI 支持手动运行

This commit is contained in:
Cp0204 2024-03-23 01:03:32 +08:00
parent 292ab70b9d
commit 16ebe5c9b8
2 changed files with 61 additions and 3 deletions

View File

@ -1,3 +1,5 @@
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
from flask import (
Flask,
render_template,
@ -7,15 +9,19 @@ from flask import (
session,
jsonify,
send_from_directory,
Response,
)
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
import subprocess
import hashlib
import json
import os
# 定义 JSON 文件路径
# 文件路径
python_path = "python3" if os.path.exists("/usr/bin/python3") else "python"
script_path = os.environ.get("SCRIPT_PATH", "./quark_auto_save.py")
config_path = os.environ.get("CONFIG_PATH", "./config/quark_config.json")
app = Flask(__name__)
@ -122,6 +128,26 @@ def update():
return "配置更新成功"
# 处理运行脚本请求
@app.route("/run_script_now", methods=["POST"])
def run_script_now():
if not is_login():
return "未登录"
command = [python_path, script_path, config_path]
def generate_output():
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
)
for line in process.stdout:
yield line
return Response(generate_output(), mimetype="text/plain")
# 定时任务执行的函数
def run_script(script_path):
os.system(f"python {script_path}")
@ -142,7 +168,7 @@ def reload_tasks():
trigger = CronTrigger.from_crontab(crontab)
scheduler.remove_all_jobs()
scheduler.add_job(
run_script, trigger=trigger, args=[f"quark_auto_save.py {config_path}"]
run_script, trigger=trigger, args=[f"{python_path} {script_path} {config_path}"]
)
if scheduler.state == 2:
scheduler.resume() # 恢复调度器

View File

@ -142,6 +142,7 @@
<button type="button" class="btn btn-primary" @click="addTask()">增加任务</button>
<div class="bottom-buttons">
<input type="submit" class="btn btn-success" value="保存">
<a class="btn btn-primary" @click="runScriptNow">运行</a>
<a class="btn btn-danger" href="/logout">退出</a>
</div>
</form>
@ -150,6 +151,24 @@
<a target="_blank" href="https://github.com/Cp0204/quark-auto-save">@Cp0204/quark_auto_save</a> v{{ version }}
</p>
</div>
<!-- 模态框 -->
<div class="modal" tabindex="-1" id="logModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">执行日志</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<pre v-html="run_log"></pre>
</div>
</div>
</div>
</div>
</div>
<!-- 引入 Bootstrap JS -->
@ -173,7 +192,8 @@
apikey: ""
},
tasklist: []
}
},
run_log: ""
},
watch: {
'task.shareurl': function (newVal, oldVal) {
@ -238,6 +258,18 @@
},
clearShareurlBan(task) {
delete task.shareurl_ban;
},
runScriptNow() {
$('#logModal').modal('toggle')
this.run_log = "请耐心等待脚本全部执行完毕..."
axios.post('/run_script_now')
.then(response => {
this.run_log = response.data;
})
.catch(error => {
this.run_log = "错误:\n" + error
console.error('Error:', error);
});
}
}
});