Skip to content

Commit 4b9004a

Browse files
authored
Transfer less unused info for exec command (#691)
We don't need the entire batch spec for simply executing a workspace, so we reduce the payload transferred here. This also has the nice side-effect that we finally got to clean up the Task struct a little from things not specific to that given task.
1 parent 73eb999 commit 4b9004a

9 files changed

Lines changed: 90 additions & 78 deletions

File tree

cmd/src/batch_common.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/mattn/go-isatty"
1919

2020
batcheslib "github.com/sourcegraph/sourcegraph/lib/batches"
21+
"github.com/sourcegraph/sourcegraph/lib/batches/template"
2122

2223
"github.com/sourcegraph/src-cli/internal/api"
2324
"github.com/sourcegraph/src-cli/internal/batches"
@@ -342,7 +343,14 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
342343
})
343344

344345
ui.CheckingCache()
345-
tasks := svc.BuildTasks(ctx, batchSpec, workspaces)
346+
tasks := svc.BuildTasks(
347+
ctx,
348+
&template.BatchChangeAttributes{
349+
Name: batchSpec.Name,
350+
Description: batchSpec.Description,
351+
},
352+
workspaces,
353+
)
346354
var (
347355
specs []*batcheslib.ChangesetSpec
348356
uncachedTasks []*executor.Task
@@ -351,15 +359,16 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
351359
coord.ClearCache(ctx, tasks)
352360
uncachedTasks = tasks
353361
} else {
354-
uncachedTasks, specs, err = coord.CheckCache(ctx, tasks)
362+
// Check the cache for completely cached executions.
363+
uncachedTasks, specs, err = coord.CheckCache(ctx, batchSpec, tasks)
355364
if err != nil {
356365
return err
357366
}
358367
}
359368
ui.CheckingCacheSuccess(len(specs), len(uncachedTasks))
360369

361370
taskExecUI := ui.ExecutingTasks(*verbose, opts.flags.parallelism)
362-
freshSpecs, logFiles, execErr := coord.Execute(ctx, uncachedTasks, batchSpec, taskExecUI)
371+
freshSpecs, logFiles, execErr := coord.ExecuteAndBuildSpecs(ctx, batchSpec, uncachedTasks, taskExecUI)
363372
// Add external changeset specs.
364373
importedSpecs, importErr := svc.CreateImportChangesetSpecs(ctx, batchSpec)
365374
var errs *multierror.Error

