Beispiel #1
0
        public async Task <bool> RunAsync(OrderingScheduleArgs args)
        {
            _logger.LogInformation("Execute Ordering Scheduler: {0}-{1}-{2}", args.LdpMerchanerId, args.LdpOrderId, args.Message);
            await _dispatchOrderingMessageService.PublishAsync(args.LdpOrderId, args.LdpMerchanerId, args.Message);

            return(true);
        }
        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            return(_busClient.SubscribeAsync <LvpOrderMessage>(async(message) =>
            {
                try
                {
                    ILotteryMerchanterApplicationService lotteryMerchanterApplicationService = _iocResolver.GetRequiredService <ILotteryMerchanterApplicationService>();

                    IUnitOfWorkManager unitOfWorkManager = _iocResolver.GetRequiredService <IUnitOfWorkManager>();
                    using (var uow = unitOfWorkManager.Begin())
                    {
                        /* 此处必须保证投注渠道已经开通相应的彩种和出票渠道*/
                        string ldpVenderId = await lotteryMerchanterApplicationService.FindLdpMerchanterIdAsync(message.LvpVenderId, message.LotteryId);
                        if (string.IsNullOrEmpty(ldpVenderId))
                        {
                            _logger.LogError("当前投注渠道{0}不支持该彩种{1}", message.LvpVenderId, message.LotteryId);
                            return new Nack();
                        }
                        IOrderingApplicationService orderingApplicationService = _iocResolver.GetRequiredService <IOrderingApplicationService>();
                        (LotteryMerchanteOrder order, TimeSpan? delay) = await orderingApplicationService.CreateAsync(message.LvpOrderId, message.LvpUserId, message.LvpVenderId, message.LotteryId, message.LotteryPlayId, message.IssueNumber, message.InvestCode, message.InvestType, message.InvestCount, message.InvestTimes, message.InvestAmount);

                        /* 需要延期投注的票加入计划任务,否则直接分票投注*/
                        if (delay.HasValue)
                        {
                            ISchedulerManager schedulerManager = _iocResolver.GetRequiredService <ISchedulerManager>();
                            await schedulerManager.EnqueueAsync <ILotteryOrderingScheduler, OrderingScheduleArgs>(new OrderingScheduleArgs
                            {
                                LdpOrderId = order.Id,
                                LdpMerchanerId = ldpVenderId,
                                Message = message
                            }, SchedulerPriority.High, delay);
                        }
                        else
                        {
                            await _dispatchOrderingMessageService.PublishAsync(order.Id, ldpVenderId, message);
                        }
                        uow.Complete();
                        return new Ack();
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error of the ordering :{0}", message.LvpOrderId);
                }
                return new Nack();
            }, context =>
            {
                context.UseSubscribeConfiguration(configuration =>
                {
                    configuration.OnDeclaredExchange(exchange =>
                    {
                        exchange.WithName("Baibaocp.LotteryOrdering")
                        .WithDurability(true)
                        .WithAutoDelete(false)
                        .WithType(ExchangeType.Topic);
                    });
                    configuration.FromDeclaredQueue(queue =>
                    {
                        queue.WithName("LotteryOrdering.Orders")
                        .WithAutoDelete(false)
                        .WithDurability(true);
                    });
                    configuration.Consume(consume =>
                    {
                        consume.WithRoutingKey("LotteryOrdering.Accepted.#");
#if DEBUG
                        consume.WithPrefetchCount(1);
#endif
                    });
                });
            }, stoppingToken));
        }