fix: 修复计划任务编辑失败的问题

pull/205/head
ssongliu 2023-03-14 10:19:29 +08:00 committed by ssongliu
parent 4994cc39f1
commit daefd650a5
6 changed files with 50 additions and 17 deletions

View File

@ -222,6 +222,12 @@ func (u *BackupService) Update(req dto.BackupOperate) error {
if err := json.Unmarshal([]byte(req.Vars), &varMap); err != nil {
return err
}
oldVars := backup.Vars
oldDir, err := loadLocalDir()
if err != nil {
return err
}
upMap := make(map[string]interface{})
upMap["bucket"] = req.Bucket
upMap["credential"] = req.Credential
@ -235,9 +241,8 @@ func (u *BackupService) Update(req dto.BackupOperate) error {
if strings.HasSuffix(dirStr, "/") {
dirStr = dirStr[:strings.LastIndex(dirStr, "/")]
}
if err := updateBackupDir(dirStr); err != nil {
upMap["vars"] = backup.Vars
_ = backupRepo.Update(req.ID, upMap)
if err := updateBackupDir(dirStr, oldDir); err != nil {
_ = backupRepo.Update(req.ID, (map[string]interface{}{"vars": oldVars}))
return err
}
}
@ -309,8 +314,7 @@ func loadLocalDir() (string, error) {
return "", fmt.Errorf("error type dir: %T", varMap["dir"])
}
func updateBackupDir(dir string) error {
oldDir := global.CONF.System.Backup
func updateBackupDir(dir, oldDir string) error {
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
return err
@ -323,6 +327,5 @@ func updateBackupDir(dir string) error {
if err != nil {
return errors.New(string(stdout))
}
global.CONF.System.Backup = dir
return nil
}

View File

@ -178,24 +178,27 @@ func (u *CronjobService) Create(cronjobDto dto.CronjobCreate) error {
cronjob.Spec = loadSpec(cronjob)
global.LOG.Infof("create cronjob %s successful, spec: %s", cronjob.Name, cronjob.Spec)
if err := u.StartJob(&cronjob); err != nil {
entryID, err := u.StartJob(&cronjob)
if err != nil {
return err
}
cronjob.EntryID = uint64(entryID)
if err := cronjobRepo.Create(&cronjob); err != nil {
return err
}
return nil
}
func (u *CronjobService) StartJob(cronjob *model.Cronjob) error {
func (u *CronjobService) StartJob(cronjob *model.Cronjob) (int, error) {
if cronjob.EntryID != 0 {
global.Cron.Remove(cron.EntryID(cronjob.EntryID))
global.LOG.Infof("stop cronjob entryID: %d", cronjob.EntryID)
}
entryID, err := u.AddCronJob(cronjob)
if err != nil {
return err
return 0, err
}
_ = cronjobRepo.Update(cronjob.ID, map[string]interface{}{"entry_id": entryID})
return nil
global.LOG.Debug(global.Cron.Entries())
return entryID, nil
}
func (u *CronjobService) Delete(ids []uint) error {
@ -225,12 +228,15 @@ func (u *CronjobService) Update(id uint, req dto.CronjobUpdate) error {
return constant.ErrRecordNotFound
}
cronjob.EntryID = cronModel.EntryID
cronjob.Type = cronModel.Type
cronjob.Spec = loadSpec(cronjob)
if err := u.StartJob(&cronjob); err != nil {
newEntryID, err := u.StartJob(&cronjob)
if err != nil {
return err
}
upMap := make(map[string]interface{})
upMap["entry_id"] = newEntryID
upMap["name"] = req.Name
upMap["script"] = req.Script
upMap["spec_type"] = req.SpecType
@ -254,15 +260,20 @@ func (u *CronjobService) UpdateStatus(id uint, status string) error {
if cronjob.ID == 0 {
return errors.WithMessage(constant.ErrRecordNotFound, "record not found")
}
var (
entryID int
err error
)
if status == constant.StatusEnable {
if err := u.StartJob(&cronjob); err != nil {
entryID, err = u.StartJob(&cronjob)
if err != nil {
return err
}
} else {
global.Cron.Remove(cron.EntryID(cronjob.EntryID))
global.LOG.Infof("stop cronjob entryID: %d", cronjob.EntryID)
}
return cronjobRepo.Update(cronjob.ID, map[string]interface{}{"status": status})
return cronjobRepo.Update(cronjob.ID, map[string]interface{}{"status": status, "entry_id": entryID})
}
func (u *CronjobService) AddCronJob(cronjob *model.Cronjob) (int, error) {

View File

@ -4,6 +4,7 @@ import (
"time"
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/app/repo"
"github.com/1Panel-dev/1Panel/backend/app/service"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/cron/job"
@ -44,8 +45,12 @@ func Run() {
global.LOG.Errorf("start my cronjob failed, err: %v", err)
}
for _, cronjob := range cronJobs {
if err := service.ServiceGroupApp.StartJob(&cronjob); err != nil {
entryID, err := service.ServiceGroupApp.StartJob(&cronjob)
if err != nil {
global.LOG.Errorf("start %s job %s failed, err: %v", cronjob.Type, cronjob.Name, err)
}
if err := repo.NewICronjobRepo().Update(cronjob.ID, map[string]interface{}{"entry_id": entryID}); err != nil {
global.LOG.Errorf("update cronjob %s %s failed, err: %v", cronjob.Type, cronjob.Name, err)
}
}
}

View File

@ -35,6 +35,7 @@ export default {
saveAndEnable: 'Save and enable',
import: 'Import',
search: 'Search',
refresh: 'Refresh',
},
search: {
timeStart: 'Time start',

View File

@ -36,6 +36,7 @@ export default {
saveAndEnable: '',
import: '',
search: '',
refresh: '',
},
search: {
timeStart: '',

View File

@ -49,6 +49,10 @@
 {{ $t('cronjob.handle') }}
</el-tag>
<span class="buttons">
<el-button type="primary" @click="onRefresh" link>
{{ $t('commons.button.refresh') }}
</el-button>
<el-divider direction="vertical" />
<el-button type="primary" @click="onHandle(dialogData.rowData)" link>
{{ $t('commons.button.handle') }}
</el-button>
@ -450,6 +454,14 @@ const search = async () => {
loadRecord(currentRecord.value);
searchInfo.recordTotal = res.data.total;
};
const onRefresh = () => {
records.value = [];
searchInfo.page = 1;
searchInfo.pageSize = 8;
search();
};
const onDownload = async (record: any, backupID: number) => {
if (!record.file || record.file.indexOf('/') === -1) {
MsgError(i18n.global.t('cronjob.errPath', [record.file]));