Skip to content

Commit 3011e0c

Browse files
authored
Add -clear-cache flag to src action exec (#166)
* Add -clear-cache flag to src action exec * Remove dead code
1 parent da0ce74 commit 3011e0c

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

cmd/src/actions_cache.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type actionExecutionCacheKey struct {
2020
type actionExecutionCache interface {
2121
get(ctx context.Context, key actionExecutionCacheKey) (result PatchInput, ok bool, err error)
2222
set(ctx context.Context, key actionExecutionCacheKey, result PatchInput) error
23+
clear(ctx context.Context, key actionExecutionCacheKey) error
2324
}
2425

2526
type actionExecutionDiskCache struct {
@@ -82,6 +83,15 @@ func (c actionExecutionDiskCache) set(ctx context.Context, key actionExecutionCa
8283
return ioutil.WriteFile(path, data, 0600)
8384
}
8485

86+
func (c actionExecutionDiskCache) clear(ctx context.Context, key actionExecutionCacheKey) error {
87+
path, err := c.cacheFilePath(key)
88+
if err != nil {
89+
return err
90+
}
91+
92+
return os.Remove(path)
93+
}
94+
8595
// actionExecutionNoOpCache is an implementation of actionExecutionCache that does not store or
8696
// retrieve cache entries.
8797
type actionExecutionNoOpCache struct{}
@@ -93,3 +103,7 @@ func (actionExecutionNoOpCache) get(ctx context.Context, key actionExecutionCach
93103
func (actionExecutionNoOpCache) set(ctx context.Context, key actionExecutionCacheKey, result PatchInput) error {
94104
return nil
95105
}
106+
107+
func (actionExecutionNoOpCache) clear(ctx context.Context, key actionExecutionCacheKey) error {
108+
return nil
109+
}

cmd/src/actions_exec.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,12 @@ Format of the action JSON files:
134134
var (
135135
fileFlag = flagSet.String("f", "-", "The action file. If not given or '-' standard input is used. (Required)")
136136
parallelismFlag = flagSet.Int("j", runtime.GOMAXPROCS(0), "The number of parallel jobs.")
137-
cacheDirFlag = flagSet.String("cache", displayUserCacheDir, "Directory for caching results.")
138-
keepLogsFlag = flagSet.Bool("keep-logs", false, "Do not remove execution log files when done.")
139-
timeoutFlag = flagSet.Duration("timeout", defaultTimeout, "The maximum duration a single action run can take (excluding the building of Docker images).")
137+
138+
cacheDirFlag = flagSet.String("cache", displayUserCacheDir, "Directory for caching results.")
139+
clearCacheFlag = flagSet.Bool("clear-cache", false, "Remove possibly cached results for an action before executing it.")
140+
141+
keepLogsFlag = flagSet.Bool("keep-logs", false, "Do not remove execution log files when done.")
142+
timeoutFlag = flagSet.Duration("timeout", defaultTimeout, "The maximum duration a single action run can take (excluding the building of Docker images).")
140143

141144
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.")
142145
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.")
@@ -196,9 +199,10 @@ Format of the action JSON files:
196199
}
197200

198201
opts := actionExecutorOptions{
199-
timeout: *timeoutFlag,
200-
keepLogs: *keepLogsFlag,
201-
cache: actionExecutionDiskCache{dir: *cacheDirFlag},
202+
timeout: *timeoutFlag,
203+
keepLogs: *keepLogsFlag,
204+
clearCache: *clearCacheFlag,
205+
cache: actionExecutionDiskCache{dir: *cacheDirFlag},
202206
}
203207
if !*verbose {
204208
opts.onUpdate = newTerminalUI(*keepLogsFlag)

cmd/src/actions_exec_backend.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ type actionExecutorOptions struct {
1212
keepLogs bool
1313
timeout time.Duration
1414

15-
cache actionExecutionCache
15+
clearCache bool
16+
cache actionExecutionCache
17+
1618
onUpdate func(map[ActionRepo]ActionRepoStatus)
1719
}
1820

cmd/src/actions_exec_backend_runner.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,19 @@ type ActionRepoStatus struct {
3636
func (x *actionExecutor) do(ctx context.Context, repo ActionRepo) (err error) {
3737
// Check if cached.
3838
cacheKey := actionExecutionCacheKey{Repo: repo, Runs: x.action.Steps}
39-
if result, ok, err := x.opt.cache.get(ctx, cacheKey); err != nil {
40-
return errors.Wrapf(err, "checking cache for %s", repo.Name)
41-
} else if ok {
42-
status := ActionRepoStatus{Cached: true, Patch: result}
43-
x.updateRepoStatus(repo, status)
44-
x.logger.RepoCacheHit(repo, status.Patch != PatchInput{})
45-
return nil
39+
if x.opt.clearCache {
40+
if err := x.opt.cache.clear(ctx, cacheKey); err != nil {
41+
return errors.Wrapf(err, "clearing cache for %s", repo.Name)
42+
}
43+
} else {
44+
if result, ok, err := x.opt.cache.get(ctx, cacheKey); err != nil {
45+
return errors.Wrapf(err, "checking cache for %s", repo.Name)
46+
} else if ok {
47+
status := ActionRepoStatus{Cached: true, Patch: result}
48+
x.updateRepoStatus(repo, status)
49+
x.logger.RepoCacheHit(repo, status.Patch != PatchInput{})
50+
return nil
51+
}
4652
}
4753

4854
prefix := "action-" + strings.Replace(strings.Replace(repo.Name, "/", "-", -1), "github.com-", "", -1)

0 commit comments

Comments
 (0)