Esempio n. 1
0
// finishAndAwaitCleanup sends the returned TaskEndResponse and error
// for processing by the main agent loop.
func (agt *Agent) finishAndAwaitCleanup(status string) (*apimodels.TaskEndResponse, error) {

	// Signal all background actions to stop. If HandleSignals is still running,
	// this will cause it to return.
	close(agt.signalHandler.stopBackgroundChan)

	var detail *apimodels.TaskEndDetail
	select {
	case detail = <-agt.endChan:
	default:
		// endChan will be empty if the task completed without error
		detail = agt.getTaskEndDetail()
	}
	if status == evergreen.TaskSucceeded {
		detail.Status = evergreen.TaskSucceeded
		agt.logger.LogTask(slogger.INFO, "Task completed - SUCCESS.")
	} else {
		agt.logger.LogTask(slogger.INFO, "Task completed - FAILURE.")
	}

	// run post commands
	if agt.taskConfig.Project.Post != nil {
		agt.logger.LogTask(slogger.INFO, "Running post-task commands.")
		start := time.Now()
		err := agt.RunCommands(agt.taskConfig.Project.Post.List(), false, agt.callbackTimeoutSignal())
		if err != nil {
			agt.logger.LogExecution(slogger.ERROR, "Error running post-task command: %v", err)
		}
		agt.logger.LogTask(slogger.INFO, "Finished running post-task commands in %v.", time.Since(start).String())
	}

	t := agt.taskConfig.Project.FindProjectTask(agt.taskConfig.Task.DisplayName)
	if agt.taskConfig.Project.DisableCleanup || (t != nil && t.DisableCleanup) {
		agt.logger.LogExecution(slogger.INFO, "Skipping process cleanup.")
	} else {
		agt.logger.LogExecution(slogger.INFO, "Running process cleanup.")
		err := shell.KillSpawnedProcs(agt.taskConfig.Task.Id, agt.logger)
		if err != nil {
			agt.logger.LogExecution(slogger.ERROR, "Error cleaning up spawned processes: %v", err)
		}
	}
	err := agt.removeTaskDirectory()
	if err != nil {
		agt.logger.LogExecution(slogger.ERROR, "Error removing task directory: %v", err)
	}

	agt.logger.LogExecution(slogger.INFO, "Sending final status as: %v", detail.Status)
	ret, err := agt.End(detail)
	if ret != nil && !ret.RunNext {
		agt.logger.LogExecution(slogger.INFO, "No new tasks to run. Agent will shut down.")
	}
	agt.APILogger.FlushAndWait() // ensure we send any logs from End()
	return ret, err
}
Esempio n. 2
0
// finishAndAwaitCleanup sends the returned TaskEndResponse and error
// for processing by the main agent loop.
func (agt *Agent) finishAndAwaitCleanup(status string) (*apimodels.TaskEndResponse, error) {

	// Signal all background actions to stop. If HandleSignals is still running,
	// this will cause it to return.
	close(agt.signalHandler.stopBackgroundChan)

	var detail *apimodels.TaskEndDetail
	select {
	case detail = <-agt.endChan:
	default:
		// endChan will be empty if the task completed without error
		detail = agt.getTaskEndDetail()
	}
	if status == evergreen.TaskSucceeded {
		detail.Status = evergreen.TaskSucceeded
		agt.logger.LogTask(slogger.INFO, "Task completed - SUCCESS.")
	} else {
		agt.logger.LogTask(slogger.INFO, "Task completed - FAILURE.")
	}

	// run post commands
	if agt.taskConfig.Project.Post != nil {
		agt.logger.LogTask(slogger.INFO, "Running post-task commands.")
		start := time.Now()
		err := agt.RunCommands(agt.taskConfig.Project.Post.List(), false, agt.callbackTimeoutSignal())
		if err != nil {
			agt.logger.LogExecution(slogger.ERROR, "Error running post-task command: %v", err)
		}
		agt.logger.LogTask(slogger.INFO, "Finished running post-task commands in %v.", time.Since(start).String())
	}

	agt.logger.LogExecution(slogger.INFO, "Sending final status as: %v", detail.Status)
	ret, err := agt.End(detail)
	agt.APILogger.FlushAndWait() // ensure we send any logs from End()
	return ret, err
}