Esempio n. 1
0
        protected override async Task ExecuteAsync(CancellationToken cancelToken)
        {
            monik.ApplicationWarning(stringVersion + " Started");
            Console.WriteLine(stringVersion + " Started");

            while (!cancelToken.IsCancellationRequested)
            {
                try
                {
                    await Task.Factory.StartNew(Method, cancelToken);
                }
                catch (TaskCanceledException _)
                {
                    monik.ApplicationWarning("Stopping...");
                }
                catch (AggregateException aggrExc)
                {
                    monik.ApplicationError("Aggregate exception at: " + stringVersion);
                    aggrExc.Handle((ex) =>
                    {
                        monik.ApplicationError($"Inner exception of type {ex.GetType().Name} has occured. Message: {ex.Message} Stacktrace: {ex.StackTrace}");
                        return(true);
                    });
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Error in core schedule loop: {ex}");
                }
                finally
                {
                    await Task.Delay(Period * 1000, cancelToken);
                }
            }
        }
Esempio n. 2
0
        private void CleanerTask()
        {
            try
            {
                // cleanup logs
                var logDeep = _settings.DayDeepLog;
                var logThreshold = _repository.GetLogThreshold(logDeep);
                if (logThreshold.HasValue)
                {
                    var count = _repository.CleanUpLog(logThreshold.Value);
                    _monik.LogicInfo("Cleaner delete Log: {0} rows", count);
                }

                // cleanup keep-alive
                var kaDeep = _settings.DayDeepKeepAlive;
                var kaThreshold = _repository.GetKeepAliveThreshold(kaDeep);
                if (kaThreshold.HasValue)
                {
                    var count = _repository.CleanUpKeepAlive(kaThreshold.Value);
                    _monik.LogicInfo("Cleaner delete KeepAlive: {0} rows", count);
                }
            }
            catch (Exception ex)
            {
                _monik.ApplicationError("CleanerTask: {0}", ex.Message);
            }
        }
Esempio n. 3
0
        public void Start()
        {
            monik.ApplicationWarning(stringVersion + " Started");
            Console.WriteLine(stringVersion + " Started");

            try
            {
                nancyHost.Start();
            }
            catch (Exception e)
            {
                monik.ApplicationError(e.Message);
                Console.WriteLine(e.Message);
            }
        }
Esempio n. 4
0
        private dynamic GetKeepAliveStatuses(KeepAliveRequest filter)
        {
            try
            {
                var kaResult = _cacheKeepAlive.GetKeepAlive2(filter);
                var result   = new List <KeepAliveStatus>();

                foreach (var ka in kaResult)
                {
                    var inst = _sourceInstanceCache.GetInstanceById(ka.InstanceID);

                    KeepAliveStatus status = new KeepAliveStatus()
                    {
                        SourceID     = inst.SourceID,
                        InstanceID   = inst.ID,
                        SourceName   = inst.SourceRef().Name,
                        InstanceName = inst.Name,
                        DisplayName  = inst.SourceRef().Name + "." + inst.Name,
                        Created      = ka.Created,
                        Received     = ka.Received,
                        StatusOK     = (DateTime.UtcNow - ka.Created).TotalSeconds < 180 // in seconds
                                                                                         // TODO: use param or default value for delta seconds
                    };

                    result.Add(status);
                }

                return(Response.AsJson(result.ToArray()));
            }
            catch (Exception ex)
            {
                _monik.ApplicationError($"Method /status : {ex.Message}");
                return(HttpStatusCode.InternalServerError);
            }
        }
