2024-07-23 14:48:37 +08:00
|
|
|
package hook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
|
2025-01-21 18:32:14 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/dto"
|
2024-07-23 14:48:37 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/repo"
|
2024-08-13 15:33:34 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/service"
|
2024-07-23 14:48:37 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/global"
|
2024-11-28 18:42:40 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/common"
|
2025-01-21 18:32:14 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/xpack"
|
2024-07-23 14:48:37 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Init() {
|
2024-11-28 18:42:40 +08:00
|
|
|
initGlobalData()
|
|
|
|
handleCronjobStatus()
|
|
|
|
handleSnapStatus()
|
2025-01-27 19:06:26 +08:00
|
|
|
|
|
|
|
loadLocalDir()
|
2024-11-28 18:42:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func initGlobalData() {
|
2024-07-23 14:48:37 +08:00
|
|
|
settingRepo := repo.NewISettingRepo()
|
|
|
|
if _, err := settingRepo.Get(settingRepo.WithByKey("SystemStatus")); err != nil {
|
|
|
|
_ = settingRepo.Create("SystemStatus", "Free")
|
|
|
|
}
|
|
|
|
if err := settingRepo.Update("SystemStatus", "Free"); err != nil {
|
|
|
|
global.LOG.Fatalf("init service before start failed, err: %v", err)
|
|
|
|
}
|
2025-01-24 11:00:07 +08:00
|
|
|
global.CONF.Base.EncryptKey, _ = settingRepo.GetValueByKey("EncryptKey")
|
2024-12-26 14:13:41 +08:00
|
|
|
_ = service.NewISettingService().ReloadConn()
|
2024-08-14 10:15:55 +08:00
|
|
|
if global.IsMaster {
|
2025-01-24 11:00:07 +08:00
|
|
|
global.CoreDB = common.LoadDBConnByPath(path.Join(global.Dir.DbDir, "core.db"), "core")
|
2024-08-14 10:15:55 +08:00
|
|
|
}
|
2024-07-23 14:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleSnapStatus() {
|
|
|
|
msgFailed := "the task was interrupted due to the restart of the 1panel service"
|
|
|
|
_ = global.DB.Model(&model.Snapshot{}).Where("status = ?", "OnSaveData").
|
|
|
|
Updates(map[string]interface{}{"status": constant.StatusSuccess}).Error
|
|
|
|
|
|
|
|
_ = global.DB.Model(&model.Snapshot{}).Where("status = ?", constant.StatusWaiting).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"status": constant.StatusFailed,
|
|
|
|
"message": msgFailed,
|
|
|
|
}).Error
|
|
|
|
|
|
|
|
_ = global.DB.Model(&model.Snapshot{}).Where("recover_status = ?", constant.StatusWaiting).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"recover_status": constant.StatusFailed,
|
|
|
|
"recover_message": msgFailed,
|
|
|
|
}).Error
|
|
|
|
|
|
|
|
_ = global.DB.Model(&model.Snapshot{}).Where("rollback_status = ?", constant.StatusWaiting).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"rollback_status": constant.StatusFailed,
|
|
|
|
"rollback_message": msgFailed,
|
|
|
|
}).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleCronjobStatus() {
|
2025-01-21 18:32:14 +08:00
|
|
|
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)
|
|
|
|
}
|
2024-07-23 14:48:37 +08:00
|
|
|
}
|
|
|
|
|
2025-01-21 18:32:14 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2025-01-27 19:06:26 +08:00
|
|
|
|
|
|
|
func loadLocalDir() {
|
|
|
|
var account model.BackupAccount
|
|
|
|
if err := global.DB.Where("`type` = ?", constant.Local).First(&account).Error; err != nil {
|
|
|
|
global.LOG.Errorf("load local backup account info failed, err: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
global.Dir.LocalBackupDir = account.BackupPath
|
|
|
|
}
|