public async Task ExecuteAsync(IReportTaskRunContext taskContext)
        {
            var package = taskContext.Packages[PackageName];

            if (!RunIfVoidPackage && package.DataSets.Count == 0)
            {
                return;
            }

            await bot.SendTextMessageAsync(channel.ChatId,
                                           viewExecutor.ExecuteTelegramView(package, ReportName, UseAllSets),
                                           ParseMode.Markdown, cancellationToken : taskContext.CancelSource.Token);
        }
Example #2
0
        public void Execute(string address = null)
        {
            var dtoInstance = new DtoFullInstance()
            {
                StartTime = DateTime.Now,
                TaskId    = Id,
                State     = (int)InstanceState.InProcess
            };

            dtoInstance.Id =
                _repository.CreateEntity(_mapper.Map <DtoInstance>(dtoInstance));

            _repository.CreateEntity(_mapper.Map <DtoInstanceData>(dtoInstance));

            string[] deliveryAddrs = { };

            if (!string.IsNullOrEmpty(address))
            {
                deliveryAddrs = new[] { address }
            }
            ;
            else if (SendAddresses != null)
            {
                deliveryAddrs = SendAddresses.GetAddresses();
            }

            Stopwatch duration = new Stopwatch();

            duration.Start();
            int    i            = 1;
            bool   dataObtained = false;
            string jsonReport   = "";
            string htmlReport   = "";

            while (!dataObtained && i <= TryCount)
            {
                try
                {
                    jsonReport   = _dataEx.Execute(this);
                    htmlReport   = _viewEx.ExecuteHtml(ViewTemplate, jsonReport);
                    dataObtained = true;
                    i++;
                    break;
                }
                catch (Exception ex)
                {
                    jsonReport = ex.Message;
                    htmlReport = ex.Message;
                }

                i++;
            }

            if (dataObtained)
            {
                try
                {
                    if (deliveryAddrs?.Length > 0)
                    {
                        _postMaster.Send(ReportName, deliveryAddrs,
                                         HasHtmlBody ? htmlReport : null,
                                         HasJsonAttachment ? jsonReport : null);
                    }

                    if (ChatId != 0)
                    {
                        try
                        {
                            _bot.SendTextMessageAsync(ChatId, _viewEx.ExecuteTelegramView(jsonReport, ReportName),
                                                      ParseMode.Markdown).Wait();
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                            throw;
                        }
                    }

                    _monik.ApplicationInfo($"Отчёт {Id} успешно выслан");
                }
                catch (Exception e)
                {
                    _monik.ApplicationError(e.Message);
                }
            }

            duration.Stop();

            dtoInstance.Data      = _archiver.CompressString(jsonReport);
            dtoInstance.ViewData  = _archiver.CompressString(htmlReport);
            dtoInstance.TryNumber = i - 1;
            dtoInstance.Duration  = Convert.ToInt32(duration.ElapsedMilliseconds);
            dtoInstance.State     = dataObtained ? (int)InstanceState.Success : (int)InstanceState.Failed;

            // string filename = $@"{AppDomain.CurrentDomain.BaseDirectory}\\Report{Id}-{DateTime.Now:HHmmss}";

            _repository.UpdateEntity(_mapper.Map <DtoInstance>(dtoInstance));
            _repository.UpdateEntity(_mapper.Map <DtoInstanceData>(dtoInstance));
        } //method