Пример #1
0
        private static void RefreshRabbitMqQueue(IServiceProvider serviceProvider)
        {
            var linkRepository        = serviceProvider.GetService <IRepository <Link> >();
            var unitOfWorkFactory     = serviceProvider.GetService <IUnitOfWorkFactory>();
            var rabbitMqConfiguration = serviceProvider.GetService <RabbitMqConfiguration>();
            var rabbitMqService       = new RabbitMqService(rabbitMqConfiguration);

            using (var uow = unitOfWorkFactory.GetUoW())
            {
                var links = linkRepository.GetAll()
                            .Where(x => x.LinkStatus == LinkStatus.Waiting)
                            .ToArray();

                foreach (var link in links)
                {
                    var linkEvent = new LinkEvent {
                        Id = link.Id, Href = link.Href
                    };
                    var successfullySent = rabbitMqService.SendLinkEvent(linkEvent);
                    link.LinkStatus = successfullySent ? LinkStatus.Queue : LinkStatus.Waiting;
                }

                uow.Commit();
            }
        }
Пример #2
0
        public ActionResult Add(string href, int countStep, double tact)
        {
            if (href == null)
            {
                return(UnprocessableEntity("Поле является обязательным"));
            }
            if (href.Length > 50)
            {
                return(UnprocessableEntity("Ссылка должна быть не больше 50 символов"));
            }
            if (countStep < 1)
            {
                return(UnprocessableEntity("Количество шагов должно быть больше 1"));
            }
            if (tact < 0)
            {
                return(UnprocessableEntity("Такт должен быть неотрицательным"));
            }

            try
            {
                new Uri($"http://{href}");
            }
            catch (UriFormatException e)
            {
                return(UnprocessableEntity(e.Message));
            }

            using var uow = _unitOfWorkFactory.GetUoW();
            var link = new Link
            {
                Href       = href,
                CountStep  = countStep,
                Tact       = tact,
                LinkStatus = LinkStatus.Waiting
            };

            _linkRepository.Add(link);
            uow.Commit();

            var linkEvent = new LinkEvent {
                Id = link.Id, Href = link.Href
            };
            var successfullySent = _rabbitMqService.SendLinkEvent(linkEvent);

            link.LinkStatus = successfullySent ? LinkStatus.Queue : LinkStatus.Waiting;
            uow.Commit();

            return(RedirectToAction("Index"));
        }