Exemplo n.º 1
0
// 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 = UpdateAllTasks(
			bson.M{
				TaskBuildIdKey: buildId,
				TaskStatusKey:  evergreen.TaskUndispatched,
			},
			bson.M{"$set": bson.M{TaskActivatedKey: active, TaskActivatedByKey: 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 build.IsSystemActivator(caller) {
			_, err = UpdateAllTasks(
				bson.M{
					TaskBuildIdKey:     buildId,
					TaskStatusKey:      evergreen.TaskUndispatched,
					TaskActivatedByKey: caller,
				},
				bson.M{"$set": bson.M{TaskActivatedKey: active, TaskActivatedByKey: caller}},
			)

		} else {
			// update all tasks if the caller is not evergreen.
			_, err = UpdateAllTasks(
				bson.M{
					TaskBuildIdKey: buildId,
					TaskStatusKey:  evergreen.TaskUndispatched,
				},
				bson.M{"$set": bson.M{TaskActivatedKey: active, TaskActivatedByKey: caller}},
			)
		}
	}

	if err != nil {
		return err
	}
	if err = build.UpdateActivation(buildId, active, caller); err != nil {
		return err
	}
	return RefreshTasksCache(buildId)
}
Exemplo n.º 2
0
func SetTaskActivated(taskId string, caller string, active bool) error {
	task, err := FindTask(taskId)
	if err != nil {
		return err
	}

	if active {

		// if the task is being activated, make sure to activate all of the task's
		// dependencies as well
		for _, dep := range task.DependsOn {
			if err = SetTaskActivated(dep.TaskId, caller, true); err != nil {
				return fmt.Errorf("error activating dependency for %v with id %v: %v",
					taskId, dep.TaskId, err)
			}
		}

		if task.DispatchTime != ZeroTime && task.Status == evergreen.TaskUndispatched {
			err = task.reset()
		} else {
			err = UpdateOneTask(
				bson.M{
					TaskIdKey: taskId,
				},
				bson.M{
					"$set": bson.M{
						TaskActivatedKey:   active,
						TaskActivatedByKey: caller,
					},
					"$unset": bson.M{
						TaskMinQueuePosKey: "",
					},
				},
			)
		}
		// if the caller is not evergreen or the the task is activated by evergreen, deactivate it
	} else if !build.IsSystemActivator(caller) || build.IsSystemActivator(task.ActivatedBy) {
		err = UpdateOneTask(
			bson.M{
				TaskIdKey: taskId,
			},
			bson.M{
				"$set": bson.M{
					TaskActivatedKey:     active,
					TaskScheduledTimeKey: ZeroTime,
				},
				"$unset": bson.M{
					TaskMinQueuePosKey: "",
				},
			},
		)
	} else {
		return nil
	}

	if err != nil {
		return err
	}

	if active {
		event.LogTaskActivated(taskId, caller)
	} else {
		event.LogTaskDeactivated(taskId, caller)
	}

	// update the cached version of the task, in its build document
	return build.SetCachedTaskActivated(task.BuildId, taskId, active)
}