Ejemplo n.º 1
0
        public async Task <Tuple <bool, string> > Process(ReportDeliveryQueueItem item)
        {
            bool   success = true;
            string result  = "";

            if (item != null && item.ScheduledTask != null)
            {
                try
                {
                    var client  = new RestClient(Config.SystemBehaviorConfig.ResgridBaseUrl);
                    var request = new RestRequest("User/Reports/InternalRunReport", Method.GET);
                    request.AddParameter("type", item.ScheduledTask.Data);
                    request.AddParameter("departmentId", item.Department.DepartmentId);

                    var response = client.Execute(request);

                    if (!string.IsNullOrWhiteSpace(response.Content))
                    {
                        //var content =
                        //	response.Content.Replace(
                        //		"<script type=\"text/javascript\">//<![CDATA[try { if (!window.CloudFlare) { var CloudFlare =[{ verbose: 0,p: 0,byc: 0,owlid: \"cf\",bag2: 1,mirage2: 0,oracle: 0,paths: { cloudflare: \"/cdn-cgi/nexp/dok3v=1613a3a185/\"},atok: \"ab24e007e451de77ff7ceb381c2348f0\",petok: \"80b990bdd3e004d336674babd22922ae48e99033-1471055012-1800\",zone: \"resgrid.com\",rocket: \"0\",apps: { \"ga_key\":{ \"ua\":\"UA-5288869-4\",\"ga_bs\":\"2\"},\"abetterbrowser\":{ \"config\":\"none\"} },sha2test: 0}]; !function(a, b){ a = document.createElement(\"script\"),b = document.getElementsByTagName(\"script\")[0],a.async = !0,a.src = \"//ajax.cloudflare.com/cdn-cgi/nexp/dok3v=0489c402f5/cloudflare.min.js\",b.parentNode.insertBefore(a, b)} ()} } catch (e) { };//]]></script>", "");

                        Regex rRemScript = new Regex(@"<script[^>]*>[\s\S]*?</script>");
                        var   content    = rRemScript.Replace(response.Content, "");

                        var systemNotificaiton = new EmailNotification();
                        systemNotificaiton.Subject = string.Format("{0} Report for {1} ", ((ReportTypes)int.Parse(item.ScheduledTask.Data)), DateTime.UtcNow.TimeConverter(item.Department));

                        string fileName = string.Format("{0}Report_{1}.pdf", ((ReportTypes)int.Parse(item.ScheduledTask.Data)),
                                                        DateTime.UtcNow.TimeConverter(item.Department));

                        fileName = fileName.Replace(" ", "_");
                        fileName = fileName.Replace("/", "");
                        fileName = fileName.Replace(":", "");

                        systemNotificaiton.To             = item.Email;
                        systemNotificaiton.AttachmentName = fileName;
                        systemNotificaiton.AttachmentData = _pdfProvider.ConvertHtmlToPdf(content);

                        _emailService.SendReportDeliveryEmail(systemNotificaiton);
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogException(ex);
                    result  = ex.ToString();
                    success = false;
                }

                if (success)
                {
                    await _scheduledTasksService.CreateScheduleTaskLogAsync(item.ScheduledTask);
                }
            }

            return(new Tuple <bool, string>(success, result));
        }
Ejemplo n.º 2
0
        public async Task ProcessAsync(ReportDeliveryTaskCommand command, IQuidjiboProgress progress, CancellationToken cancellationToken)
        {
            progress.Report(1, $"Starting the {Name} Task");

            await Task.Factory.StartNew(() =>
            {
                var _departmentsService    = Bootstrapper.GetKernel().Resolve <IDepartmentsService>();
                var _scheduledTasksService = Bootstrapper.GetKernel().Resolve <IScheduledTasksService>();
                var _usersService          = Bootstrapper.GetKernel().Resolve <IUsersService>();
                var logic = new ReportDeliveryLogic();

                var allItems = _scheduledTasksService.GetUpcomingScheduledTaks();

                if (allItems != null)
                {
                    // Filter only the past items and ones that are 5 minutes 30 seconds in the future
                    var items = from st in allItems
                                let department                         = _departmentsService.GetDepartmentByUserId(st.UserId)
                                                             let email = _usersService.GetMembershipByUserId(st.UserId).Email
                                                                         let runTime = st.WhenShouldJobBeRun(TimeConverterHelper.TimeConverter(DateTime.UtcNow, department))
                                                                                       where
                                                                                       st.TaskType == (int)TaskTypes.ReportDelivery && runTime.HasValue &&
                                                                                       runTime.Value >= TimeConverterHelper.TimeConverter(DateTime.UtcNow, department) &&
                                                                                       runTime.Value <= TimeConverterHelper.TimeConverter(DateTime.UtcNow, department).AddMinutes(5).AddSeconds(30)
                                                                                       select new
                    {
                        ScheduledTask = st,
                        Department    = department,
                        Email         = email
                    };

                    if (items != null && items.Count() > 0)
                    {
                        _logger.LogInformation("ReportDelivery::Reports to Deliver: " + items.Count());

                        foreach (var i in items)
                        {
                            var qi           = new ReportDeliveryQueueItem();
                            qi.ScheduledTask = i.ScheduledTask;
                            qi.Department    = i.Department;
                            qi.Email         = i.Email;

                            _logger.LogInformation("ReportDelivery::Processing Report:" + qi.ScheduledTask.ScheduledTaskId);

                            var result = logic.Process(qi);

                            if (result.Item1)
                            {
                                _logger.LogInformation($"ReportDelivery::Processed Report {qi.ScheduledTask.ScheduledTaskId} successfully.");
                            }
                            else
                            {
                                _logger.LogInformation($"ReportDelivery::Failed to Process report {qi.ScheduledTask.ScheduledTaskId} error {result.Item2}");
                            }
                        }
                    }
                }
            }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default);

            progress.Report(100, $"Finishing the {Name} Task");
        }