1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 16:29:17 +08:00
1Panel/agent/init/hook/hook.go

92 lines
2.7 KiB
Go
Raw Normal View History

2024-07-23 14:48:37 +08:00
package hook
import (
"path"
"github.com/1Panel-dev/1Panel/agent/app/model"
"github.com/1Panel-dev/1Panel/agent/app/repo"
"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"
"github.com/1Panel-dev/1Panel/agent/utils/common"
2024-07-23 14:48:37 +08:00
)
func Init() {
initGlobalData()
handleCronjobStatus()
handleSnapStatus()
loadLocalDir()
}
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)
}
_ = service.NewISettingService().ReloadConn()
if global.IsMaster {
global.CoreDB = common.LoadDBConnByPath(path.Join(global.CONF.System.DbPath, "core.db"), "core")
}
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() {
_ = 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
}
func loadLocalDir() {
var vars string
if global.IsMaster {
var account model.BackupAccount
if err := global.CoreDB.Where("id = 1").First(&account).Error; err != nil {
global.LOG.Errorf("load local backup account info failed, err: %v", err)
return
}
vars = account.Vars
} else {
account, _, err := service.NewBackupClientWithID(1)
if err != nil {
global.LOG.Errorf("load local backup account info failed, err: %v", err)
return
}
vars = account.Vars
2024-07-23 14:48:37 +08:00
}
localDir, err := service.LoadLocalDirByStr(vars)
if err != nil {
global.LOG.Errorf("load local backup dir failed, err: %v", err)
return
2024-07-23 14:48:37 +08:00
}
global.CONF.System.Backup = localDir
2024-07-23 14:48:37 +08:00
}