Skip to content

Commit

Permalink
Merge pull request #3543 from ActiveState/DX-3040
Browse files Browse the repository at this point in the history
Check process in use
  • Loading branch information
MDrakos authored Oct 17, 2024
2 parents f600a1d + 6d4951d commit f4cc3c9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 2 additions & 0 deletions internal/locale/locales/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1574,3 +1574,5 @@ config_get_help:
other: "To GET the value for a specific config key run '[ACTIONABLE]state config get <key>[/RESET]'"
config_set_help:
other: "To SET the value for a specific config key run '[ACTIONABLE]state config set <key> <value>[/RESET]'"
err_clean_in_use:
other: "Could not clean your cache as you appear to have a runtime in use. Please stop any running State Tool processes and project runtime processes and try again."
43 changes: 39 additions & 4 deletions internal/runners/clean/cache.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package clean

import (
"context"
"os"
"time"

"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
Expand All @@ -15,6 +17,7 @@ import (
)

type Cache struct {
prime primeable
output output.Outputer
config configurable
confirm promptable
Expand All @@ -28,11 +31,12 @@ type CacheParams struct {
}

func NewCache(prime primeable) *Cache {
return newCache(prime.Output(), prime.Config(), prime.Prompt(), prime.IPComm())
return newCache(prime, prime.Output(), prime.Config(), prime.Prompt(), prime.IPComm())
}

func newCache(output output.Outputer, cfg configurable, confirm promptable, ipComm svcctl.IPCommunicator) *Cache {
func newCache(prime primeable, output output.Outputer, cfg configurable, confirm promptable, ipComm svcctl.IPCommunicator) *Cache {
return &Cache{
prime: prime,
output: output,
config: cfg,
confirm: confirm,
Expand Down Expand Up @@ -76,9 +80,16 @@ func (c *Cache) removeCache(path string, force bool) error {
}
}

logging.Debug("Removing cache path: %s", path)
err := removeCache(c.path)
inUse, err := c.checkPathInUse(path)
if err != nil {
return errs.Wrap(err, "Failed to check if path is in use")
}
if inUse {
return locale.NewInputError("err_clean_in_use")
}

logging.Debug("Removing cache path: %s", path)
if err := removeCache(c.path); err != nil {
return errs.Wrap(err, "Failed to remove cache")
}

Expand All @@ -97,6 +108,14 @@ func (c *Cache) removeProjectCache(projectDir, namespace string, force bool) err
}
}

inUse, err := c.checkPathInUse(projectDir)
if err != nil {
return errs.Wrap(err, "Failed to check if path is in use")
}
if inUse {
return locale.NewInputError("err_clean_in_use")
}

projectInstallPath, err := runtime_helpers.TargetDirFromProjectDir(projectDir)
if err != nil {
return errs.Wrap(err, "Failed to determine project install path")
Expand All @@ -109,3 +128,19 @@ func (c *Cache) removeProjectCache(projectDir, namespace string, force bool) err

return nil
}

func (c *Cache) checkPathInUse(path string) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

procs, err := c.prime.SvcModel().GetProcessesInUse(ctx, path)
if err != nil {
return false, errs.Wrap(err, "Failed to get processes in use")
}

if len(procs) > 0 {
return true, nil
}

return false, nil
}
1 change: 1 addition & 0 deletions internal/runners/clean/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type primeable interface {
primer.Configurer
primer.IPCommunicator
primer.Analyticer
primer.SvcModeler
}

func NewUninstall(prime primeable) (*Uninstall, error) {
Expand Down

0 comments on commit f4cc3c9

Please sign in to comment.