cmd/src/batch_exec.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ func executeBatchSpecInWorkspaces(ctx context.Context, ui *ui.JSONLines, opts ex
105105

106106
// Since we already know which workspace we want to execute the steps in,
107107
// we can convert it to a RepoWorkspace and build a task only for that one.
108-
repoWorkspace := convertWorkspace(input.Workspace)
108+
repoWorkspace := convertWorkspace(input)
109109

110110
var workspaceCreator workspace.Creator
111111

112-
if len(input.Workspace.Steps) > 0 {
112+
if len(input.Steps) > 0 {
113113
ui.PreparingContainerImages()
114114
images, err := svc.EnsureDockerImages(
115-
ctx, input.Workspace.Steps, opts.flags.parallelism,
115+
ctx, input.Steps, opts.flags.parallelism,
116116
ui.PreparingContainerImagesProgress,
117117
)
118118
if err != nil {
@@ -147,13 +147,13 @@ func executeBatchSpecInWorkspaces(ctx context.Context, ui *ui.JSONLines, opts ex
147147
// `src batch exec` uses server-side caching for changeset specs, so we
148148
// only need to call `CheckStepResultsCache` to make sure that per-step cache entries
149149
// are loaded and set on the tasks.
150-
tasks := svc.BuildTasks(ctx, input.Spec, []service.RepoWorkspace{repoWorkspace})
150+
tasks := svc.BuildTasks(ctx, &input.BatchChangeAttributes, []service.RepoWorkspace{repoWorkspace})
151151
if err := coord.CheckStepResultsCache(ctx, tasks); err != nil {
152152
return err
153153
}
154154

155155
taskExecUI := ui.ExecutingTasks(*verbose, opts.flags.parallelism)
156-
_, _, err = coord.Execute(ctx, tasks, input.Spec, taskExecUI)
156+
err = coord.Execute(ctx, tasks, taskExecUI)
157157
if err == nil || opts.flags.skipErrors {
158158
if err == nil {
159159
taskExecUI.Success()
@@ -191,7 +191,7 @@ func loadWorkspaceExecutionInput(file string) (batcheslib.WorkspacesExecutionInp
191191
return input, nil
192192
}
193193

194-
func convertWorkspace(w batcheslib.Workspace) service.RepoWorkspace {
194+
func convertWorkspace(w batcheslib.WorkspacesExecutionInput) service.RepoWorkspace {
195195
fileMatches := make(map[string]bool)
196196
for _, path := range w.SearchResultPaths {
197197
fileMatches[path] = true

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
2222
github.com/sourcegraph/go-diff v0.6.1
2323
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf
24-
github.com/sourcegraph/sourcegraph/lib v0.0.0-20220122003450-8a45ff2c8826
24+
github.com/sourcegraph/sourcegraph/lib v0.0.0-20220204151625-fceb046d5b75
2525
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
2626
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
2727
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f
@@ -39,7 +39,7 @@ require (
3939
github.com/gogo/protobuf v1.3.2 // indirect
4040
github.com/hashicorp/errwrap v1.1.0 // indirect
4141
github.com/inconshreveable/log15 v0.0.0-20201112154412-8562bdadbbac // indirect
42-
github.com/klauspost/compress v1.13.6 // indirect
42+
github.com/klauspost/compress v1.14.2 // indirect
4343
github.com/klauspost/pgzip v1.2.5 // indirect
4444
github.com/kr/pretty v0.3.0 // indirect
4545
github.com/kr/text v0.2.0 // indirect
@@ -56,7 +56,7 @@ require (
5656
github.com/stretchr/testify v1.7.0 // indirect
5757
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
5858
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
59-
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 // indirect
59+
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 // indirect
6060
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
6161
gopkg.in/yaml.v2 v2.4.0 // indirect
6262
)

go.sum

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY
1414
github.com/alecthomas/kingpin v2.2.6+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE=
1515
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
1616
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
17-
github.com/alecthomas/units v0.0.0-20210927113745-59d0afb8317a/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
17+
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
1818
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
1919
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
2020
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
@@ -169,8 +169,8 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
169169
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
170170
github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
171171
github.com/klauspost/compress v1.9.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
172-
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
173-
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
172+
github.com/klauspost/compress v1.14.2 h1:S0OHlFk/Gbon/yauFJ4FfJJF5V0fc5HbBTJazi28pRw=
173+
github.com/klauspost/compress v1.14.2/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
174174
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
175175
github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE=
176176
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
@@ -268,8 +268,8 @@ github.com/sourcegraph/go-diff v0.6.1 h1:hmA1LzxW0n1c3Q4YbrFgg4P99GSnebYa3x8gr0H
268268
github.com/sourcegraph/go-diff v0.6.1/go.mod h1:iBszgVvyxdc8SFZ7gm69go2KDdt3ag071iBaWPF6cjs=
269269
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf h1:oAdWFqhStsWiiMP/vkkHiMXqFXzl1XfUNOdxKJbd6bI=
270270
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf/go.mod h1:ppFaPm6kpcHnZGqQTFhUIAQRIEhdQDWP1PCv4/ON354=
271-
github.com/sourcegraph/sourcegraph/lib v0.0.0-20220122003450-8a45ff2c8826 h1:8AoxPHU5kpcAx1frcU1xHpLAjc2UZTYxD82BMOUp50Y=
272-
github.com/sourcegraph/sourcegraph/lib v0.0.0-20220122003450-8a45ff2c8826/go.mod h1:KhG6S/6YtG9eFdcXlfSD4ogB4qR8V6wLTLQd7x3ToAM=
271+
github.com/sourcegraph/sourcegraph/lib v0.0.0-20220204151625-fceb046d5b75 h1:XCvY1beVBLRnJHgyQ9T9AexDlUMfQwez3yGAX59yBZ4=
272+
github.com/sourcegraph/sourcegraph/lib v0.0.0-20220204151625-fceb046d5b75/go.mod h1:u3KHLBGdpCN8bgI8PxPafb8vh3e+ftHkHZdjaXXIpFA=
273273
github.com/sourcegraph/yaml v1.0.1-0.20200714132230-56936252f152 h1:z/MpntplPaW6QW95pzcAR/72Z5TWDyDnSo0EOcyij9o=
274274
github.com/sourcegraph/yaml v1.0.1-0.20200714132230-56936252f152/go.mod h1:GIjDIg/heH5DOkXY3YJ/wNhfHsQHoXGjl8G8amsYQ1I=
275275
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
@@ -385,9 +385,9 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc
385385
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
386386
golang.org/x/sys v0.0.0-20211102192858-4dd72447c267/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
387387
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
388-
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
389-
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1chK4VfFLhRsDK7MhqGRY=
390-
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
388+
golang.org/x/sys v0.0.0-20211213223007-03aa0b5f6827/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
389+
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 h1:XDXtA5hveEEV8JB2l7nhMTp3t3cHp9ZpwcdjqyEWLlo=
390+
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
391391
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
392392
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
393393
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
@@ -416,6 +416,7 @@ golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
416416
golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
417417
golang.org/x/tools v0.1.8-0.20211102182255-bb4add04ddef/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
418418
golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
419+
golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
419420
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
420421
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
421422
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
@@ -474,3 +475,4 @@ jaytaylor.com/html2text v0.0.0-20200412013138-3577fbdbcff7/go.mod h1:OxvTsCwKosq
474475
mvdan.cc/gofumpt v0.0.0-20210107193838-d24d34e18d44/go.mod h1:yXG1r1WqZVKWbVRtBWKWX9+CxGYfA51nSomhM0woR48=
475476
mvdan.cc/gofumpt v0.1.0/go.mod h1:yXG1r1WqZVKWbVRtBWKWX9+CxGYfA51nSomhM0woR48=
476477
mvdan.cc/gofumpt v0.2.0/go.mod h1:TiGmrf914DAuT6+hDIxOqoDb4QXIzAuEUSXqEf9hGKY=
478+
mvdan.cc/gofumpt v0.2.1/go.mod h1:a/rvZPhsNaedOJBzqRD9omnwVwHZsBdJirXHa9Gh9Ig=

internal/batches/executor/coordinator.go

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/cockroachdb/errors"
9-
"github.com/hashicorp/go-multierror"
109

1110
batcheslib "github.com/sourcegraph/sourcegraph/lib/batches"
1211
"github.com/sourcegraph/sourcegraph/lib/batches/execution"
@@ -86,9 +85,9 @@ func NewCoordinator(opts NewCoordinatorOpts) *Coordinator {
8685
// CheckCache checks whether the internal ExecutionCache contains
8786
// ChangesetSpecs for the given Tasks. If cached ChangesetSpecs exist, those
8887
// are returned, otherwise the Task, to be executed later.
89-
func (c *Coordinator) CheckCache(ctx context.Context, tasks []*Task) (uncached []*Task, specs []*batcheslib.ChangesetSpec, err error) {
88+
func (c *Coordinator) CheckCache(ctx context.Context, batchSpec *batcheslib.BatchSpec, tasks []*Task) (uncached []*Task, specs []*batcheslib.ChangesetSpec, err error) {
9089
for _, t := range tasks {
91-
cachedSpecs, found, err := c.checkCacheForTask(ctx, t)
90+
cachedSpecs, found, err := c.checkCacheForTask(ctx, batchSpec, t)
9291
if err != nil {
9392
return nil, nil, err
9493
}
@@ -134,7 +133,7 @@ func (c *Coordinator) ClearCache(ctx context.Context, tasks []*Task) error {
134133
return nil
135134
}
136135

137-
func (c *Coordinator) checkCacheForTask(ctx context.Context, task *Task) (specs []*batcheslib.ChangesetSpec, found bool, err error) {
136+
func (c *Coordinator) checkCacheForTask(ctx context.Context, batchSpec *batcheslib.BatchSpec, task *Task) (specs []*batcheslib.ChangesetSpec, found bool, err error) {
138137
globalEnv := os.Environ()
139138

140139
// Check if the task is cached.
@@ -164,15 +163,15 @@ func (c *Coordinator) checkCacheForTask(ctx context.Context, task *Task) (specs
164163
return specs, true, nil
165164
}
166165

167-
specs, err = c.buildChangesetSpecs(task, result)
166+
specs, err = c.buildChangesetSpecs(task, batchSpec, result)
168167
if err != nil {
169168
return specs, false, err
170169
}
171170

172171
return specs, true, nil
173172
}
174173

175-
func (c Coordinator) buildChangesetSpecs(task *Task, result execution.Result) ([]*batcheslib.ChangesetSpec, error) {
174+
func (c Coordinator) buildChangesetSpecs(task *Task, batchSpec *batcheslib.BatchSpec, result execution.Result) ([]*batcheslib.ChangesetSpec, error) {
176175
input := &batcheslib.ChangesetSpecInput{
177176
Repository: batcheslib.Repository{
178177
ID: task.Repository.ID,
@@ -182,8 +181,8 @@ func (c Coordinator) buildChangesetSpecs(task *Task, result execution.Result) ([
182181
BaseRev: task.Repository.Rev(),
183182
},
184183
BatchChangeAttributes: task.BatchChangeAttributes,
185-
Template: task.Template,
186-
TransformChanges: task.TransformChanges,
184+
Template: batchSpec.ChangesetTemplate,
185+
TransformChanges: batchSpec.TransformChanges,
187186

188187
Result: execution.Result{
189188
Diff: result.Diff,
@@ -222,30 +221,36 @@ func (c *Coordinator) loadCachedStepResults(ctx context.Context, task *Task, glo
222221
return nil
223222
}
224223

225-
func (c *Coordinator) cacheAndBuildSpec(ctx context.Context, taskResult taskResult, ui TaskExecutionUI) ([]*batcheslib.ChangesetSpec, error) {
224+
func (c *Coordinator) writeCache(ctx context.Context, taskResult taskResult, ui TaskExecutionUI) error {
226225
// Add to the cache, even if no diff was produced.
227226
globalEnv := os.Environ()
228227
cacheKey := taskResult.task.cacheKey(globalEnv)
229228
if err := c.cache.Set(ctx, cacheKey, taskResult.result); err != nil {
230-
return nil, errors.Wrapf(err, "caching result for %q", taskResult.task.Repository.Name)
229+
return errors.Wrapf(err, "caching result for %q", taskResult.task.Repository.Name)
231230
}
232231

233232
// Save the per-step results
234233
for _, stepResult := range taskResult.stepResults {
235234
key := cacheKeyForStep(cacheKey, stepResult.StepIndex)
236235
if err := c.cache.SetStepResult(ctx, key, stepResult); err != nil {
237-
return nil, errors.Wrapf(err, "caching result for step %d in %q", stepResult.StepIndex, taskResult.task.Repository.Name)
236+
return errors.Wrapf(err, "caching result for step %d in %q", stepResult.StepIndex, taskResult.task.Repository.Name)
238237
}
239238
}
240239

240+
return nil
241+
}
242+
243+
func (c *Coordinator) writeCacheAndBuildSpecs(ctx context.Context, batchSpec *batcheslib.BatchSpec, taskResult taskResult, ui TaskExecutionUI) ([]*batcheslib.ChangesetSpec, error) {
244+
c.writeCache(ctx, taskResult, ui)
245+
241246
// If the steps didn't result in any diff, we don't need to create a
242247
// changeset spec that's displayed to the user and send to the server.
243248
if taskResult.result.Diff == "" {
244249
return nil, nil
245250
}
246251

247252
// Build the changeset specs.
248-
specs, err := c.buildChangesetSpecs(taskResult.task, taskResult.result)
253+
specs, err := c.buildChangesetSpecs(taskResult.task, batchSpec, taskResult.result)
249254
if err != nil {
250255
return nil, err
251256
}
@@ -254,37 +259,44 @@ func (c *Coordinator) cacheAndBuildSpec(ctx context.Context, taskResult taskResu
254259
return specs, nil
255260
}
256261

257-
// Execute executes the given Tasks and the importChangeset statements in the
258-
// given spec. It regularly calls the executionProgressPrinter with the
259-
// current TaskStatuses.
260-
func (c *Coordinator) Execute(ctx context.Context, tasks []*Task, spec *batcheslib.BatchSpec, ui TaskExecutionUI) ([]*batcheslib.ChangesetSpec, []string, error) {
261-
var (
262-
specs []*batcheslib.ChangesetSpec
263-
errs *multierror.Error
264-
)
265-
266-
ui.Start(tasks)
262+
// Execute executes the given tasks. It calls the ui on updates.
263+
func (c *Coordinator) Execute(ctx context.Context, tasks []*Task, ui TaskExecutionUI) error {
264+
results, err := c.doExecute(ctx, tasks, ui)
267265

268-
// Run executor
269-
c.exec.Start(ctx, tasks, ui)
270-
results, err := c.exec.Wait(ctx)
271-
if err != nil {
272-
if c.opts.SkipErrors {
273-
errs = multierror.Append(errs, err)
274-
} else {
275-
return nil, nil, err
266+
// Write results to cache.
267+
for _, taskResult := range results {
268+
if cacheErr := c.writeCache(ctx, taskResult, ui); cacheErr != nil {
269+
return cacheErr
276270
}
277271
}
278272

273+
return err
274+
}
275+
276+
// ExecuteAndBuildSpecs executes the given tasks and builds changeset specs for the results.
277+
// It calls the ui on updates.
278+
func (c *Coordinator) ExecuteAndBuildSpecs(ctx context.Context, batchSpec *batcheslib.BatchSpec, tasks []*Task, ui TaskExecutionUI) ([]*batcheslib.ChangesetSpec, []string, error) {
279+
results, errs := c.doExecute(ctx, tasks, ui)
280+
281+
var specs []*batcheslib.ChangesetSpec
282+
279283
// Write results to cache, build ChangesetSpecs if possible and add to list.
280284
for _, taskResult := range results {
281-
taskSpecs, err := c.cacheAndBuildSpec(ctx, taskResult, ui)
285+
taskSpecs, err := c.writeCacheAndBuildSpecs(ctx, batchSpec, taskResult, ui)
282286
if err != nil {
283287
return nil, nil, err
284288
}
285289

286290
specs = append(specs, taskSpecs...)
287291
}
288292

289-
return specs, c.logManager.LogFiles(), errs.ErrorOrNil()
293+
return specs, c.logManager.LogFiles(), errs
294+
}
295+
296+
func (c *Coordinator) doExecute(ctx context.Context, tasks []*Task, ui TaskExecutionUI) (results []taskResult, err error) {
297+
ui.Start(tasks)
298+
299+
// Run executor
300+
c.exec.Start(ctx, tasks, ui)
301+
return c.exec.Wait(ctx)
290302
}

0 commit comments

Comments
 (0)