diff --git a/backend/app/service/backup.go b/backend/app/service/backup.go index 2127c2d00..06062a623 100644 --- a/backend/app/service/backup.go +++ b/backend/app/service/backup.go @@ -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 } diff --git a/backend/app/service/cornjob.go b/backend/app/service/cornjob.go index 63156a600..cc012b2ed 100644 --- a/backend/app/service/cornjob.go +++ b/backend/app/service/cornjob.go @@ -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 { - global.Cron.Remove(cron.EntryID(cronjob.EntryID)) - global.LOG.Infof("stop cronjob entryID: %d", cronjob.EntryID) +func (u *CronjobService) StartJob(cronjob *model.Cronjob) (int, error) { + if cronjob.EntryID != 0 { + global.Cron.Remove(cron.EntryID(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) { diff --git a/backend/cron/cron.go b/backend/cron/cron.go index 048355e2a..4369aadf0 100644 --- a/backend/cron/cron.go +++ b/backend/cron/cron.go @@ -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) + } } } diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts index 0f46dab64..e76f939a7 100644 --- a/frontend/src/lang/modules/en.ts +++ b/frontend/src/lang/modules/en.ts @@ -35,6 +35,7 @@ export default { saveAndEnable: 'Save and enable', import: 'Import', search: 'Search', + refresh: 'Refresh', }, search: { timeStart: 'Time start', diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index c0bc432c9..03b9a491b 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -36,6 +36,7 @@ export default { saveAndEnable: '保存并启用', import: '导入', search: '搜索', + refresh: '刷新', }, search: { timeStart: '开始时间', diff --git a/frontend/src/views/cronjob/record/index.vue b/frontend/src/views/cronjob/record/index.vue index 74dac596d..ecf0ad167 100644 --- a/frontend/src/views/cronjob/record/index.vue +++ b/frontend/src/views/cronjob/record/index.vue @@ -49,6 +49,10 @@  {{ $t('cronjob.handle') }} + + {{ $t('commons.button.refresh') }} + + {{ $t('commons.button.handle') }} @@ -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]));