Esempio n. 5
0
        } //ctor

        public async Task OpenPublicBoardCommandExecute(PublicBoardJson obj)
        {
            var selectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            FolderBrowserDialog dialog = new FolderBrowserDialog
            {
                Description         = "Select folder to save public board",
                ShowNewFolderButton = true,
                SelectedPath        = selectedPath
            };

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = dialog.SelectedPath + "\\" + obj.Name + ".kam";
                HttpResponseMessage resp;

                HttpClient hc = new HttpClient();
                try
                {
                    resp = await hc.GetAsync(obj.Url);
                }
                catch (Exception ex)
                {
                    mon.ApplicationError($"StartupViewModel.OpenPublicBoardCommandExecute network error: {ex.Message}");
                    await dialCoord.ShowMessageAsync(this, "Network Error", "Can not download file");

                    return;
                }

                try
                {
                    await ReadAsFileAsync(resp.Content, fileName, true);
                }
                catch (Exception ex)
                {
                    mon.ApplicationError($"StartupViewModel.OpenPublicBoardCommandExecute file save error: {ex.Message}");
                    await dialCoord.ShowMessageAsync(this, "Error", "Can not save file");

                    return;
                }

                if (await OpenBoardView(fileName))
                {
                    appConfig.UpdateRecent(fileName, false);
                }
            }
        }
Esempio n. 6
0
        private void ToMonik(TraceEventType eventType, string format, object[] args)
        {
            switch (eventType)
            {
            case TraceEventType.Critical:
                _monik.ApplicationFatal(format, args);
                break;

            case TraceEventType.Error:
                _monik.ApplicationError(format, args);
                break;

            case TraceEventType.Warning:
                _monik.ApplicationWarning(format, args);
                break;

            case TraceEventType.Information:
                _monik.ApplicationInfo(format, args);
                break;
            }
        }
Esempio n. 7
0
        private void UpdateTaskList()
        {
            try
            {
                var taskList = repository.GetListEntitiesByDtoType <DtoTask>();
                if (taskList == null)
                {
                    return;
                }
                lock (tasks)
                {
                    tasks.Clear();

                    foreach (var dtoTask in taskList)
                    {
                        var task = autofac.Resolve <IReportTask>(
                            new NamedParameter("id", dtoTask.Id),
                            new NamedParameter("name", dtoTask.Name),
                            new NamedParameter("parameters", dtoTask.Parameters),
                            new NamedParameter("dependsOn", dtoTask.DependsOn),
                            new NamedParameter("schedule", schedules
                                               .FirstOrDefault(s => s.Id == dtoTask.ScheduleId)),
                            new NamedParameter("opers", operations
                                               .Where(oper => oper.TaskId == dtoTask.Id)
                                               .Where(oper => !oper.IsDeleted).ToList()));

                        //todo: might be replaced with saved time from db
                        task.UpdateLastTime();
                        tasks.Add(task);
                    }
                } //lock
            }

            catch (Exception e)
            {
                var msg = $"Error while updating tasks: {e.Message}";
                monik.ApplicationError(msg);
                Console.WriteLine(msg);
            }
        } //taskresolver
Esempio n. 8
0
        protected override async Task ExecuteAsync(CancellationToken cancelToken)
        {
            monik.ApplicationWarning(stringVersion + " Started");
            Console.WriteLine(stringVersion + " Started");

            while (!cancelToken.IsCancellationRequested)
            {
                try
                {
                    await Task.Factory.StartNew(Method, cancelToken);

                    await Task.Delay(Period * 1000, cancelToken);
                }
                catch (TaskCanceledException _)
                {
                    monik.ApplicationWarning("Stopping...");
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Error in core schedule loop: {ex}");
                }
            }
        }
