fix: 解决某些情况下安装应用错误没有正确显示的问题 (#5295)

Refs https://github.com/1Panel-dev/1Panel/issues/5289
This commit is contained in:
zhengkunwang 2024-06-05 14:18:08 +08:00 committed by GitHub
parent 296ae1c2e1
commit 33c4b8bba9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 29 additions and 17 deletions

View File

@ -460,7 +460,7 @@ func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) (
go func() {
defer func() {
if err != nil {
appInstall.Status = constant.Error
appInstall.Status = constant.UpErr
appInstall.Message = err.Error()
_ = appInstallRepo.Save(context.Background(), appInstall)
}

View File

@ -153,7 +153,7 @@ func (a *AppInstallService) CheckExist(req request.AppInstalledInfo) (*response.
if reflect.DeepEqual(appInstall, model.AppInstall{}) {
return res, nil
}
if err = syncAppInstallStatus(&appInstall); err != nil {
if err = syncAppInstallStatus(&appInstall, false); err != nil {
return nil, err
}
@ -244,26 +244,26 @@ func (a *AppInstallService) Operate(req request.AppInstalledOperate) error {
if err != nil {
return handleErr(install, err, out)
}
return syncAppInstallStatus(&install)
return syncAppInstallStatus(&install, false)
case constant.Stop:
out, err := compose.Stop(dockerComposePath)
if err != nil {
return handleErr(install, err, out)
}
return syncAppInstallStatus(&install)
return syncAppInstallStatus(&install, false)
case constant.Restart:
out, err := compose.Restart(dockerComposePath)
if err != nil {
return handleErr(install, err, out)
}
return syncAppInstallStatus(&install)
return syncAppInstallStatus(&install, false)
case constant.Delete:
if err := deleteAppInstall(install, req.DeleteBackup, req.ForceDelete, req.DeleteDB); err != nil && !req.ForceDelete {
return err
}
return nil
case constant.Sync:
return syncAppInstallStatus(&install)
return syncAppInstallStatus(&install, true)
case constant.Upgrade:
upgradeReq := request.AppInstallUpgrade{
InstallID: install.ID,
@ -431,7 +431,7 @@ func (a *AppInstallService) SyncAll(systemInit bool) error {
continue
}
if !systemInit {
if err = syncAppInstallStatus(&i); err != nil {
if err = syncAppInstallStatus(&i, false); err != nil {
global.LOG.Errorf("sync install app[%s] error,mgs: %s", i.Name, err.Error())
}
}
@ -730,7 +730,7 @@ func (a *AppInstallService) GetParams(id uint) (*response.AppConfig, error) {
return &res, nil
}
func syncAppInstallStatus(appInstall *model.AppInstall) error {
func syncAppInstallStatus(appInstall *model.AppInstall, force bool) error {
if appInstall.Status == constant.Installing || appInstall.Status == constant.Rebuilding || appInstall.Status == constant.Upgrading {
return nil
}
@ -753,8 +753,7 @@ func syncAppInstallStatus(appInstall *model.AppInstall) error {
for _, con := range containers {
containersMap[con.Names[0]] = con
}
synAppInstall(containersMap, appInstall)
_ = appInstallRepo.Save(context.Background(), appInstall)
synAppInstall(containersMap, appInstall, force)
return nil
}

View File

@ -929,7 +929,7 @@ func upApp(appInstall *model.AppInstall, pullImages bool) {
return
}
if err := upProject(appInstall); err != nil {
appInstall.Status = constant.Error
appInstall.Status = constant.UpErr
} else {
appInstall.Status = constant.Running
}
@ -1137,18 +1137,22 @@ func handleErr(install model.AppInstall, err error, out string) error {
install.Message = out
reErr = errors.New(out)
}
install.Status = constant.Error
install.Status = constant.UpErr
_ = appInstallRepo.Save(context.Background(), &install)
return reErr
}
func doNotNeedSync(installed model.AppInstall) bool {
return installed.Status == constant.Installing || installed.Status == constant.Rebuilding || installed.Status == constant.Upgrading || installed.Status == constant.Syncing
return installed.Status == constant.Installing || installed.Status == constant.Rebuilding || installed.Status == constant.Upgrading ||
installed.Status == constant.Syncing
}
func synAppInstall(containers map[string]types.Container, appInstall *model.AppInstall) {
func synAppInstall(containers map[string]types.Container, appInstall *model.AppInstall, force bool) {
containerNames := strings.Split(appInstall.ContainerName, ",")
if len(containers) == 0 {
if appInstall.Status == constant.UpErr && !force {
return
}
appInstall.Status = constant.Error
appInstall.Message = buserr.WithName("ErrContainerNotFound", strings.Join(containerNames, ",")).Error()
_ = appInstallRepo.Save(context.Background(), appInstall)
@ -1182,6 +1186,12 @@ func synAppInstall(containers map[string]types.Container, appInstall *model.AppI
appInstall.Status = constant.Running
case pausedCount == total:
appInstall.Status = constant.Paused
case len(notFoundNames) == total:
if appInstall.Status == constant.UpErr && !force {
return
}
appInstall.Status = constant.Error
appInstall.Message = buserr.WithName("ErrContainerNotFound", strings.Join(notFoundNames, ",")).Error()
default:
var msg string
if exitedCount > 0 {
@ -1225,7 +1235,7 @@ func handleInstalled(appInstallList []model.AppInstall, updated bool, sync bool)
continue
}
if sync && !doNotNeedSync(installed) {
synAppInstall(containersMap, &installed)
synAppInstall(containersMap, &installed, false)
}
installDTO := response.AppInstallDTO{

View File

@ -948,7 +948,7 @@ func (w WebsiteService) PreInstallCheck(req request.WebsiteInstallCheckReq) ([]r
if len(checkIds) > 0 {
installList, _ := appInstallRepo.ListBy(commonRepo.WithIdsIn(checkIds))
for _, install := range installList {
if err = syncAppInstallStatus(&install); err != nil {
if err = syncAppInstallStatus(&install, false); err != nil {
return nil, err
}
res = append(res, response.WebsitePreInstallCheck{

View File

@ -13,7 +13,7 @@ const (
Syncing = "Syncing"
SyncSuccess = "SyncSuccess"
Paused = "Paused"
SyncErr = "SyncErr"
UpErr = "UpErr"
ContainerPrefix = "1Panel-"

View File

@ -263,6 +263,7 @@ const message = {
applying: 'Applying',
applyerror: 'Failure',
syncerr: 'Error',
uperr: 'Error',
},
units: {
second: 'Second',

View File

@ -261,6 +261,7 @@ const message = {
applying: '申請中',
applyerror: '失敗',
syncerr: '失敗',
uperr: '失败',
},
units: {
second: '秒',

View File

@ -261,6 +261,7 @@ const message = {
applying: '申请中',
applyerror: '失败',
syncerr: '失败',
uperr: '失败',
},
units: {
second: '秒',