public void SendTaskResult(string taskId, OctaneTaskResult octaneTaskResult) { var baseUri = $"{INTERNAL_API}{_connectionDetails.SharedSpace}{ANALYTICS_CI_SERVERS}{_connectionDetails.InstanceId}/tasks/{taskId}/result"; ResponseWrapper res = _restConnector.ExecutePut(baseUri, null, octaneTaskResult.ToString(), RequestConfiguration.Create().SetTimeout(DEFAULT_TIMEOUT)); ValidateExpectedStatusCode(res, HttpStatusCode.NoContent); }
private void ExecuteTask(Uri taskUrl, OctaneTaskResult taskResult) { var start = DateTime.Now; var taskType = ParseTaskUriToTaskType(taskUrl); switch (taskType) { case TaskType.GetJobsList: taskResult.Body = JsonHelper.SerializeObject(_tfsApis.GetJobsList()); break; case TaskType.GetJobDetail: var jobId = taskUrl.Segments[taskUrl.Segments.Length - 1]; taskResult.Body = JsonHelper.SerializeObject(_tfsApis.GetJobDetail(jobId)); break; case TaskType.ExecutePipelineRunRequest: var joinedProjectName = taskUrl.Segments[taskUrl.Segments.Length - 2].Trim('/'); var buildParts = joinedProjectName.Split('.'); QueueNewBuild(taskResult, buildParts[0], buildParts[1], buildParts[2]); break; case TaskType.Undefined: Log.Debug($"Undefined task : {taskUrl}"); break; default: break; } var end = DateTime.Now; Log.Debug($"Task {taskType} executed in {(long)((end - start).TotalMilliseconds)} ms"); }
private void QueueNewBuild(OctaneTaskResult taskResult, string collectionName, string projectId, string buildDefinitionId) { try { _tfsApis.QueueNewBuild(collectionName, projectId, buildDefinitionId); taskResult.Body = "Job started"; } catch (Exception e) { Log.Error("Failed to QueueNewBuild :" + e.Message, e); if (e is UnauthorizedAccessException) { taskResult.Status = 403; //qc:pipeline-management-run-pipeline-failed-no-permission taskResult.Body = "No permissions"; } else if (e.Message.Contains("BuildRequestValidationFailedException") && e.Message.Contains("Could not queue the build")) { taskResult.Status = 503; //qc:pipeline-management-ci-is-down taskResult.Body = "Agent is down"; } else { taskResult.Status = 500; taskResult.Body = "Failed to queue job"; } } }
private void HandleTasks(string taskData) { Log.Info($"Received tasks : {taskData}"); LogPolling.Debug($"Received tasks : {taskData}"); OctaneTaskResult taskResult = null; OctaneTask octaneTask = null; try { //process task var octaneTasks = JsonHelper.DeserializeObject <List <OctaneTask> >(taskData); if (octaneTasks == null || octaneTasks.Count == 0) { Log.Error("Received data does not contains any valid task."); return; } octaneTask = octaneTasks[0]; int status = HttpMethodEnum.POST.Equals(octaneTask.Method) ? 201 : 200; taskResult = new OctaneTaskResult(status, octaneTask.Id, ""); ExecuteTask(octaneTask?.ResultUrl, taskResult); Log.Debug($"Sending result to octane : {taskResult.Status} - {taskResult.Body}"); } catch (Exception ex) { if (octaneTask != null) { taskResult.Status = 500; taskResult.Body = "failed to process"; } ExceptionHelper.HandleExceptionAndRestartIfRequired(ex, Log, "HandleTask"); } finally { if (taskResult != null) { _octaneApis.SendTaskResult(taskResult.Id.ToString(), taskResult); } } }