-
-
Notifications
You must be signed in to change notification settings - Fork 405
feat: purge build cache #2033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: purge build cache #2033
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
165789c
feat: purge cache after expiration time
b65c37b
fix: handle cache dir not created
3b11d67
Merge branch 'master' into feat/purge-build-cache
776d66c
refactor: new interface
446e2af
style: lint
7bf832d
Merge branch 'master' into feat/purge-build-cache
Bikappa d9d07e4
Merge branch 'master' into feat/purge-build-cache
6ac7fce
fix: cache age check
9f3de10
Merge branch 'master' into feat/purge-build-cache
Bikappa bf58720
refactor: cleaner flow
9aa2d1f
Merge branch 'feat/purge-build-cache' of github.com:arduino/arduino-c…
c71bf14
Merge branch 'master' into feat/purge-build-cache
Bikappa 93fef69
Merge branch 'master' into feat/purge-build-cache
Bikappa 4c64cb5
Merge branch 'master' into feat/purge-build-cache
Bikappa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,6 @@ venv | |
/docsgen/arduino-cli.exe | ||
/docs/rpc/*.md | ||
/docs/commands/*.md | ||
|
||
# Delve debugger binary file | ||
__debug_bin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.html | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to [email protected]. | ||
|
||
package buildcache | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/arduino/go-paths-helper" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const lastUsedFileName = ".last-used" | ||
|
||
// BuildCache represents a cache of built files (sketches and cores), it's designed | ||
// to work on directories. Given a directory as "base" it handles direct subdirectories as | ||
// keys | ||
type BuildCache struct { | ||
baseDir *paths.Path | ||
} | ||
|
||
// GetOrCreate retrieves or creates the cache directory at the given path | ||
// If the cache already exists the lifetime of the cache is extended. | ||
func (bc *BuildCache) GetOrCreate(key string) (*paths.Path, error) { | ||
keyDir := bc.baseDir.Join(key) | ||
if err := keyDir.MkdirAll(); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := keyDir.Join(lastUsedFileName).WriteFile([]byte{}); err != nil { | ||
return nil, err | ||
} | ||
return keyDir, nil | ||
} | ||
|
||
// Purge removes all cache directories within baseDir that have expired | ||
// To know how long ago a directory has been last used | ||
// it checks into the .last-used file. | ||
func (bc *BuildCache) Purge(ttl time.Duration) { | ||
files, err := bc.baseDir.ReadDir() | ||
if err != nil { | ||
return | ||
} | ||
for _, file := range files { | ||
if file.IsDir() { | ||
removeIfExpired(file, ttl) | ||
} | ||
} | ||
} | ||
|
||
// New instantiates a build cache | ||
func New(baseDir *paths.Path) *BuildCache { | ||
return &BuildCache{baseDir} | ||
} | ||
|
||
func removeIfExpired(dir *paths.Path, ttl time.Duration) { | ||
fileInfo, err := dir.Join().Stat() | ||
if err != nil { | ||
return | ||
} | ||
lifeExpectancy := ttl - time.Since(fileInfo.ModTime()) | ||
if lifeExpectancy > 0 { | ||
return | ||
} | ||
logrus.Tracef(`Purging cache directory "%s". Expired by %s`, dir, lifeExpectancy.Abs()) | ||
err = dir.RemoveAll() | ||
if err != nil { | ||
logrus.Tracef(`Error while pruning cache directory "%s": %s`, dir, errors.WithStack(err)) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.html | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to [email protected]. | ||
|
||
package buildcache | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/arduino/go-paths-helper" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_UpdateLastUsedFileNotExisting(t *testing.T) { | ||
testBuildDir := paths.New(t.TempDir(), "sketches", "xxx") | ||
require.NoError(t, testBuildDir.MkdirAll()) | ||
timeBeforeUpdating := time.Unix(0, 0) | ||
requireCorrectUpdate(t, testBuildDir, timeBeforeUpdating) | ||
} | ||
|
||
func Test_UpdateLastUsedFileExisting(t *testing.T) { | ||
testBuildDir := paths.New(t.TempDir(), "sketches", "xxx") | ||
require.NoError(t, testBuildDir.MkdirAll()) | ||
|
||
// create the file | ||
preExistingFile := testBuildDir.Join(lastUsedFileName) | ||
require.NoError(t, preExistingFile.WriteFile([]byte{})) | ||
timeBeforeUpdating := time.Now().Add(-time.Second) | ||
preExistingFile.Chtimes(time.Now(), timeBeforeUpdating) | ||
requireCorrectUpdate(t, testBuildDir, timeBeforeUpdating) | ||
} | ||
|
||
func requireCorrectUpdate(t *testing.T, dir *paths.Path, prevModTime time.Time) { | ||
_, err := New(dir.Parent()).GetOrCreate(dir.Base()) | ||
require.NoError(t, err) | ||
expectedFile := dir.Join(lastUsedFileName) | ||
fileInfo, err := expectedFile.Stat() | ||
require.Nil(t, err) | ||
require.Greater(t, fileInfo.ModTime(), prevModTime) | ||
} | ||
|
||
func TestPurge(t *testing.T) { | ||
ttl := time.Minute | ||
|
||
dirToPurge := paths.New(t.TempDir(), "root") | ||
|
||
lastUsedTimesByDirPath := map[*paths.Path]time.Time{ | ||
(dirToPurge.Join("old")): time.Now().Add(-ttl - time.Hour), | ||
(dirToPurge.Join("fresh")): time.Now().Add(-ttl + time.Minute), | ||
} | ||
|
||
// create the metadata files | ||
for dirPath, lastUsedTime := range lastUsedTimesByDirPath { | ||
require.NoError(t, dirPath.MkdirAll()) | ||
infoFilePath := dirPath.Join(lastUsedFileName).Canonical() | ||
require.NoError(t, infoFilePath.WriteFile([]byte{})) | ||
// make sure access time does not matter | ||
accesstime := time.Now() | ||
require.NoError(t, infoFilePath.Chtimes(accesstime, lastUsedTime)) | ||
} | ||
|
||
New(dirToPurge).Purge(ttl) | ||
|
||
files, err := dirToPurge.Join("fresh").Stat() | ||
require.Nil(t, err) | ||
require.True(t, files.IsDir()) | ||
require.True(t, dirToPurge.Exist()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.