From 123997fa13b03fa71a0cb008cb6c7ef09c5e96a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=98=AD?= <81747598+lan-yonghui@users.noreply.github.com> Date: Wed, 23 Oct 2024 11:46:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=9D=A2=E6=9D=BF=E9=87=8D=E5=90=AF?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E8=AE=A1=E5=88=92=E4=BB=BB=E5=8A=A1=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E6=97=B6=E8=BF=9B=E8=A1=8C=E7=9F=AD=E4=BF=A1=E5=91=8A?= =?UTF-8?q?=E8=AD=A6=20(#6817)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/init/hook/hook.go | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) 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 + } +}