public async Task <PushAllSendResult> DoWork()
        {
            var result = new PushAllSendResult();

            try
            {
                result.BeforeTotal = _context.Adverts.Count(a => !a.IsNotificated);

                List <Advert> adverts = _context.Adverts.Where(a => !a.IsNotificated).OrderBy(a => a.CreatedDate).Take(5).ToList();

                foreach (var advert in adverts)
                {
                    bool isSended = _pushNotificationSender.Send(advert);

                    if (isSended)
                    {
                        advert.IsNotificated         = true;
                        _context.Entry(advert).State = EntityState.Modified;
                        _context.SaveChanges();

                        result.SendOk = result.SendOk + 1 ?? 1;
                    }
                    else
                    {
                        result.SendFail = result.SendFail + 1 ?? 1;
                    }

                    await Task.Delay(31000);
                }

                result.AfterTotal = _context.Adverts.Count(a => !a.IsNotificated);
                result.GrandTotal = _context.Adverts.Count();
            }
            catch (Exception ex)
            {
                _logger.Error("AdvertSender", ex);
            }

            return(result);
        }
Exemplo n.º 2
0
        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            _timer.Stop();

            _logger.Info("Сработал таймер сбора");

            try
            {
                List <Link> links = _advertsContext.Links.Where(l => l.IsActive).ToList();
                _logger.Info(string.Format("Количество профилей - {0}", links.Count));

                AvitoParser avitoParser = _scope.Resolve <AvitoParser>();

                foreach (Link link in links)
                {
                    List <Advert> advertsRaw = avitoParser.StartWork(link);
                    List <Advert> adverts    = _advertsErrorChecker.CheckList(advertsRaw);

                    int count = 0;

                    foreach (Advert advert in adverts)
                    {
                        if (_advertsContext.Adverts.Any(a => a.PublicId == advert.PublicId))
                        {
                            continue;
                        }

                        _advertsContext.Adverts.Add(advert);
                        _advertsContext.SaveChanges();

                        count++;
                    }

                    if (count > 0)
                    {
                        _logger.Info(string.Format("{0} -> {1}", link.Name, count));
                    }
                }

                if (links.Any(p => p.IsNew))
                {
                    foreach (Link link in links.Where(l => l.IsNew))
                    {
                        //_pushNotificationSender.Send(link.Channel, "Служебное сообщение",
                        //	"Сервис настроен. Теперь Вам будут приходить сообщения о новых объявлениях", "pushall.ru");

                        link.IsNew = false;
                        _advertsContext.Entry(link).State = EntityState.Modified;
                        _advertsContext.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Application.OnTimedEvent", ex);
            }

            _logger.Info("Обработка завершена");

            _timer.Interval = 45000;
            _timer.Start();
        }