Esempio n. 9
0
        public void OnStart()
        {
            // Create context for ActiveQueues
            var context = new ActiveQueueContext
            {
                OnError = (errorMessage) =>
                {
                    _monik.ApplicationError(errorMessage);
                    Thread.Sleep(DelayOnException);
                },
                OnVerbose = (verboseMessage) =>
                {
                    _monik.ApplicationVerbose(verboseMessage);
                },
                OnMeasure = (metricName, value) =>
                {
                    _monik.Measure(metricName, AggregationType.Gauge, value);
                    _monik.Measure(metricName + "All", AggregationType.Accumulator, value);
                },
                OnReceivedMessage = (msg) =>
                {
                    _msgBuffer.Enqueue(msg);
                    _newMessageEvent.Set();
                },
                OnReceivedMessages = (messages) =>
                {
                    foreach (var msg in messages)
                    {
                        _msgBuffer.Enqueue(msg);
                    }
                    _newMessageEvent.Set();
                }
            };

            if (_queueReaderSettings != null)
            {
                foreach (var it in _queueReaderSettings)
                {
                    try
                    {
                        var queue = CreateActiveQueueByType(it.Type);

                        if (queue != null)
                        {
                            _queues.Add(queue);
                            queue.Start(it, context);
                        }
                        else
                        {
                            _monik.ApplicationWarning(
                                $"MessagePump.OnStart cannot initialize {it.Name}: unknown type {it.Type}");
                        }
                    }
                    catch (Exception ex)
                    {
                        _monik.ApplicationError($"MessagePump.OnStart failed initialization {it.Name}: {ex.Message}");
                    }
                } //configure all event sources
            }

            // Start message processing
            _pumpTask = Task.Run(OnProcessTask);

            _monik.ApplicationVerbose("MessagePump started");
        }
