Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/lang/modules/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lang/modules/tw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ const message = {
retainCopiesHelper: '執行記錄及日誌保留份数',
retainCopiesHelper1: '備份文件保留份数',
retainCopiesUnit: ' 份 (查看)',
cronSpecRule: '請輸入正確的執行周期',
cronSpecRule: '第 {0} 行中執行週期格式錯誤,請檢查後重試!',
perMonth: '每月',
perWeek: '每周',
perHour: '每小時',
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lang/modules/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ const message = {
retainCopiesHelper: '执行记录及日志保留份数',
retainCopiesHelper1: '备份文件保留份数',
retainCopiesUnit: ' 份 (查看)',
cronSpecRule: '请输入正确的执行周期',
cronSpecRule: '第 {0} 行中执行周期格式错误,请检查后重试!',
perMonth: '每月',
perWeek: '每周',
perHour: '每小时',
Expand Down
26 changes: 5 additions & 21 deletions frontend/src/views/cronjob/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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':
Expand Down
118 changes: 95 additions & 23 deletions frontend/src/views/cronjob/operate/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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">
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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'));
Expand Down
Loading