Skip to content

Commit 0dfc771

Browse files
committed
Address PR feedback.
1 parent 3d17a1c commit 0dfc771

File tree

3 files changed

+43
-31
lines changed

3 files changed

+43
-31
lines changed

internal/constants/constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,4 +495,4 @@ const IgnoreEnvEnvVarName = "ACTIVESTATE_CLI_IGNORE_ENV"
495495
const BuildProgressUrlPathName = "distributions"
496496

497497
// RuntimeCacheSizeConfigKey is the config key for the runtime cache size.
498-
const RuntimeCacheSizeConfigKey = "runtime.cache_size"
498+
const RuntimeCacheSizeConfigKey = "runtime.cache.size"

pkg/runtime/depot.go

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/ActiveState/cli/internal/installation/storage"
2020
"github.com/ActiveState/cli/internal/logging"
2121
configMediator "github.com/ActiveState/cli/internal/mediators/config"
22-
"github.com/ActiveState/cli/internal/multilog"
2322
"github.com/ActiveState/cli/internal/sliceutils"
2423
"github.com/ActiveState/cli/internal/smartlink"
2524
)
@@ -51,6 +50,8 @@ type artifactInfo struct {
5150
InUse bool `json:"inUse"`
5251
Size int64 `json:"size"`
5352
LastAccessTime int64 `json:"lastAccessTime"`
53+
54+
id strfmt.UUID // for convenience when removing stale artifacts; should NOT have json tag
5455
}
5556

5657
type ErrVolumeMismatch struct {
@@ -220,7 +221,10 @@ func (d *depot) DeployViaLink(id strfmt.UUID, relativeSrc, absoluteDest string)
220221
Files: files.RelativePaths(),
221222
RelativeSrc: relativeSrc,
222223
})
223-
d.recordUse(id)
224+
err = d.recordUse(id)
225+
if err != nil {
226+
return errs.Wrap(err, "Could not record artifact use")
227+
}
224228

225229
return nil
226230
}
@@ -278,26 +282,26 @@ func (d *depot) DeployViaCopy(id strfmt.UUID, relativeSrc, absoluteDest string)
278282
Files: files.RelativePaths(),
279283
RelativeSrc: relativeSrc,
280284
})
281-
d.recordUse(id)
285+
err = d.recordUse(id)
286+
if err != nil {
287+
return errs.Wrap(err, "Could not record artifact use")
288+
}
282289

283290
return nil
284291
}
285292