Esempio n. 10
0
        public SecureNancyModule(ICacheMetric cacheMetric, ICacheSourceInstance sourceInstanceCache, IMonik monik)
        {
            this.RequiresAuthentication();

            Delete("/instances/{id:int}", args =>
            {
                try
                {
                    monik.ApplicationInfo($"Delete /instances/{args.id} by {Context.CurrentUser.Identity.Name}");
                    sourceInstanceCache.RemoveInstance(args.id);
                    return(HttpStatusCode.OK);
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method DELETE /instances/id : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });

            Delete("/metrics/{id:int}", args =>
            {
                try
                {
                    monik.ApplicationInfo($"Delete /metrics/{args.id} by {Context.CurrentUser.Identity.Name}");
                    cacheMetric.RemoveMetric(args.id);
                    return(HttpStatusCode.OK);
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method DELETE /metrics/id : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });

            Delete("/sources/{id:int}", args =>
            {
                try
                {
                    monik.ApplicationInfo($"Delete /sources/{args.id} by {Context.CurrentUser.Identity.Name}");
                    sourceInstanceCache.RemoveSource((short)args.id);
                    return(HttpStatusCode.OK);
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method DELETE /sources/id : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });

            Post("/groups", args =>
            {
                try
                {
                    var group = this.Bind <Group_>();
                    monik.ApplicationInfo($"Post /groups {group.Name} by {Context.CurrentUser.Identity.Name}");
                    var result = sourceInstanceCache.CreateGroup(group);
                    return(Response.AsJson(result, HttpStatusCode.Created)
                           .WithHeader("Location", $"/groups/{result.ID}"));
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method POST /groups : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });
            Delete("/groups/{id:int}", args =>
            {
                try
                {
                    monik.ApplicationInfo($"Delete /groups/{args.id} by {Context.CurrentUser.Identity.Name}");
                    var result = sourceInstanceCache.RemoveGroup((short)args.id);
                    return(result ? HttpStatusCode.OK : HttpStatusCode.NotFound);
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method DELETE /groups/id : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });
            Put("/groups/{groupId:int}/instances/{instanceId:int}", args =>
            {
                try
                {
                    monik.ApplicationInfo($"Put /groups/{args.groupId}/instances/{args.instanceId} by {Context.CurrentUser.Identity.Name}");
                    sourceInstanceCache.AddInstanceToGroup(args.instanceId, (short)args.groupId);
                    return(HttpStatusCode.OK);
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method PUT /groups/id/instances/id : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });
            Delete("/groups/{groupId:int}/instances/{instanceId:int}", args =>
            {
                try
                {
                    monik.ApplicationInfo($"Delete /groups/{args.groupId}/instances/{args.instanceId} by {Context.CurrentUser.Identity.Name}");
                    var result = sourceInstanceCache.RemoveInstanceFromGroup(args.instanceId, (short)args.groupId);
                    return(result ? HttpStatusCode.OK : HttpStatusCode.NotFound);
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method DELETE /groups/id/instances/id : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });
        }
Esempio n. 11
0
        public void OnStart()
        {
            // 1. Load all sources in memory
            var sources = _repository.GetAllSources();

            foreach (var src in sources)
            {
                if (!_sources.ContainsKey(src.Name))
                {
                    _sourceMap.Add(src.ID, src);
                    _sources.Add(src.Name, src);
                }
                else
                {
                    _monik.ApplicationError($"Database contains more than one same source name: {src.Name}");
                }
            }

            // 2. Load all instances in memory
            var instances = _repository.GetAllInstances();

            foreach (var ins in instances)
            {
                if (_sourceMap.ContainsKey(ins.SourceID))
                {
                    Source src = _sourceMap[ins.SourceID];
                    string key = GetSourceInstanceKey(src.Name, ins.Name);

                    if (!_sourceInstanceMap.ContainsKey(key))
                    {
                        ins.SourceRef(src);
                        _instanceMap.Add(ins.ID, ins);
                        _sourceInstanceMap.Add(key, ins);
                    }
                    else
                    {
                        _monik.ApplicationError(
                            $"Database contains more than one the same instance name '{ins.Name}' for the source '{src.Name}'");
                    }
                }
                else
                {
                    _monik.ApplicationError($"Database doesnt contains source(id={ins.SourceID}) for the instance '{ins.Name}'");
                }
            }

            // 3. Load all groups in memory
            var groups = _repository.GetAllGroupsAndFill();

            _defaultInstances = new List <int>();
            _groups           = new Dictionary <short, Group>();

            foreach (var it in groups)
            {
                if (it.IsDefault)
                {
                    foreach (var it2 in it.Instances)
                    {
                        if (!_defaultInstances.Contains(it2))
                        {
                            _defaultInstances.Add(it2);
                        }
                    }
                }

                _groups.Add(it.ID, it);
            }

            _monik.ApplicationVerbose("CacheSourceInstance started");
        }
Esempio n. 12
0
        public void RunTask(IReportTaskRunContext taskContext)
        {
            Stopwatch duration = new Stopwatch();

            duration.Start();

            bool deleteFolder = false;

            if (taskContext.OpersToExecute.Any(oper => oper.CreateDataFolder))
            {
                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> >();

            if (!CheckIfDependenciesCompleted(taskContext, exceptions))
            {
                return;
            }

            try
            {
                foreach (var oper in taskContext.OpersToExecute)
                {
                    if (taskContext.CancelSource.IsCancellationRequested)
                    {
                        taskContext.CancelSource.Dispose();
                        success = false;
                        break;
                    }

                    RunOperation(taskContext, oper, dtoTaskInstance, exceptions);
                }

                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";
                    SendServiceInfo(msg);
                }

                else
                {
                    success = false;
                    var msg = $"Task {taskContext.TaskId} completed with errors";
                    SendServiceInfo(msg);

                    taskContext.DefaultExporter.SendError(exceptions, taskContext.TaskName);
                }
            }

            catch (Exception e)
            {
                success = false;
                var msg = $"Task {taskContext.TaskId}, named {taskContext.TaskName} is not completed. An error has occurred: {e.Message}";
                monik.ApplicationError(msg);
                Console.WriteLine(msg);
                //  TODO: add a taskId to mail theme.
                taskContext.DefaultExporter.SendError(exceptions, taskContext.TaskName);
            }

            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);
        }
Esempio n. 13
0
        public MainNancyModule(IRepository repo, ICacheLog cacheLog, ICacheKeepAlive cacheKeepAlive,
                               ICacheMetric cacheMetric, ICacheSourceInstance sourceInstanceCache, IMonik monik)
        {
            _repo                = repo;
            _cacheLog            = cacheLog;
            _cacheKeepAlive      = cacheKeepAlive;
            _cacheMetric         = cacheMetric;
            _sourceInstanceCache = sourceInstanceCache;
            _monik               = monik;

            Get("/sources", args =>
            {
                try
                {
                    List <Source> result = sourceInstanceCache.GetAllSources();
                    return(Response.AsJson <Source[]>(result.ToArray()));
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method /sources : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });

            Get("/instances", args =>
            {
                try
                {
                    List <Instance> result = sourceInstanceCache.GetAllInstances();
                    return(Response.AsJson <Instance[]>(result.ToArray()));
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method /instances : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });

            Get("/groups", args =>
            {
                try
                {
                    List <Group> result = sourceInstanceCache.GetAllGroups();
                    return(Response.AsJson <Group[]>(result.ToArray()));
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method /groups : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });

            Post("/logs5", args =>
            {
                try
                {
                    var filter = this.Bind <LogRequest>();

                    List <Log_> result = cacheLog.GetLogs5(filter);
                    return(Response.AsJson <Log_[]>(result.ToArray()));
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method /logs5 : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });

            Post("/keepalive2", args =>
            {
                var filter = this.Bind <KeepAliveRequest>();

                try
                {
                    List <KeepAlive_> result = cacheKeepAlive.GetKeepAlive2(filter);
                    return(Response.AsJson <KeepAlive_[]>(result.ToArray()));
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method /keepalive : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });

            Get("/keepalive-status", args =>
            {
                var filter = new KeepAliveRequest();
                return(GetKeepAliveStatuses(filter));
            });

            Post("/keepalive-status", args =>
            {
                var filter = this.Bind <KeepAliveRequest>();
                return(GetKeepAliveStatuses(filter));
            });

            Get("/metrics", args =>
            {
                try
                {
                    var result = cacheMetric.GetMetricsDescriptions();
                    return(Response.AsJson(result));
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method /metrics : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });

            Get("/metrics/currents", args =>
            {
                try
                {
                    var result = cacheMetric.GetAllCurrentMeasures();
                    return(Response.AsJson(result));
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method /metrics/currents : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });

            Post("/metrics/currents", args =>
            {
                try
                {
                    var filter = this.Bind <MetricRequest>();

                    var result = cacheMetric.GetCurrentMeasures(filter);
                    return(Response.AsJson(result));
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method POST /metrics/currents : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });

            Get("/metrics/{id:int}/current", args =>
            {
                try
                {
                    int metricId = args.id;

                    var result = cacheMetric.GetCurrentMeasure(metricId);
                    return(Response.AsJson(result));
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method /metrics/id/current : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });

            Get("/metrics/windows", args =>
            {
                try
                {
                    var result = cacheMetric.GetAllWindowsMeasures();
                    return(Response.AsJson(result));
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method /metrics/windows : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });

            Post("/metrics/windows", args =>
            {
                try
                {
                    var filter = this.Bind <MetricRequest>();

                    var result = cacheMetric.GetWindowMeasures(filter);
                    return(Response.AsJson(result));
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method POST /metrics/windows : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });

            Get("/metrics/{id:int}/window", args =>
            {
                try
                {
                    int metricId = args.id;

                    var result = cacheMetric.GetWindowMeasure(metricId);
                    return(Response.AsJson(result));
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method /metrics/id/window : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });

            Get("/metrics/{id:int}/history", args =>
            {
                try
                {
                    int metricId = args.id;
                    var p        = this.Bind <MetricHistoryRequestParameters>();
                    var result   = cacheMetric.GetMetricHistory(metricId, p.Amount, p.Skip);
                    return(Response.AsJson(result));
                }
                catch (Exception ex)
                {
                    monik.ApplicationError($"Method /metrics/id/history : {ex.Message}");
                    return(HttpStatusCode.InternalServerError);
                }
            });
        }
Esempio n. 14
0
        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);
        }