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