public void Finalize(SnTaskResult result) { if (result.Successful || result.Task == null || result.Task.Type.CompareTo("AsposePreviewGenerator") != 0 || string.IsNullOrEmpty(result.Task.TaskData)) { return; } try { var settings = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat }; var serializer = JsonSerializer.Create(settings); using (var jreader = new JsonTextReader(new StringReader(result.Task.TaskData))) { var previewData = serializer.Deserialize(jreader) as JObject; var contentId = previewData["Id"].Value <int>(); using (new SystemAccount()) { DocumentPreviewProvider.SetPreviewStatus(Node.Load <File>(contentId), PreviewStatus.Error); } } } catch (Exception ex) { Logger.WriteException(ex); } }
private static SnTaskResult ExecuteTask(SnTask t) { Console.WriteLine("Start work on task#" + t.Id); var result = new SnTaskResult { AgentName = AgentName, Task = t }; try { if (t.Type == "DoNotRunAnyExecutor") { using (var executor = new TestExecutor()) result.ResultCode = executor.Execute(t); } else { using (var executor = new OutProcExecutor()) result.ResultCode = executor.Execute(t); } } catch (Exception e) { result.Exception = e; } Console.WriteLine("Execution finished."); return(result); }
public virtual Task OnTaskFinishedAsync(SnTaskResult result, CancellationToken cancellationToken) { // the task was executed successfully without an error message if (result.Successful && result.Error == null) { return(Task.CompletedTask); } try { if (result.Error != null) { // log the error message and details for admins SnLog.WriteError("Task execution error, see the details below.", EventId.TaskManagement.General, properties: new Dictionary <string, object> { { "TaskType", result.Task.Type }, { "TaskData", result.Task.TaskData }, { "ErrorCode", result.Error.ErrorCode }, { "ErrorType", result.Error.ErrorType }, { "Message", result.Error.Message }, { "Details", result.Error.Details }, { "CallingContext", result.Error.CallingContext } }); } } catch (Exception ex) { SnLog.WriteException(ex); } return(Task.CompletedTask); }
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 virtual void Finalize(SnTaskResult result) { // not enough information if (result.Task == null || string.IsNullOrEmpty(result.Task.TaskData)) { return; } // the task was executed successfully without an error message if (result.Successful && result.Error == null) { return; } try { if (result.Error != null) { // log the error message and details for admins Logger.WriteError(ContentRepository.EventId.Preview.PreviewGenerationError, "Preview generation error, see the details below.", properties: new Dictionary <string, object> { { "ErrorCode", result.Error.ErrorCode }, { "ErrorType", result.Error.ErrorType }, { "Message", result.Error.Message }, { "Details", result.Error.Details } }); } // deserialize task data to retrieve content info var settings = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat }; var serializer = JsonSerializer.Create(settings); using (var jreader = new JsonTextReader(new IO.StringReader(result.Task.TaskData))) { var previewData = serializer.Deserialize(jreader) as JObject; var contentId = previewData["Id"].Value <int>(); using (new SystemAccount()) { DocumentPreviewProvider.SetPreviewStatus(Node.Load <File>(contentId), PreviewStatus.Error); } } } catch (Exception ex) { Logger.WriteException(ex); } }
private static void WriteFinishExecutionEvent(SnTaskResult taskResult) { var task = taskResult.Task; WriteEvent(null, taskResult.Successful ? TaskEventType.Done : TaskEventType.Failed, task.Id, null, task.Title, taskResult.Error == null ? (string)null : taskResult.Error.ToString(), task.AppId, taskResult.MachineName, taskResult.AgentName, task.Tag, null, null, null, null); }
public static void FinalizeTask(SnTaskResult taskResult) { SnTrace.TaskManagement.Write("TaskDataHandler FinalizeTask: " + (taskResult.Successful ? "Done" : "Error") + ", Id: " + taskResult.Task.Id); using (var cn = new SqlConnection(Configuration.ConnectionString)) using (var cm = new SqlCommand(DELETETASKSQL, cn) { CommandType = System.Data.CommandType.Text }) { cm.Parameters.Add("@Id", SqlDbType.Int).Value = taskResult.Task.Id; cn.Open(); cm.ExecuteNonQuery(); } WriteFinishExecutionEvent(taskResult); }
private Task WriteFinishExecutionEventAsync(SnTaskResult taskResult, CancellationToken cancellationToken) { var task = taskResult.Task; return(WriteEventAsync(null, taskResult.Successful ? TaskEventType.Done : TaskEventType.Failed, task.Id, null, task.Title, taskResult.Error?.ToString(), task.AppId, taskResult.MachineName, taskResult.AgentName, task.Tag, null, null, null, null, cancellationToken)); }
public async Task FinalizeTaskAsync(SnTaskResult taskResult, CancellationToken cancellationToken) { SnTrace.TaskManagement.Write("TaskDataHandler FinalizeTask: " + (taskResult.Successful ? "Done" : "Error") + ", Id: " + taskResult.Task.Id); await using (var cn = new SqlConnection(_connectionString)) { await using var cm = new SqlCommand(DELETETASKSQL, cn) { CommandType = CommandType.Text }; cm.Parameters.Add("@Id", SqlDbType.Int).Value = taskResult.Task.Id; await cn.OpenAsync(cancellationToken).ConfigureAwait(false); await cm.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); } await WriteFinishExecutionEventAsync(taskResult, cancellationToken).ConfigureAwait(false); }
public static void Ad2PortalSyncFinalizer(Content content, SnTaskResult result) { SnTaskManager.OnTaskFinished(result); // not enough information if (result.Task == null) { return; } try { if (!string.IsNullOrEmpty(result.ResultData)) { dynamic resultData = JsonConvert.DeserializeObject(result.ResultData); SnLog.WriteInformation("AD sync finished. See details below.", EventId.DirectoryServices, properties: new Dictionary <string, object> { { "SyncedObjects", resultData.SyncedObjects }, { "ObjectErrorCount", resultData.ObjectErrorCount }, { "ErrorCount", resultData.ErrorCount }, { "ElapsedTime", resultData.ElapsedTime } }); } else { SnLog.WriteWarning("AD sync finished with no results.", EventId.DirectoryServices); } } catch (Exception ex) { SnLog.WriteException(ex, "Error during AD sync finalizer.", EventId.DirectoryServices); } // the task was executed successfully without an error message if (result.Successful && result.Error == null) { return; } SnLog.WriteError("Error during AD sync. " + result.Error); }
private static SnTaskResult ExecuteTask(SnTask t) { Console.WriteLine("Start work on task#" + t.Id); var result = new SnTaskResult { MachineName = Environment.MachineName, AgentName = AgentName, Task = t }; try { if (t.Type == "DoNotRunAnyExecutor") { using var executor = new TestExecutor(); result.ResultCode = executor.Execute(t); } else { using var executor = new OutProcExecutor(AgentConfig); result.ResultCode = executor.Execute(t); } } catch (Exception e) { result.Error = SnTaskError.Create(e); } Console.WriteLine("Execution finished."); result.ResultData = _resultData; if (result.Error == null && _resultError != null) { result.Error = SnTaskError.Parse(_resultError); } _resultData = null; _resultError = null; return(result); }
private static Task SendResultAndDeleteTask(SnTaskResult result) { return(InvokeProxyAsync(Hub.TaskFinished, result)); }
private static void SendResultAndDeleteTask(SnTaskResult result) { InvokeProxy(Hub.TaskFinished, result); }
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); } }
private static void SendResult(SnTaskResult result) { //_hubProxy.Invoke(Hub.TaskFinished, result); InvokeProxy(Hub.TaskFinished, result); }