From 257237a6df7e1fa8d27bcb563795058a38e16286 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Mon, 30 Mar 2020 16:26:03 +0200 Subject: [PATCH 1/2] Add -clear-cache flag to src action exec --- cmd/src/actions_cache.go | 14 ++++++++++++++ cmd/src/actions_exec.go | 19 +++++++++++++------ cmd/src/actions_exec_backend.go | 4 +++- cmd/src/actions_exec_backend_runner.go | 20 +++++++++++++------- 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/cmd/src/actions_cache.go b/cmd/src/actions_cache.go index e99b9f3b96..3e72862991 100644 --- a/cmd/src/actions_cache.go +++ b/cmd/src/actions_cache.go @@ -20,6 +20,7 @@ type actionExecutionCacheKey struct { type actionExecutionCache interface { get(ctx context.Context, key actionExecutionCacheKey) (result PatchInput, ok bool, err error) set(ctx context.Context, key actionExecutionCacheKey, result PatchInput) error + clear(ctx context.Context, key actionExecutionCacheKey) error } type actionExecutionDiskCache struct { @@ -82,6 +83,15 @@ func (c actionExecutionDiskCache) set(ctx context.Context, key actionExecutionCa return ioutil.WriteFile(path, data, 0600) } +func (c actionExecutionDiskCache) clear(ctx context.Context, key actionExecutionCacheKey) error { + path, err := c.cacheFilePath(key) + if err != nil { + return err + } + + return os.Remove(path) +} + // actionExecutionNoOpCache is an implementation of actionExecutionCache that does not store or // retrieve cache entries. type actionExecutionNoOpCache struct{} @@ -93,3 +103,7 @@ func (actionExecutionNoOpCache) get(ctx context.Context, key actionExecutionCach func (actionExecutionNoOpCache) set(ctx context.Context, key actionExecutionCacheKey, result PatchInput) error { return nil } + +func (actionExecutionNoOpCache) clear(ctx context.Context, key actionExecutionCacheKey) error { + return nil +} diff --git a/cmd/src/actions_exec.go b/cmd/src/actions_exec.go index 750c242950..80d1ad1200 100644 --- a/cmd/src/actions_exec.go +++ b/cmd/src/actions_exec.go @@ -134,9 +134,12 @@ Format of the action JSON files: var ( fileFlag = flagSet.String("f", "-", "The action file. If not given or '-' standard input is used. (Required)") parallelismFlag = flagSet.Int("j", runtime.GOMAXPROCS(0), "The number of parallel jobs.") - cacheDirFlag = flagSet.String("cache", displayUserCacheDir, "Directory for caching results.") - keepLogsFlag = flagSet.Bool("keep-logs", false, "Do not remove execution log files when done.") - timeoutFlag = flagSet.Duration("timeout", defaultTimeout, "The maximum duration a single action run can take (excluding the building of Docker images).") + + cacheDirFlag = flagSet.String("cache", displayUserCacheDir, "Directory for caching results.") + clearCacheFlag = flagSet.Bool("clear-cache", false, "Remove possibly cached results for an action before executing it.") + + keepLogsFlag = flagSet.Bool("keep-logs", false, "Do not remove execution log files when done.") + timeoutFlag = flagSet.Duration("timeout", defaultTimeout, "The maximum duration a single action run can take (excluding the building of Docker images).") createPatchSetFlag = flagSet.Bool("create-patchset", false, "Create a patch set from the produced set of patches. When the execution of the action fails in a single repository a prompt will ask to confirm or reject the patch set creation.") forceCreatePatchSetFlag = flagSet.Bool("force-create-patchset", false, "Force creation of patch set from the produced set of patches, without asking for confirmation even when the execution of the action failed for a subset of repositories.") @@ -161,6 +164,9 @@ Format of the action JSON files: return errors.New("cache is not a valid path") } + if *clearCacheFlag { + } + var ( actionFile []byte err error @@ -196,9 +202,10 @@ Format of the action JSON files: } opts := actionExecutorOptions{ - timeout: *timeoutFlag, - keepLogs: *keepLogsFlag, - cache: actionExecutionDiskCache{dir: *cacheDirFlag}, + timeout: *timeoutFlag, + keepLogs: *keepLogsFlag, + clearCache: *clearCacheFlag, + cache: actionExecutionDiskCache{dir: *cacheDirFlag}, } if !*verbose { opts.onUpdate = newTerminalUI(*keepLogsFlag) diff --git a/cmd/src/actions_exec_backend.go b/cmd/src/actions_exec_backend.go index d86064415b..ea60dc674f 100644 --- a/cmd/src/actions_exec_backend.go +++ b/cmd/src/actions_exec_backend.go @@ -12,7 +12,9 @@ type actionExecutorOptions struct { keepLogs bool timeout time.Duration - cache actionExecutionCache + clearCache bool + cache actionExecutionCache + onUpdate func(map[ActionRepo]ActionRepoStatus) } diff --git a/cmd/src/actions_exec_backend_runner.go b/cmd/src/actions_exec_backend_runner.go index aa31422f4d..cc49107cbe 100644 --- a/cmd/src/actions_exec_backend_runner.go +++ b/cmd/src/actions_exec_backend_runner.go @@ -36,13 +36,19 @@ type ActionRepoStatus struct { func (x *actionExecutor) do(ctx context.Context, repo ActionRepo) (err error) { // Check if cached. cacheKey := actionExecutionCacheKey{Repo: repo, Runs: x.action.Steps} - if result, ok, err := x.opt.cache.get(ctx, cacheKey); err != nil { - return errors.Wrapf(err, "checking cache for %s", repo.Name) - } else if ok { - status := ActionRepoStatus{Cached: true, Patch: result} - x.updateRepoStatus(repo, status) - x.logger.RepoCacheHit(repo, status.Patch != PatchInput{}) - return nil + if x.opt.clearCache { + if err := x.opt.cache.clear(ctx, cacheKey); err != nil { + return errors.Wrapf(err, "clearing cache for %s", repo.Name) + } + } else { + if result, ok, err := x.opt.cache.get(ctx, cacheKey); err != nil { + return errors.Wrapf(err, "checking cache for %s", repo.Name) + } else if ok { + status := ActionRepoStatus{Cached: true, Patch: result} + x.updateRepoStatus(repo, status) + x.logger.RepoCacheHit(repo, status.Patch != PatchInput{}) + return nil + } } prefix := "action-" + strings.Replace(strings.Replace(repo.Name, "/", "-", -1), "github.com-", "", -1) From 895472986f266b9082b1cfdad0dad04d69e365c5 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Mon, 30 Mar 2020 16:50:40 +0200 Subject: [PATCH 2/2] Remove dead code --- cmd/src/actions_exec.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/cmd/src/actions_exec.go b/cmd/src/actions_exec.go index 80d1ad1200..8012dca1ed 100644 --- a/cmd/src/actions_exec.go +++ b/cmd/src/actions_exec.go @@ -164,9 +164,6 @@ Format of the action JSON files: return errors.New("cache is not a valid path") } - if *clearCacheFlag { - } - var ( actionFile []byte err error