286-
func (d *depot) recordUse(id strfmt.UUID) {
293+
func (d *depot) recordUse(id strfmt.UUID) error {
287294
// Ensure a cache entry for this artifact exists and then update its last access time.
288295
if _, exists := d.config.Cache[id]; !exists {
289296
size, err := fileutils.GetDirSize(d.Path(id))
290297
if err != nil {
291-
multilog.Error("Could not get artifact size on disk: %v", err)
292-
size = 0
298+
return errs.Wrap(err, "Could not get artifact size on disk")
293299
}
294-
logging.Debug("Recording artifact '%s' with size %.1f MB", id.String(), float64(size)/float64(MB))
295-
d.config.Cache[id] = &artifactInfo{Size: size}
296-
} else {
297-
logging.Debug("Recording use of artifact '%s'", id.String())
300+
d.config.Cache[id] = &artifactInfo{Size: size, id: id}
298301
}
299302
d.config.Cache[id].InUse = true
300303
d.config.Cache[id].LastAccessTime = time.Now().Unix()
304+
return nil
301305
}
302306

303307
func (d *depot) Undeploy(id strfmt.UUID, relativeSrc, path string) error {
@@ -422,7 +426,10 @@ func (d *depot) Save() error {
422426
logging.Debug("Artifact '%s' is no longer in use", id.String())
423427
}
424428
}
425-
d.removeStaleArtifacts()
429+
err := d.removeStaleArtifacts()
430+
if err != nil {
431+
return errs.Wrap(err, "Could not remove stale artifacts")
432+
}
426433

427434
// Write config file changes to disk
428435
configFile := filepath.Join(d.depotPath, depotFile)
@@ -468,36 +475,37 @@ func someFilesExist(filePaths []string, basePath string) bool {
468475

469476
// removeStaleArtifacts iterates over all unused artifacts in the depot, sorts them by last access
470477
// time, and removes them until the size of cached artifacts is under the limit.
471-
func (d *depot) removeStaleArtifacts() {
472-
type artifact struct {
473-
id strfmt.UUID
474-
info *artifactInfo
475-
}
478+
func (d *depot) removeStaleArtifacts() error {
476479
var totalSize int64
477-
unusedArtifacts := make([]*artifact, 0)
480+
unusedArtifacts := make([]*artifactInfo, 0)
478481

479-
for id, info := range d.config.Cache {
482+
for _, info := range d.config.Cache {
480483
if !info.InUse {
481484
totalSize += info.Size
482-
unusedArtifacts = append(unusedArtifacts, &artifact{id: id, info: info})
485+
unusedArtifacts = append(unusedArtifacts, info)
483486
}
484487
}
485488
logging.Debug("There are %d unused artifacts totaling %.1f MB in size", len(unusedArtifacts), float64(totalSize)/float64(MB))
486489

487490
sort.Slice(unusedArtifacts, func(i, j int) bool {
488-
return unusedArtifacts[i].info.LastAccessTime < unusedArtifacts[j].info.LastAccessTime
491+
return unusedArtifacts[i].LastAccessTime < unusedArtifacts[j].LastAccessTime
489492
})
490493

494+
var rerr error
491495
for _, artifact := range unusedArtifacts {
492496
if totalSize <= d.cacheSize {
493497
break // done
494498
}
495-
logging.Debug("Removing cached artifact '%s', last accessed on %s", artifact.id.String(), time.Unix(artifact.info.LastAccessTime, 0).Format(time.UnixDate))
496499
if err := os.RemoveAll(d.Path(artifact.id)); err == nil {
497-
totalSize -= artifact.info.Size
500+
totalSize -= artifact.Size
498501
} else {
499-
multilog.Error("Could not delete old artifact: %v", err)
502+
if err := errs.Wrap(err, "Could not delete old artifact"); rerr == nil {
503+
rerr = err
504+
} else {
505+
rerr = errs.Pack(rerr, err)
506+
}
500507
}
501508
delete(d.config.Cache, artifact.id)
502509
}
510+
return rerr
503511
}

test/integration/runtime_int_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/ActiveState/cli/internal/constants"
10+
"github.com/ActiveState/cli/internal/fileutils"
1011
"github.com/ActiveState/cli/internal/osutils"
1112
"github.com/ActiveState/cli/internal/testhelpers/e2e"
1213
"github.com/ActiveState/cli/internal/testhelpers/osutil"
@@ -223,23 +224,26 @@ func (suite *RuntimeIntegrationTestSuite) TestRuntimeCache() {
223224
cp.Expect("Downloading")
224225
cp.ExpectExitCode(0)
225226

227+
depot := filepath.Join(ts.Dirs.Cache, "depot")
228+
artifacts, err := fileutils.ListDirSimple(depot, true)
229+
suite.Require().NoError(err)
230+
226231
cp = ts.Spawn("switch", "mingw") // should not remove cached shared/zlib artifact
227232
cp.ExpectExitCode(0)
228233

229-
cp = ts.Spawn("install", "shared/zlib")
230-
cp.Expect("Installing")
231-
cp.ExpectExitCode(0)
232-
suite.Assert().NotContains(cp.Snapshot(), "Downloading", "shared/zlib should have been cached")
234+
artifacts2, err := fileutils.ListDirSimple(depot, true)
235+
suite.Require().NoError(err)
236+
suite.Assert().Equal(artifacts, artifacts2, "shared/zlib should have remained in the cache")
233237

234238
cp = ts.Spawn("config", "set", constants.RuntimeCacheSizeConfigKey, "0")
235239
cp.ExpectExitCode(0)
236240

237241
cp = ts.Spawn("switch", "main") // should remove cached shared/zlib artifact
238242
cp.ExpectExitCode(0)
239243

240-
cp = ts.Spawn("install", "shared/zlib")
241-
cp.Expect("Downloading")
242-
cp.ExpectExitCode(0)
244+
artifacts3, err := fileutils.ListDirSimple(depot, true)
245+
suite.Require().NoError(err)
246+
suite.Assert().NotEqual(artifacts, artifacts3, "shared/zlib should have been removed from the cache")
243247
}
244248

245249
func TestRuntimeIntegrationTestSuite(t *testing.T) {

0 commit comments

Comments
 (0)