public void TaskFinished(SnTaskResult taskResult) { SnTrace.TaskManagement.Write("AgentHub TaskFinished called. Agent: {0} / {1}, taskId: {2}, code: {3}, error: {4}", taskResult.MachineName, taskResult.AgentName, taskResult.Task.Id, taskResult.ResultCode, taskResult.Error == null ? "" : taskResult.Error.Message); try { if (string.IsNullOrEmpty(taskResult.Task.AppId)) { SnLog.WriteWarning($"AppId is empty for task #{taskResult.Task.Id}.", EventId.TaskManagement.Lifecycle); return; } var doesApplicationNeedNotification = !string.IsNullOrWhiteSpace(taskResult.Task.GetFinalizeUrl()); // first we make sure that the app is accessible by sending a ping request if (doesApplicationNeedNotification && !ApplicationHandler.SendPingRequest(taskResult.Task.AppId)) { var app = ApplicationHandler.GetApplication(taskResult.Task.AppId); SnLog.WriteError(string.Format("Ping request to application {0} ({1}) failed when finalizing task #{2}. Task success: {3}, error: {4}", taskResult.Task.AppId, app == null ? "unknown app" : app.ApplicationUrl, taskResult.Task.Id, taskResult.Successful, taskResult.Error == null ? "-" : taskResult.Error.ToString()), EventId.TaskManagement.Communication); doesApplicationNeedNotification = false; } // remove the task from the database first TaskDataHandler.FinalizeTask(taskResult); SnTrace.TaskManagement.Write("AgentHub TaskFinished: task {0} has been deleted.", taskResult.Task.Id); if (doesApplicationNeedNotification) { // This method does not need to be awaited, because we do not want to do anything // with the result, only notify the app that the task has been finished. ApplicationHandler.SendFinalizeNotificationAsync(taskResult); } // notify monitors TaskMonitorHub.OnTaskEvent(taskResult.Successful ? SnTaskEvent.CreateDoneEvent(taskResult.Task.Id, taskResult.Task.Title, taskResult.ResultData, taskResult.Task.AppId, taskResult.Task.Tag, taskResult.MachineName, taskResult.AgentName) : SnTaskEvent.CreateFailedEvent(taskResult.Task.Id, taskResult.Task.Title, taskResult.ResultData, taskResult.Task.AppId, taskResult.Task.Tag, taskResult.MachineName, taskResult.AgentName)); } catch (Exception ex) { SnLog.WriteException(ex, "AgentHub TaskFinished failed.", EventId.TaskManagement.General); } }
public async Task TaskFinished(SnTaskResult taskResult) { SnTrace.TaskManagement.Write("AgentHub TaskFinished called. Agent: {0} / {1}, taskId: {2}, code: {3}, error: {4}", taskResult.MachineName, taskResult.AgentName, taskResult.Task.Id, taskResult.ResultCode, taskResult.Error == null ? "" : taskResult.Error.Message); try { if (string.IsNullOrEmpty(taskResult.Task.AppId)) { SnLog.WriteWarning($"AppId is empty for task #{taskResult.Task.Id}.", EventId.TaskManagement.Lifecycle); return; } var app = _applicationHandler.GetApplication(taskResult.Task.AppId); var doesApplicationNeedNotification = !string.IsNullOrWhiteSpace(taskResult.Task.GetFinalizeUrl(app)); // first we make sure that the app is accessible by sending a ping request if (doesApplicationNeedNotification && !(await _applicationHandler.SendPingRequestAsync(taskResult.Task.AppId, Context.ConnectionAborted) .ConfigureAwait(false))) { SnLog.WriteError($"Ping request to application {taskResult.Task.AppId} " + $"({(app == null ? "unknown app" : app.ApplicationUrl)}) " + $"failed when finalizing task #{taskResult.Task.Id}. " + $"Task success: {taskResult.Successful}, " + $"error: {(taskResult.Error == null ? "-" : taskResult.Error.ToString())}", EventId.TaskManagement.Communication); doesApplicationNeedNotification = false; } // remove the task from the database first await _dataHandler.FinalizeTaskAsync(taskResult, Context.ConnectionAborted).ConfigureAwait(false); SnTrace.TaskManagement.Write("AgentHub TaskFinished: task {0} has been deleted.", taskResult.Task.Id); if (doesApplicationNeedNotification) { // This method does not need to be awaited, because we do not want to do anything // with the result, only notify the app that the task has been finished. #pragma warning disable 4014 _applicationHandler.SendFinalizeNotificationAsync(taskResult, CancellationToken.None); #pragma warning restore 4014 } // notify monitors var te = taskResult.Successful ? SnTaskEvent.CreateDoneEvent(taskResult.Task.Id, taskResult.Task.Title, taskResult.ResultData, taskResult.Task.AppId, taskResult.Task.Tag, taskResult.MachineName, taskResult.AgentName) : SnTaskEvent.CreateFailedEvent(taskResult.Task.Id, taskResult.Task.Title, taskResult.ResultData, taskResult.Task.AppId, taskResult.Task.Tag, taskResult.MachineName, taskResult.AgentName); await _monitorHub.OnTaskEvent(te).ConfigureAwait(false); } catch (Exception ex) { SnLog.WriteException(ex, "AgentHub TaskFinished failed.", EventId.TaskManagement.General); } }