// SetBuildActivation updates the "active" state of this build and all associated tasks. // It also updates the task cache for the build document. func SetBuildActivation(buildId string, active bool, caller string) error { var err error // If activating a task, set the ActivatedBy field to be the caller if active { _, err = task.UpdateAll( bson.M{ task.BuildIdKey: buildId, task.StatusKey: evergreen.TaskUndispatched, }, bson.M{"$set": bson.M{task.ActivatedKey: active, task.ActivatedByKey: caller}}, ) } else { // if trying to deactivate a task then only deactivate tasks that have not been activated by a user. // if the caller is the default task activator, // only deactivate tasks that are activated by the default task activator if evergreen.IsSystemActivator(caller) { _, err = task.UpdateAll( bson.M{ task.BuildIdKey: buildId, task.StatusKey: evergreen.TaskUndispatched, task.ActivatedByKey: caller, }, bson.M{"$set": bson.M{task.ActivatedKey: active, task.ActivatedByKey: caller}}, ) } else { // update all tasks if the caller is not evergreen. _, err = task.UpdateAll( bson.M{ task.BuildIdKey: buildId, task.StatusKey: evergreen.TaskUndispatched, }, bson.M{"$set": bson.M{task.ActivatedKey: active, task.ActivatedByKey: caller}}, ) } } if err != nil { return err } if err = build.UpdateActivation(buildId, active, caller); err != nil { return err } return RefreshTasksCache(buildId) }
// AbortVersion sets the abort flag on all tasks associated with the version which are in an // abortable state func AbortVersion(versionId string) error { _, err := task.UpdateAll( bson.M{ task.VersionKey: versionId, task.StatusKey: bson.M{"$in": evergreen.AbortableStatuses}, }, bson.M{"$set": bson.M{task.AbortedKey: true}}, ) return err }
func SetVersionPriority(versionId string, priority int64) error { modifier := bson.M{task.PriorityKey: priority} //blacklisted - these tasks should never run, so unschedule now if priority < 0 { modifier[task.ActivatedKey] = false } _, err := task.UpdateAll( bson.M{task.VersionKey: versionId}, bson.M{"$set": modifier}, ) return err }
// RestartBuild restarts completed tasks associated with a given buildId. // If abortInProgress is true, it also sets the abort flag on any in-progress tasks. func RestartBuild(buildId string, taskIds []string, abortInProgress bool, caller string) error { // restart all the 'not in-progress' tasks for the build allTasks, err := task.Find(task.ByIdsBuildAndStatus(taskIds, buildId, task.CompletedStatuses)) if err != nil && err != mgo.ErrNotFound { return err } for _, t := range allTasks { if t.DispatchTime != util.ZeroTime { err = resetTask(t.Id) if err != nil { return fmt.Errorf("Restarting build %v failed, could not task.reset on task: %v", buildId, t.Id, err) } } } if abortInProgress { // abort in-progress tasks in this build _, err = task.UpdateAll( bson.M{ task.BuildIdKey: buildId, task.StatusKey: bson.M{ "$in": evergreen.AbortableStatuses, }, }, bson.M{ "$set": bson.M{ task.AbortedKey: true, }, }, ) if err != nil { return err } } return build.UpdateActivation(buildId, true, caller) }
// RestartVersion restarts completed tasks associated with a given versionId. // If abortInProgress is true, it also sets the abort flag on any in-progress tasks. func RestartVersion(versionId string, taskIds []string, abortInProgress bool, caller string) error { // restart all the 'not in-progress' tasks for the version allTasks, err := task.Find(task.ByDispatchedWithIdsVersionAndStatus(taskIds, versionId, task.CompletedStatuses)) if err != nil && err != mgo.ErrNotFound { return err } // archive all the tasks for _, t := range allTasks { if err := t.Archive(); err != nil { return fmt.Errorf("failed to archive task: %v", err) } } // Set all the task fields to indicate restarted err = task.ResetTasks(taskIds) if err != nil { return err } // TODO figure out a way to coalesce updates for task cache for the same build, so we // only need to do one update per-build instead of one per-task here. // Doesn't seem to be possible as-is because $ can only apply to one array element matched per // document. buildIdSet := map[string]bool{} for _, t := range allTasks { buildIdSet[t.BuildId] = true err = build.ResetCachedTask(t.BuildId, t.Id) if err != nil { return err } } // reset the build statuses, once per build buildIdList := make([]string, 0, len(buildIdSet)) for k, _ := range buildIdSet { buildIdList = append(buildIdList, k) } // Set the build status for all the builds containing the tasks that we touched _, err = build.UpdateAllBuilds( bson.M{build.IdKey: bson.M{"$in": buildIdList}}, bson.M{"$set": bson.M{build.StatusKey: evergreen.BuildStarted}}, ) if abortInProgress { // abort in-progress tasks in this build _, err = task.UpdateAll( bson.M{ task.VersionKey: versionId, task.IdKey: bson.M{"$in": taskIds}, task.StatusKey: bson.M{"$in": evergreen.AbortableStatuses}, }, bson.M{"$set": bson.M{task.AbortedKey: true}}, ) if err != nil { return err } } // update activation for all the builds for _, b := range buildIdList { err := build.UpdateActivation(b, true, caller) if err != nil { return err } } return nil }