Skip to content

Commit

Permalink
feat: add check before install (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH authored Feb 22, 2024
1 parent 6ba2c7e commit 2d80702
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions route/v2/compose_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ func (a *AppManagement) InstallComposeApp(ctx echo.Context, params codegen.Insta
})
}

if service.MyService.Compose().IsInstalling(composeApp.Name) {
message := fmt.Sprintf("compose app `%s` has another installing", composeApp.Name)
return ctx.JSON(http.StatusConflict, codegen.ComposeAppBadRequest{Message: &message})
}

// attach context key/value pairs from upstream
backgroundCtx := common.WithProperties(context.Background(), PropertiesFromQueryParams(ctx))

Expand Down
19 changes: 17 additions & 2 deletions service/compose_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"
"path/filepath"
"sync"

"github.com/IceWhaleTech/CasaOS-AppManagement/common"
"github.com/IceWhaleTech/CasaOS-AppManagement/pkg/config"
Expand All @@ -21,7 +22,9 @@ import (
"go.uber.org/zap"
)

type ComposeService struct{}
type ComposeService struct {
installationInProgress sync.Map
}

func (s *ComposeService) PrepareWorkingDirectory(name string) (string, error) {
workingDirectory := filepath.Join(config.AppInfo.AppsPath, name)
Expand All @@ -34,6 +37,11 @@ func (s *ComposeService) PrepareWorkingDirectory(name string) (string, error) {
return workingDirectory, nil
}

func (s *ComposeService) IsInstalling(appName string) bool {
_, ok := s.installationInProgress.Load(appName)
return ok
}

func (s *ComposeService) Install(ctx context.Context, composeApp *ComposeApp) error {
// set store_app_id (by convention is the same as app name at install time if it does not exist)
_, isStoreApp := composeApp.SetStoreAppID(composeApp.Name)
Expand Down Expand Up @@ -82,6 +90,11 @@ func (s *ComposeService) Install(ctx context.Context, composeApp *ComposeApp) er
}

go func(ctx context.Context) {
s.installationInProgress.Store(composeApp.Name, true)
defer func() {
s.installationInProgress.Delete(composeApp.Name)
}()

go PublishEventWrapper(ctx, common.EventTypeAppInstallBegin, nil)

defer PublishEventWrapper(ctx, common.EventTypeAppInstallEnd, nil)
Expand Down Expand Up @@ -179,7 +192,9 @@ func (s *ComposeService) List(ctx context.Context) (map[string]*ComposeApp, erro
}

func NewComposeService() *ComposeService {
return &ComposeService{}
return &ComposeService{
installationInProgress: sync.Map{},
}
}

func baseInterpolationMap() map[string]string {
Expand Down

0 comments on commit 2d80702

Please sign in to comment.