Skip to content

Implement some primitive cache locking. #29

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 1 commit into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions internal/cachedirectory/cachedirectory.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,43 @@ func (cacheDirectory *CacheDirectory) CheckOrCreateVersionFile(pull bool, versio
return errors.New(errorPushNonCache)
}

func (cacheDirectory *CacheDirectory) Lock() error {
file, err := os.Create(cacheDirectory.lockFilePath())
if err != nil {
return errors.Wrap(err, "Error locking cache directory.")
}
defer file.Close()
// If the cache directory is already locked, it's not really a huge issue since the purpose of the lock is mostly to check whether a `pull` operation was interrupted before pushing.
return nil
}

func (cacheDirectory *CacheDirectory) Unlock() error {
err := os.Remove(cacheDirectory.lockFilePath())
if err != nil {
return errors.Wrap(err, "Error unlocking cache directory.")
}
return nil
}

func (cacheDirectory *CacheDirectory) CheckLock() error {
_, err := os.Stat(cacheDirectory.lockFilePath())
if err == nil {
return errors.New("The cache directory is locked, likely due to a `pull` command being interrupted. Please run `pull` again to ensure all required data is downloaded.")
}
if os.IsNotExist(err) {
return nil
}
return errors.Wrap(err, "Error checking if cache directory is locked.")
}

func (cacheDirectory *CacheDirectory) versionFilePath() string {
return path.Join(cacheDirectory.path, ".codeql-actions-sync-version")
}

func (cacheDirectory *CacheDirectory) lockFilePath() string {
return path.Join(cacheDirectory.path, ".codeql-actions-sync-lock")
}

func (cacheDirectory *CacheDirectory) GitPath() string {
return path.Join(cacheDirectory.path, "git")
}
Expand Down
11 changes: 11 additions & 0 deletions internal/cachedirectory/cachedirectory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,14 @@ func TestCreateCacheDirectoryWithTrailingSlash(t *testing.T) {
err := cacheDirectory.CheckOrCreateVersionFile(true, aVersion)
require.NoError(t, err)
}

func TestLocking(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
cacheDirectory := NewCacheDirectory(path.Join(temporaryDirectory, "cache"))
require.NoError(t, cacheDirectory.CheckOrCreateVersionFile(true, aVersion))
require.NoError(t, cacheDirectory.Lock())
require.NoError(t, cacheDirectory.Lock())
require.Error(t, cacheDirectory.CheckLock())
require.NoError(t, cacheDirectory.Unlock())
require.NoError(t, cacheDirectory.CheckLock())
}
9 changes: 9 additions & 0 deletions internal/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ func Pull(ctx context.Context, cacheDirectory cachedirectory.CacheDirectory, sou
if err != nil {
return err
}
err = cacheDirectory.Lock()
if err != nil {
return err
}

var tokenClient *http.Client
if sourceToken != "" {
Expand Down Expand Up @@ -266,6 +270,11 @@ func Pull(ctx context.Context, cacheDirectory cachedirectory.CacheDirectory, sou
if err != nil {
return err
}

err = cacheDirectory.Unlock()
if err != nil {
return err
}
log.Print("Finished pulling the CodeQL Action repository and bundles!")
return nil
}
4 changes: 4 additions & 0 deletions internal/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ func Push(ctx context.Context, cacheDirectory cachedirectory.CacheDirectory, des
if err != nil {
return err
}
err = cacheDirectory.CheckLock()
if err != nil {
return err
}

destinationURL = strings.TrimRight(destinationURL, "/")
tokenSource := oauth2.StaticTokenSource(
Expand Down