private void ProcessNotExecutedTask(IReportTaskRunContext taskContext, List <Tuple <Exception, string> > exceptions, Exception e) { var oper = taskContext.OpersToExecute.First(); exceptions.Add(new Tuple <Exception, string>(e, oper.Properties.Name)); var msg = $"Task {taskContext.TaskId} was not executed (" + e.Message + ")"; SendServiceInfo(msg); taskContext.DefaultExporter.SendError(exceptions, taskContext.TaskName); taskContext.TaskInstance.Duration = 0; taskContext.TaskInstance.State = (int)InstanceState.Failed; var dtoOperInstance = new DtoOperInstance { TaskInstanceId = taskContext.TaskInstance.Id, OperationId = oper.Properties.Id, StartTime = DateTime.Now, Duration = 0, ErrorMessage = e.Message, State = (int)InstanceState.Failed }; dtoOperInstance.Id = repository.CreateEntity(dtoOperInstance); repository.UpdateEntity(taskContext.TaskInstance); }
private string GetOperationStateFromInstance(string operName, DtoOperInstance instance) { var state = (InstanceState)instance.State; return(operName + $" (State: {state.ToString()}," + $" started: {instance.StartTime}" + (instance.Duration > 0 ? $", duration: {instance.Duration / 60000} m {(instance.Duration % 60000) / 1000.0:f0} s" : "") + ")"); }
private void RunOperation(IReportTaskRunContext taskContext, IOperation oper, DtoTaskInstance dtoTaskInstance, List <Tuple <Exception, string> > exceptions) { { var dtoOperInstance = new DtoOperInstance { TaskInstanceId = dtoTaskInstance.Id, OperationId = oper.Properties.Id, StartTime = DateTime.Now, Duration = 0, State = (int)InstanceState.InProcess }; dtoOperInstance.Id = repository.CreateEntity(dtoOperInstance); taskContext.PackageStates[oper.Properties.Number - 1] = GetOperationStateFromInstance(oper.Properties.Name, dtoOperInstance); Stopwatch operDuration = new Stopwatch(); operDuration.Start(); try { Task.Run(async() => await oper .ExecuteAsync(taskContext)).Wait(taskContext.CancelSource.Token); if (oper.Properties.NeedSavePackage) { dtoOperInstance.DataSet = taskContext.GetCompressedPackage(oper.Properties.PackageName); } dtoOperInstance.State = (int)InstanceState.Success; operDuration.Stop(); dtoOperInstance.Duration = Convert.ToInt32(operDuration.ElapsedMilliseconds); repository.UpdateEntity(dtoOperInstance); } catch (Exception e) { if (e is OperationCanceledException) { dtoOperInstance.State = (int)InstanceState.Canceled; } else { if (e.InnerException == null) { exceptions.Add(new Tuple <Exception, string>(e, oper.Properties.Name)); dtoOperInstance.ErrorMessage = e.Message; } else { var allExceptions = e.FromHierarchy(ex => ex.InnerException).ToList(); exceptions.AddRange(allExceptions .Select(exx => new Tuple <Exception, string>(exx, oper.Properties.Name))); dtoOperInstance.ErrorMessage = string.Join("\n", allExceptions.Select(exx => exx.Message)); } dtoOperInstance.State = (int)InstanceState.Failed; } operDuration.Stop(); dtoOperInstance.Duration = Convert.ToInt32(operDuration.ElapsedMilliseconds); repository.UpdateEntity(dtoOperInstance); } finally { taskContext.PackageStates[oper.Properties.Number - 1] = GetOperationStateFromInstance(oper.Properties.Name, dtoOperInstance); } } }
public void RunOperations(IReportTaskRunContext taskContext) { Stopwatch duration = new Stopwatch(); duration.Start(); bool deleteFolder = false; if (taskContext.OpersToExecute.Any(oper => oper is SshImporter))//todo:not sshimporter but needsfolder { deleteFolder = true; taskContext.CreateDataFolder(); } taskContext.PackageStates = taskContext.OpersToExecute .Select(oper => oper.Properties.Name + " (Not started) ").ToList(); var dtoTaskInstance = taskContext.TaskInstance; var success = true; var exceptions = new List <Tuple <Exception, string> >(); try { foreach (var oper in taskContext.OpersToExecute) { if (taskContext.CancelSource.IsCancellationRequested) { taskContext.CancelSource.Dispose(); success = false; break; } var dtoOperInstance = new DtoOperInstance { TaskInstanceId = dtoTaskInstance.Id, OperationId = oper.Properties.Id, StartTime = DateTime.Now, Duration = 0, State = (int)InstanceState.InProcess }; dtoOperInstance.Id = repository.CreateEntity(dtoOperInstance); taskContext.PackageStates[oper.Properties.Number - 1] = GetOperationStateFromInstance(oper.Properties.Name, dtoOperInstance); Stopwatch operDuration = new Stopwatch(); operDuration.Start(); try { Task.Run(async() => await oper .ExecuteAsync(taskContext)).Wait(taskContext.CancelSource.Token); if (oper.Properties.NeedSavePackage) { dtoOperInstance.DataSet = taskContext.GetCompressedPackage(oper.Properties.PackageName); } dtoOperInstance.State = (int)InstanceState.Success; operDuration.Stop(); dtoOperInstance.Duration = Convert.ToInt32(operDuration.ElapsedMilliseconds); repository.UpdateEntity(dtoOperInstance); } catch (Exception e) { if (e is OperationCanceledException) { dtoOperInstance.State = (int)InstanceState.Canceled; } else { if (e.InnerException == null) { exceptions.Add(new Tuple <Exception, string>(e, oper.Properties.Name)); dtoOperInstance.ErrorMessage = e.Message; } else { var allExceptions = e.FromHierarchy(ex => ex.InnerException).ToList(); exceptions.AddRange(allExceptions .Select(exx => new Tuple <Exception, string>(exx, oper.Properties.Name))); dtoOperInstance.ErrorMessage = string.Join("\n", allExceptions.Select(exx => exx.Message)); } dtoOperInstance.State = (int)InstanceState.Failed; } operDuration.Stop(); dtoOperInstance.Duration = Convert.ToInt32(operDuration.ElapsedMilliseconds); repository.UpdateEntity(dtoOperInstance); } finally { taskContext.PackageStates[oper.Properties.Number - 1] = GetOperationStateFromInstance(oper.Properties.Name, dtoOperInstance); } } if (exceptions.Count == 0 || dtoTaskInstance.State == (int)InstanceState.Canceled) { var msg = dtoTaskInstance.State == (int)InstanceState.Canceled ? $"Task {taskContext.TaskId} stopped" : $"Task {taskContext.TaskId} completed successfully"; monik.ApplicationInfo(msg); Console.WriteLine(msg); } else { success = false; taskContext.Exporter.SendError(exceptions, taskContext.TaskName); var msg = $"Task {taskContext.TaskId} completed with errors"; monik.ApplicationInfo(msg); Console.WriteLine(msg); } } catch (Exception e) { success = false; taskContext.Exporter.SendError(exceptions, taskContext.TaskName); var msg = $"Task {taskContext.TaskId} is not completed. An error has occurred: {e.Message}"; monik.ApplicationError(msg); Console.WriteLine(msg); } duration.Stop(); if (deleteFolder) { taskContext.RemoveDataFolder(); } dtoTaskInstance.Duration = Convert.ToInt32(duration.ElapsedMilliseconds); dtoTaskInstance.State = success ? (int)InstanceState.Success : dtoTaskInstance.State == (int)InstanceState.Canceled ? (int)InstanceState.Canceled : (int)InstanceState.Failed; repository.UpdateEntity(dtoTaskInstance); }