From 9af00cc80c14b3243ca0e56404de5a54e38aa120 Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Fri, 31 May 2024 17:57:20 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E6=9F=A5=E8=AF=A2=E6=8E=A5=E5=8F=A3=20(#5241?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/app_utils.go | 39 +++++++++++++++++-- backend/utils/docker/docker.go | 13 ++++++- .../src/views/app-store/installed/index.vue | 2 +- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 43fb4553a..2d3399d03 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "github.com/docker/docker/api/types" "math" "net/http" "os" @@ -39,6 +40,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/utils/common" "github.com/1Panel-dev/1Panel/backend/utils/compose" + "github.com/1Panel-dev/1Panel/backend/utils/docker" composeV2 "github.com/1Panel-dev/1Panel/backend/utils/docker" "github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/pkg/errors" @@ -1118,14 +1120,45 @@ func handleErr(install model.AppInstall, err error, out string) error { } func handleInstalled(appInstallList []model.AppInstall, updated bool, sync bool) ([]response.AppInstalledDTO, error) { - var res []response.AppInstalledDTO + var ( + res []response.AppInstalledDTO + containers []types.Container + ) + if sync { + cli, err := docker.NewClient() + if err != nil { + return nil, err + } + defer cli.Close() + containers, err = cli.ListAllContainers() + if err != nil { + return nil, err + } + } + for _, installed := range appInstallList { if updated && (installed.App.Type == "php" || installed.Status == constant.Installing || (installed.App.Key == constant.AppMysql && installed.Version == "5.6.51")) { continue } if sync { - if err := syncAppInstallStatus(&installed); err != nil { - global.LOG.Error("sync app install status error : ", err) + exist := false + for _, contain := range containers { + if contain.Names[0] == "/"+installed.ContainerName { + exist = true + switch contain.State { + case "exited": + installed.Status = constant.Stopped + case "running": + installed.Status = constant.Running + case "paused": + installed.Status = constant.Paused + } + break + } + } + if !exist { + installed.Status = constant.Error + installed.Message = buserr.WithName("ErrContainerNotFound", installed.ContainerName).Error() } } diff --git a/backend/utils/docker/docker.go b/backend/utils/docker/docker.go index ba9ff78ab..e11e42e1d 100644 --- a/backend/utils/docker/docker.go +++ b/backend/utils/docker/docker.go @@ -57,7 +57,7 @@ func (c Client) ListContainersByName(names []string) ([]types.Container, error) namesMap = make(map[string]bool) res []types.Container ) - options.All = false + options.All = true if len(names) > 0 { var array []filters.KeyValuePair for _, n := range names { @@ -77,6 +77,17 @@ func (c Client) ListContainersByName(names []string) ([]types.Container, error) } return res, nil } +func (c Client) ListAllContainers() ([]types.Container, error) { + var ( + options container.ListOptions + ) + options.All = true + containers, err := c.cli.ContainerList(context.Background(), options) + if err != nil { + return nil, err + } + return containers, nil +} func (c Client) CreateNetwork(name string) error { _, err := c.cli.NetworkCreate(context.Background(), name, types.NetworkCreate{ diff --git a/frontend/src/views/app-store/installed/index.vue b/frontend/src/views/app-store/installed/index.vue index f89bc3606..383167ff8 100644 --- a/frontend/src/views/app-store/installed/index.vue +++ b/frontend/src/views/app-store/installed/index.vue @@ -625,7 +625,7 @@ onMounted(() => { }, 1000); timer = setInterval(() => { search(); - }, 1000 * 20); + }, 1000 * 30); }); onUnmounted(() => {