1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-13 17:24:44 +08:00

fix: 修改计划任务执行周期校验 (#3821)

This commit is contained in:
ssongliu 2024-02-04 22:50:38 +08:00 committed by GitHub
parent 1169648162
commit 9b8b9f505b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 103 additions and 47 deletions

View File

@ -827,7 +827,7 @@ const message = {
retainCopiesHelper: 'Number of copies to retain for execution records and logs',
retainCopiesHelper1: 'Number of copies to retain for backup files',
retainCopiesUnit: ' copies (View)',
cronSpecRule: 'Please enter a correct lifecycle',
cronSpecRule: 'The execution period format in line {0} is incorrect. Please check and try again!',
perMonth: 'Every monthly',
perWeek: 'Every week',
perHour: 'Every hour',

View File

@ -787,7 +787,7 @@ const message = {
retainCopiesHelper: '執行記錄及日誌保留份数',
retainCopiesHelper1: '備份文件保留份数',
retainCopiesUnit: ' (查看)',
cronSpecRule: '請輸入正確的執行周期',
cronSpecRule: '{0} 行中執行週期格式錯誤請檢查後重試',
perMonth: '每月',
perWeek: '每周',
perHour: '每小時',

View File

@ -788,7 +788,7 @@ const message = {
retainCopiesHelper: '执行记录及日志保留份数',
retainCopiesHelper1: '备份文件保留份数',
retainCopiesUnit: ' (查看)',
cronSpecRule: '请输入正确的执行周期',
cronSpecRule: '{0} 行中执行周期格式错误请检查后重试',
perMonth: '每月',
perWeek: '每周',
perHour: '每小时',

View File

@ -78,6 +78,11 @@ function loadWeek(i: number) {
export function loadDefaultSpec(type: string) {
let item = {} as Cronjob.SpecObj;
item.week = 0;
item.day = 0;
item.hour = 0;
item.minute = 0;
item.second = 0;
switch (type) {
case 'shell':
item.specType = 'perWeek';
@ -124,27 +129,6 @@ export function loadDefaultSpec(type: string) {
return item;
}
export function checkScript(specType: string, week, day, hour, minute, second) {
switch (specType) {
case 'perMonth':
return day > 0 && day < 32 && hour >= 0 && hour < 24 && minute >= 0 && minute < 60;
case 'perWeek':
return week >= 0 && week < 7 && hour >= 0 && hour < 24 && minute >= 0 && minute < 60;
case 'perDay':
return hour >= 0 && hour < 24 && minute >= 0 && minute < 60;
case 'perHour':
return minute >= 0 && minute < 60;
case 'perNDay':
return day > 0 && day < 366 && hour >= 0 && hour < 24 && minute >= 0 && minute < 60;
case 'perNHour':
return hour > 0 && hour < 8784 && minute >= 0 && minute < 60;
case 'perNMinute':
return minute > 0 && minute < 527040;
case 'perNSecond':
return second > 0 && second < 31622400;
}
}
export function transObjToSpec(specType: string, week, day, hour, minute, second): string {
switch (specType) {
case 'perMonth':

View File

@ -78,7 +78,7 @@
<el-form-item :label="$t('cronjob.cronSpec')" prop="spec">
<div v-for="(specObj, index) of dialogData.rowData.specObjs" :key="index" style="width: 100%">
<el-select class="specTypeClass" v-model="specObj.specType">
<el-select class="specTypeClass" v-model="specObj.specType" @change="changeSpecType(index)">
<el-option
v-for="item in specOptions"
:key="item.label"
@ -133,10 +133,10 @@
</el-button>
<el-divider v-if="dialogData.rowData.specObjs.length > 1" class="divider" />
</div>
<el-button class="mt-3" @click="handleSpecAdd()">
{{ $t('commons.button.add') }}
</el-button>
</el-form-item>
<el-button class="mb-3" @click="handleSpecAdd()">
{{ $t('commons.button.add') }}
</el-button>
<el-form-item v-if="hasScript()">
<el-checkbox v-model="dialogData.rowData!.inContainer">
@ -340,7 +340,7 @@ import { useRouter } from 'vue-router';
import { listContainer } from '@/api/modules/container';
import { Database } from '@/api/interface/database';
import { ListAppInstalled } from '@/api/modules/app';
import { checkScript, loadDefaultSpec, specOptions, transObjToSpec, transSpecToObj, weekOptions } from './../helper';
import { loadDefaultSpec, specOptions, transObjToSpec, transSpecToObj, weekOptions } from './../helper';
const router = useRouter();
interface DialogProps {
@ -413,40 +413,87 @@ const dbInfo = reactive({
const verifySpec = (rule: any, value: any, callback: any) => {
if (dialogData.value.rowData!.specObjs.length === 0) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule')));
callback(new Error(i18n.global.t('commons.rule.requiredInput')));
}
for (const item of dialogData.value.rowData!.specObjs) {
for (let i = 0; i < dialogData.value.rowData!.specObjs.length; i++) {
let item = dialogData.value.rowData!.specObjs[i];
if (
!Number.isInteger(item.day) ||
!Number.isInteger(item.hour) ||
!Number.isInteger(item.minute) ||
!Number.isInteger(item.second) ||
!Number.isInteger(item.week)
) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule', [i + 1])));
return;
}
switch (item.specType) {
case 'perMonth':
if (
item.day < 0 ||
item.day > 31 ||
item.hour < 0 ||
item.hour > 23 ||
item.minute < 0 ||
item.minute > 59
) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule', [i + 1])));
return;
}
break;
case 'perNDay':
if (!(Number.isInteger(item.day) && Number.isInteger(item.hour) && Number.isInteger(item.minute))) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule')));
if (
item.day < 0 ||
item.day > 366 ||
item.hour < 0 ||
item.hour > 23 ||
item.minute < 0 ||
item.minute > 59
) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule', [i + 1])));
return;
}
break;
case 'perWeek':
if (!(Number.isInteger(item.week) && Number.isInteger(item.hour) && Number.isInteger(item.minute))) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule')));
if (
item.week < 0 ||
item.week > 6 ||
item.hour < 0 ||
item.hour > 23 ||
item.minute < 0 ||
item.minute > 59
) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule', [i + 1])));
return;
}
break;
case 'perDay':
if (!(Number.isInteger(item.hour) && Number.isInteger(item.minute))) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule')));
if (item.hour < 0 || item.hour > 23 || item.minute < 0 || item.minute > 59) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule', [i + 1])));
return;
}
break;
case 'perNHour':
if (!(Number.isInteger(item.hour) && Number.isInteger(item.minute))) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule')));
if (item.hour < 0 || item.hour > 8784 || item.minute < 0 || item.minute > 59) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule', [i + 1])));
return;
}
break;
case 'perHour':
if (item.minute < 0 || item.minute > 59) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule', [i + 1])));
return;
}
case 'perNMinute':
if (!Number.isInteger(item.minute)) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule')));
if (item.minute < 0 || item.minute > 527040) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule', [i + 1])));
return;
}
break;
case 'perNSecond':
if (!Number.isInteger(item.second)) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule')));
if (item.second < 0 || item.second > 31622400) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule', [i + 1])));
return;
}
break;
}
@ -495,6 +542,35 @@ const changeType = () => {
dialogData.value.rowData!.specObjs = [loadDefaultSpec(dialogData.value.rowData.type)];
};
const changeSpecType = (index: number) => {
let item = dialogData.value.rowData!.specObjs[index];
switch (item.specType) {
case 'perMonth':
case 'perNDay':
item.day = 3;
item.hour = 1;
item.minute = 30;
break;
case 'perWeek':
item.week = 1;
item.hour = 1;
item.minute = 30;
break;
case 'perDay':
case 'perNHour':
item.hour = 2;
item.minute = 30;
break;
case 'perHour':
case 'perNMinute':
item.minute = 30;
break;
case 'perNSecond':
item.second = 30;
break;
}
};
const handleSpecAdd = () => {
let item = {
specType: 'perWeek',
@ -582,10 +658,6 @@ function hasScript() {
const onSubmit = async (formEl: FormInstance | undefined) => {
const specs = [];
for (const item of dialogData.value.rowData.specObjs) {
if (!checkScript(item.specType, item.week, item.day, item.hour, item.minute, item.second)) {
MsgError(i18n.global.t('cronjob.cronSpecHelper'));
return;
}
const itemSpec = transObjToSpec(item.specType, item.week, item.day, item.hour, item.minute, item.second);
if (itemSpec === '') {
MsgError(i18n.global.t('cronjob.cronSpecHelper'));