Skip to content

Commit

Permalink
refactor: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Bianconi committed Jan 16, 2023
1 parent 6fb6e12 commit 6afc55a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 27 deletions.
4 changes: 2 additions & 2 deletions buildcache/build_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
// Used registers the .last-used time in the directory
func Used(dir *paths.Path) error {
unusedTTL := time.Hour
_, err := newDirectoryCache(dir.Parent().String(), unusedTTL).
_, err := newDirectoryCache(dir.Parent(), unusedTTL).
GetOrCreate(dir.Base(), time.Now())
return err
}
Expand All @@ -33,5 +33,5 @@ func Used(dir *paths.Path) error {
// To know how long ago a directory has been last used
// it checks into the .last-used file.
func Purge(baseDir *paths.Path, ttl time.Duration) {
newDirectoryCache(baseDir.String(), ttl).Purge()
newDirectoryCache(baseDir, ttl).Purge()
}
2 changes: 1 addition & 1 deletion buildcache/build_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func requireCorrectUpdate(t *testing.T, dir *paths.Path, prevModTime time.Time)
expectedFile := dir.Join(lastUsedFileName)
fileInfo, err := expectedFile.Stat()
require.Nil(t, err)
require.GreaterOrEqual(t, fileInfo.ModTime(), prevModTime)
require.Greater(t, fileInfo.ModTime(), prevModTime)
}

func TestPurge(t *testing.T) {
Expand Down
23 changes: 9 additions & 14 deletions buildcache/directory_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,17 @@ const (
)

type directoryCache struct {
baseDir string
baseDir *paths.Path
ttl time.Duration
}

func (dc *directoryCache) basePath() *paths.Path {
return paths.New(dc.baseDir)
}

func (dc *directoryCache) isExpired(key string) time.Duration {
modTime, _ := dc.modTime(key)
return dc.ttl - time.Since(modTime)
}

func (dc *directoryCache) modTime(key string) (time.Time, error) {
fileInfo, err := dc.basePath().Join(key, lastUsedFileName).Stat()
fileInfo, err := dc.baseDir.Join(key, lastUsedFileName).Stat()
if err != nil {
// folders with a missing last used file are not purged
return time.Now().Add(time.Minute), err
Expand All @@ -58,9 +54,9 @@ func (dc *directoryCache) GetOrCreate(key string, value time.Time) (time.Time, e
existing = false
}

subDir := dc.basePath().Join(key)
subDir := dc.baseDir.Join(key)
err = subDir.MkdirAll()
if err != nil || existing {
if err != nil {
return modTime, err
}
err = subDir.Join(lastUsedFileName).WriteFile([]byte{})
Expand All @@ -71,7 +67,7 @@ func (dc *directoryCache) GetOrCreate(key string, value time.Time) (time.Time, e
}

func (dc *directoryCache) Purge() error {
files, err := dc.basePath().ReadDir()
files, err := dc.baseDir.ReadDir()
if err != nil {
return err
}
Expand All @@ -88,16 +84,15 @@ func (dc *directoryCache) removeIfExpired(dir *paths.Path) {
if lifeExpectancy > 0 {
return
}
subDir := dc.basePath().Join(dir.Base())
logrus.Tracef(`Purging cache directory "%s". Expired by %s\n`, subDir, lifeExpectancy)
err := subDir.RemoveAll()
logrus.Tracef(`Purging cache directory "%s". Expired by %s\n`, dir, lifeExpectancy)
err := dir.RemoveAll()

if err != nil {
logrus.Tracef(`Error while pruning cache directory "%s".\n%s\n`, subDir, errors.WithStack(err))
logrus.Tracef(`Error while pruning cache directory "%s".\n%s\n`, dir, errors.WithStack(err))
}
}

func newDirectoryCache(baseDir string, ttl time.Duration) *directoryCache {
func newDirectoryCache(baseDir *paths.Path, ttl time.Duration) *directoryCache {
return &directoryCache{
baseDir: baseDir,
ttl: ttl,
Expand Down
14 changes: 7 additions & 7 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,19 +278,19 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
// maybePurgeBuildCache runs the build files cache purge if the policy conditions are met.
func maybePurgeBuildCache() {

compilationSinceLastPurge := inventory.Store.GetInt("build_cache.compilation_count_since_last_purge")
compilationsBeforePurge := configuration.Settings.GetUint("build_cache.compilations_before_purge")
// 0 means never purge
if compilationsBeforePurge == 0 {
return
}
compilationSinceLastPurge := inventory.Store.GetUint("build_cache.compilation_count_since_last_purge")
compilationSinceLastPurge++
inventory.Store.Set("build_cache.compilation_count_since_last_purge", compilationSinceLastPurge)
defer inventory.WriteStore()

// 0 means never purge
purgeAfterCompilationCount := configuration.Settings.GetInt("build_cache.compilations_before_purge")

if purgeAfterCompilationCount == 0 || compilationSinceLastPurge < purgeAfterCompilationCount {
if compilationsBeforePurge == 0 || compilationSinceLastPurge < compilationsBeforePurge {
return
}
inventory.Store.Set("build_cache.compilation_count_since_last_purge", 0)

cacheTTL := configuration.Settings.GetDuration("build_cache.ttl").Abs()
buildcache.Purge(paths.TempDir().Join("arduino", "cores"), cacheTTL)
buildcache.Purge(paths.TempDir().Join("arduino", "sketches"), cacheTTL)
Expand Down
6 changes: 3 additions & 3 deletions internal/integrationtest/compile_1/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ func compileWithCachePurgeNeeded(t *testing.T, env *integrationtest.Environment,

// purge case: last used file too old
oldDir1 := baseDir.Join("test_old_sketch_1")
require.NoError(t, os.MkdirAll(oldDir1.String(), 0770))
require.NoError(t, oldDir1.MkdirAll())
require.NoError(t, oldDir1.Join(".last-used").WriteFile([]byte{}))
require.NoError(t, os.Chtimes(oldDir1.Join(".last-used").String(), time.Now(), time.Unix(0, 0)))
require.NoError(t, oldDir1.Join(".last-used").Chtimes(time.Now(), time.Unix(0, 0)))
// no purge case: last used file not existing
missingFileDir := baseDir.Join("test_sketch_2")
require.NoError(t, os.MkdirAll(missingFileDir.String(), 0770))
require.NoError(t, missingFileDir.MkdirAll())

defer oldDir1.RemoveAll()
defer missingFileDir.RemoveAll()
Expand Down

0 comments on commit 6afc55a

Please sign in to comment.