diff --git a/backend/init/hook/hook.go b/backend/init/hook/hook.go index 407de681e..85eca1014 100644 --- a/backend/init/hook/hook.go +++ b/backend/init/hook/hook.go @@ -7,6 +7,7 @@ import ( "path" "strings" + "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/app/repo" "github.com/1Panel-dev/1Panel/backend/constant" @@ -14,6 +15,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/common" "github.com/1Panel-dev/1Panel/backend/utils/encrypt" + "github.com/1Panel-dev/1Panel/backend/utils/xpack" ) func Init() { @@ -126,11 +128,24 @@ func handleSnapStatus() { } func handleCronjobStatus() { - _ = global.DB.Model(&model.JobRecords{}).Where("status = ?", constant.StatusWaiting). - Updates(map[string]interface{}{ - "status": constant.StatusFailed, - "message": "the task was interrupted due to the restart of the 1panel service", - }).Error + var jobRecords []model.JobRecords + _ = global.DB.Where("status = ?", constant.StatusWaiting).Find(&jobRecords).Error + for _, record := range jobRecords { + err := global.DB.Model(&model.JobRecords{}).Where("status = ?", constant.StatusWaiting). + Updates(map[string]interface{}{ + "status": constant.StatusFailed, + "message": "the task was interrupted due to the restart of the 1panel service", + }).Error + + if err != nil { + global.LOG.Errorf("Failed to update job ID: %v, Error:%v", record.ID, err) + continue + } + + var cronjob *model.Cronjob + _ = global.DB.Where("id = ?", record.CronjobID).First(&cronjob).Error + handleCronJobAlert(cronjob) + } } func loadLocalDir() { @@ -210,3 +225,17 @@ func initDir() { } } } + +func handleCronJobAlert(cronjob *model.Cronjob) { + pushAlert := dto.PushAlert{ + TaskName: cronjob.Name, + AlertType: cronjob.Type, + EntryID: cronjob.ID, + Param: cronjob.Type, + } + err := xpack.PushAlert(pushAlert) + if err != nil { + global.LOG.Errorf("cronjob alert push failed, err: %v", err) + return + } +}