mirror of
https://github.com/Cp0204/quark-auto-save.git
synced 2026-01-16 17:30:43 +08:00
WebUI 支持手动运行
This commit is contained in:
parent
292ab70b9d
commit
16ebe5c9b8
30
app/run.py
30
app/run.py
@ -1,3 +1,5 @@
|
|||||||
|
# !/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
from flask import (
|
from flask import (
|
||||||
Flask,
|
Flask,
|
||||||
render_template,
|
render_template,
|
||||||
@ -7,15 +9,19 @@ from flask import (
|
|||||||
session,
|
session,
|
||||||
jsonify,
|
jsonify,
|
||||||
send_from_directory,
|
send_from_directory,
|
||||||
|
Response,
|
||||||
)
|
)
|
||||||
from apscheduler.schedulers.background import BackgroundScheduler
|
from apscheduler.schedulers.background import BackgroundScheduler
|
||||||
from apscheduler.triggers.cron import CronTrigger
|
from apscheduler.triggers.cron import CronTrigger
|
||||||
|
import subprocess
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import os
|
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")
|
config_path = os.environ.get("CONFIG_PATH", "./config/quark_config.json")
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@ -122,6 +128,26 @@ def update():
|
|||||||
return "配置更新成功"
|
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):
|
def run_script(script_path):
|
||||||
os.system(f"python {script_path}")
|
os.system(f"python {script_path}")
|
||||||
@ -142,7 +168,7 @@ def reload_tasks():
|
|||||||
trigger = CronTrigger.from_crontab(crontab)
|
trigger = CronTrigger.from_crontab(crontab)
|
||||||
scheduler.remove_all_jobs()
|
scheduler.remove_all_jobs()
|
||||||
scheduler.add_job(
|
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:
|
if scheduler.state == 2:
|
||||||
scheduler.resume() # 恢复调度器
|
scheduler.resume() # 恢复调度器
|
||||||
|
|||||||
@ -142,6 +142,7 @@
|
|||||||
<button type="button" class="btn btn-primary" @click="addTask()">增加任务</button>
|
<button type="button" class="btn btn-primary" @click="addTask()">增加任务</button>
|
||||||
<div class="bottom-buttons">
|
<div class="bottom-buttons">
|
||||||
<input type="submit" class="btn btn-success" value="保存">
|
<input type="submit" class="btn btn-success" value="保存">
|
||||||
|
<a class="btn btn-primary" @click="runScriptNow">运行</a>
|
||||||
<a class="btn btn-danger" href="/logout">退出</a>
|
<a class="btn btn-danger" href="/logout">退出</a>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
@ -150,6 +151,24 @@
|
|||||||
<a target="_blank" href="https://github.com/Cp0204/quark-auto-save">@Cp0204/quark_auto_save</a> v{{ version }}
|
<a target="_blank" href="https://github.com/Cp0204/quark-auto-save">@Cp0204/quark_auto_save</a> v{{ version }}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</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">×</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body">
|
||||||
|
<pre v-html="run_log"></pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 引入 Bootstrap JS -->
|
<!-- 引入 Bootstrap JS -->
|
||||||
@ -173,7 +192,8 @@
|
|||||||
apikey: ""
|
apikey: ""
|
||||||
},
|
},
|
||||||
tasklist: []
|
tasklist: []
|
||||||
}
|
},
|
||||||
|
run_log: ""
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
'task.shareurl': function (newVal, oldVal) {
|
'task.shareurl': function (newVal, oldVal) {
|
||||||
@ -238,6 +258,18 @@
|
|||||||
},
|
},
|
||||||
clearShareurlBan(task) {
|
clearShareurlBan(task) {
|
||||||
delete task.shareurl_ban;
|
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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user