fix: 调整计划任务输出,删除时移除任务输出文件

pull/77/head
ssongliu 2022-12-14 19:34:42 +08:00 committed by ssongliu
parent 275bbc17cc
commit 5945098844
8 changed files with 81 additions and 55 deletions

View File

@ -262,7 +262,7 @@ func (u *CronjobService) AddCronJob(cronjob *model.Cronjob) (int, error) {
}
func mkdirAndWriteFile(cronjob *model.Cronjob, startTime time.Time, msg []byte) (string, error) {
dir := fmt.Sprintf("%s/%s/%s-%v", constant.TaskDir, cronjob.Type, cronjob.Name, cronjob.ID)
dir := fmt.Sprintf("%s/%s/%s", constant.TaskDir, cronjob.Type, cronjob.Name)
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
return "", err
@ -289,6 +289,8 @@ func loadSpec(cronjob model.Cronjob) string {
return fmt.Sprintf("%v %v * * %v", cronjob.Minute, cronjob.Hour, cronjob.Week)
case "perNDay":
return fmt.Sprintf("%v %v */%v * *", cronjob.Minute, cronjob.Hour, cronjob.Day)
case "perDay":
return fmt.Sprintf("%v %v * * *", cronjob.Minute, cronjob.Hour)
case "perNHour":
return fmt.Sprintf("%v */%v * * *", cronjob.Minute, cronjob.Hour)
case "perHour":

View File

@ -32,6 +32,7 @@ func (u *CronjobService) HandleJob(cronjob *model.Cronjob) {
if errExec != nil {
err = errors.New(string(stdout))
}
message = stdout
case "website":
record.File, err = u.HandleBackup(cronjob, record.StartTime)
case "database":
@ -61,9 +62,11 @@ func (u *CronjobService) HandleJob(cronjob *model.Cronjob) {
cronjobRepo.EndRecords(record, constant.StatusFailed, err.Error(), string(message))
return
}
record.Records, err = mkdirAndWriteFile(cronjob, record.StartTime, message)
if err != nil {
global.LOG.Errorf("save file %s failed, err: %v", record.Records, err)
if len(message) != 0 {
record.Records, err = mkdirAndWriteFile(cronjob, record.StartTime, message)
if err != nil {
global.LOG.Errorf("save file %s failed, err: %v", record.Records, err)
}
}
cronjobRepo.EndRecords(record, constant.StatusSuccess, "", record.Records)
}
@ -143,8 +146,11 @@ func (u *CronjobService) HandleDelete(id uint) error {
global.Cron.Remove(cron.EntryID(cronjob.EntryID))
_ = cronjobRepo.DeleteRecord(cronjobRepo.WithByJobID(int(id)))
if err := os.RemoveAll(fmt.Sprintf("%s/%s-%v", constant.TaskDir, commonDir, cronjob.ID)); err != nil {
global.LOG.Errorf("rm file %s/%s-%v failed, err: %v", constant.TaskDir, commonDir, cronjob.ID, err)
dir := fmt.Sprintf("%s/%s/%s", constant.TaskDir, cronjob.Type, cronjob.Name)
if _, err := os.Stat(dir); err == nil {
if err := os.RemoveAll(dir); err != nil {
global.LOG.Errorf("rm file %s/%s failed, err: %v", constant.TaskDir, commonDir, err)
}
}
return nil
}

View File

@ -34,6 +34,6 @@ export const download = (params: Cronjob.Download) => {
return http.download<BlobPart>(`cronjobs/download`, params, { responseType: 'blob' });
};
export const handleOnce = (params: number) => {
return http.post(`cronjobs/handle/${params}`);
export const handleOnce = (id: number) => {
return http.post(`cronjobs/handle`, { id: id });
};

View File

@ -143,6 +143,8 @@ export default {
removing: 'removing',
paused: 'paused',
exited: 'exited',
enabled: 'Enabled',
disabled: 'Disabled',
},
},
menu: {
@ -469,6 +471,9 @@ export default {
},
cronjob: {
cronTask: 'Task',
changeStatus: 'Change status',
disableMsg: 'The cronjob cannot continue to run after it is stopped. Do you want to stop it?',
enableMsg: 'The cronjob has been stopped. Enable now?',
taskType: 'Task type',
shell: 'shell',
website: 'website',
@ -495,6 +500,7 @@ export default {
perWeek: 'Per week',
perHour: 'Per hour',
perNDay: 'Every N days',
perDay: 'Every days',
perNHour: 'Every N hours',
perNMinute: 'Every N minutes',
per: 'Every ',

View File

@ -146,6 +146,8 @@ export default {
paused: '',
exited: '',
installing: '',
enabled: '',
disabled: '',
},
},
menu: {
@ -481,6 +483,9 @@ export default {
},
cronjob: {
cronTask: '',
changeStatus: '',
disableMsg: '',
enableMsg: '',
taskType: '',
shell: 'Shell ',
website: '',
@ -507,6 +512,7 @@ export default {
perWeek: '',
perHour: '',
perNDay: ' N ',
perDay: '',
perNHour: ' N ',
perNMinute: ' N ',
per: '',

View File

@ -19,15 +19,18 @@
<el-table-column :label="$t('cronjob.taskName')" prop="name" />
<el-table-column :label="$t('commons.table.status')" prop="status">
<template #default="{ row }">
<el-switch
@change="onChangeStatus(row)"
:before-change="beforeChangeStatus"
v-model="row.status"
inline-prompt
size="default"
active-value="Enable"
inactive-value="Disable"
/>
<el-button
v-if="row.status === 'Enable'"
@click="onChangeStatus(row.id, 'disable')"
link
type="success"
icon="VideoPlay"
>
{{ $t('commons.status.enabled') }}
</el-button>
<el-button v-else link type="danger" @click="onChangeStatus(row.id, 'enable')" icon="VideoPause">
{{ $t('commons.status.disabled') }}
</el-button>
</template>
</el-table-column>
<el-table-column :label="$t('cronjob.cronSpec')">
@ -90,10 +93,9 @@ import { loadBackupName } from '@/views/setting/helper';
import i18n from '@/lang';
import { Cronjob } from '@/api/interface/cronjob';
import { useDeleteData } from '@/hooks/use-delete-data';
import { ElMessage } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
const selects = ref<any>([]);
const switchState = ref<boolean>(false);
const data = ref();
const paginationConfig = reactive({
@ -164,17 +166,19 @@ const onBatchDelete = async (row: Cronjob.CronjobInfo | null) => {
await useDeleteData(deleteCronjob, { ids: ids }, 'commons.msg.delete');
search();
};
const beforeChangeStatus = () => {
switchState.value = true;
return switchState.value;
};
const onChangeStatus = async (row: Cronjob.CronjobInfo) => {
if (switchState.value) {
await updateStatus({ id: row.id, status: row.status });
const onChangeStatus = async (id: number, status: string) => {
ElMessageBox.confirm(i18n.global.t('cronjob.' + status + 'Msg'), i18n.global.t('cronjob.changeStatus'), {
confirmButtonText: i18n.global.t('commons.button.confirm'),
cancelButtonText: i18n.global.t('commons.button.cancel'),
}).then(async () => {
let itemStatus = status === 'enable' ? 'Enable' : 'Disable';
await updateStatus({ id: id, status: itemStatus });
ElMessage.success(i18n.global.t('commons.msg.operationSuccess'));
search();
}
});
};
const onHandle = async (row: Cronjob.CronjobInfo) => {
await handleOnce(row.id);
ElMessage.success(i18n.global.t('commons.msg.operationSuccess'));

View File

@ -226,6 +226,16 @@ const varifySpec = (rule: any, value: any, callback: any) => {
callback(new Error(i18n.global.t('cronjob.cronSpecRule')));
}
break;
case 'perDay':
if (
!(
Number.isInteger(dialogData.value.rowData!.hour) &&
Number.isInteger(dialogData.value.rowData!.minute)
)
) {
callback(new Error(i18n.global.t('cronjob.cronSpecRule')));
}
break;
case 'perNHour':
if (
!(
@ -250,6 +260,7 @@ const specOptions = [
{ label: i18n.global.t('cronjob.perMonth'), value: 'perMonth' },
{ label: i18n.global.t('cronjob.perWeek'), value: 'perWeek' },
{ label: i18n.global.t('cronjob.perNDay'), value: 'perNDay' },
{ label: i18n.global.t('cronjob.perDay'), value: 'perDay' },
{ label: i18n.global.t('cronjob.perNHour'), value: 'perNHour' },
{ label: i18n.global.t('cronjob.perHour'), value: 'perHour' },
{ label: i18n.global.t('cronjob.perNMinute'), value: 'perNMinute' },
@ -341,7 +352,7 @@ function changeName(isChangeType: boolean, type: string) {
}
}
}
dialogData.value.rowData!.name = `${type}-test`;
dialogData.value.rowData!.name = `${type}_test`;
}
const onSubmit = async (formEl: FormInstance | undefined) => {

View File

@ -191,35 +191,26 @@
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-row v-if="currentRecord?.records">
<el-col :span="24">
<el-form-item :label="$t('commons.table.records')">
<span style="color: red" v-if="currentRecord?.status! === 'Failed'">
{{ currentRecord?.message }}
</span>
<div v-else>
<el-popover
placement="right"
:width="600"
trigger="click"
style="white-space: pre-wrap"
>
<div style="margin-left: 20px; max-height: 400px; overflow: auto">
<span style="white-space: pre-wrap">
{{ currentRecordDetail }}
</span>
</div>
<template #reference>
<el-button
type="primary"
link
@click="loadRecord(currentRecord?.records!)"
>
{{ $t('commons.button.expand') }}
</el-button>
</template>
</el-popover>
</div>
<el-popover
placement="right"
:width="600"
trigger="click"
style="white-space: pre-wrap"
>
<div style="margin-left: 20px; max-height: 400px; overflow: auto">
<span style="white-space: pre-wrap">
{{ currentRecordDetail }}
</span>
</div>
<template #reference>
<el-button type="primary" link @click="loadRecord(currentRecord?.records!)">
{{ $t('commons.button.expand') }}
</el-button>
</template>
</el-popover>
</el-form-item>
</el-col>
</el-row>