为定时任务增加随机延迟执行功能

This commit is contained in:
x1ao4 2025-05-22 00:56:03 +08:00
parent 18f344e19c
commit 0b19935e06
4 changed files with 115 additions and 5 deletions

View File

@ -25,6 +25,8 @@ import base64
import sys
import os
import re
import random
import time
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.insert(0, parent_dir)
@ -683,6 +685,18 @@ def add_task():
# 定时任务执行的函数
def run_python(args):
logging.info(f">>> 定时运行任务")
# 检查是否需要随机延迟执行
if delay := config_data.get("crontab_delay"):
try:
delay_seconds = int(delay)
if delay_seconds > 0:
# 在0到设定值之间随机选择一个延迟时间
random_delay = random.randint(0, delay_seconds)
logging.info(f">>> 随机延迟执行 {random_delay}")
time.sleep(random_delay)
except (ValueError, TypeError):
logging.warning(f">>> 延迟执行设置无效: {delay}")
os.system(f"{PYTHON_PATH} {args}")
@ -708,6 +722,9 @@ def reload_tasks():
logging.info(">>> 重载调度器")
logging.info(f"调度状态: {scheduler_state_map[scheduler.state]}")
logging.info(f"定时规则: {crontab}")
# 记录延迟执行设置
if delay := config_data.get("crontab_delay"):
logging.info(f"延迟执行: 0-{delay}")
logging.info(f"现有任务: {scheduler.get_jobs()}")
return True
else:
@ -740,6 +757,10 @@ def init():
# 默认定时规则
if not config_data.get("crontab"):
config_data["crontab"] = "0 8,18,20 * * *"
# 默认延迟执行设置
if "crontab_delay" not in config_data:
config_data["crontab_delay"] = 0
# 初始化插件配置
_, plugins_config_default, task_plugins_config_default = Config.load_plugins()

View File

@ -3724,3 +3724,43 @@ input::-moz-list-button {
background-color: #f7f7fa; /* 表头悬停背景色 */
cursor: pointer;
}
/* 移除number类型输入框的上下箭头 */
input.no-spinner::-webkit-outer-spin-button,
input.no-spinner::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
/* Firefox也需要特别处理 */
input.no-spinner {
-moz-appearance: textfield;
}
/* 秒字框正方形样式 */
.square-append {
width: 32px;
height: 32px;
display: flex;
align-items: center;
justify-content: center;
padding: 0 !important;
}
/* 确保Crontab和延迟执行框在移动端也保持一行 */
@media (max-width: 767.98px) {
.row.mb-2 .col-sm-6 {
flex: 0 0 50%;
max-width: 50%;
}
.row.mb-2 .col-sm-6.pr-1 {
padding-right: 4px !important;
padding-left: 15px !important;
}
.row.mb-2 .col-sm-6.pl-1 {
padding-left: 4px !important;
padding-right: 15px !important;
}
}

View File

@ -273,11 +273,26 @@
</span>
</div>
</div>
<div class="input-group mb-2">
<div class="input-group-prepend">
<span class="input-group-text">Crontab</span>
<div class="row mb-2">
<div class="col-sm-6 pr-1">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">Crontab</span>
</div>
<input type="text" v-model="formData.crontab" class="form-control" placeholder="必填">
</div>
</div>
<div class="col-sm-6 pl-1">
<div class="input-group" title="添加随机延迟时间定时任务将在0到设定秒数之间随机延迟执行可设范围03600秒0表示不延迟">
<div class="input-group-prepend">
<span class="input-group-text">延迟执行</span>
</div>
<input type="text" v-model="formData.crontab_delay" class="form-control no-spinner" placeholder="0-3600" @input="validateNumberInput($event, 'crontab_delay', 3600)">
<div class="input-group-append">
<span class="input-group-text square-append"></span>
</div>
</div>
</div>
<input type="text" v-model="formData.crontab" class="form-control" placeholder="必填">
</div>
<div class="row title" title="通知推送支持多个渠道查阅Wiki了解详情">
@ -1438,6 +1453,7 @@
return task;
});
// 获取所有任务父目录
config_data.tasklist.forEach(item => {
parentDir = this.getParentDirectory(item.savepath)
@ -3072,6 +3088,40 @@
}
});
},
validateNumberInput(event, field, max) {
// 获取当前输入值
let value = event.target.value;
// 获取输入框的当前光标位置
const cursorPosition = event.target.selectionStart;
// 记录原始长度
const originalLength = value.length;
// 移除非数字字符
const cleanValue = value.replace(/[^\d]/g, '');
// 如果有非数字字符被移除
if (cleanValue !== value) {
// 计算移除了多少个字符
const diff = originalLength - cleanValue.length;
// 更新输入框的值
event.target.value = cleanValue;
// 调整光标位置(考虑到字符被移除)
setTimeout(() => {
event.target.setSelectionRange(Math.max(0, cursorPosition - diff), Math.max(0, cursorPosition - diff));
}, 0);
}
// 确保不超过最大值
if (cleanValue !== '' && parseInt(cleanValue) > max) {
event.target.value = max.toString();
}
// 更新数据模型如果为空则默认为0
this.formData[field] = event.target.value === '' ? 0 : parseInt(event.target.value);
},
}
});
</script>

View File

@ -4283,7 +4283,6 @@ def do_save(account, tasklist=[]):
)
elif is_new_tree is False: # 明确没有新文件
print(f"任务完成: 没有新的文件需要转存")
print()
print()