/// <summary>
        /// 启动统计服务中心
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="SpiderException"></exception>
        public Task StartAsync(CancellationToken cancellationToken)
        {
            if (_isRunning)
            {
                throw new SpiderException("统计中心正在运行中");
            }

            _logger.LogInformation("统计中心启动");

            _mq.Subscribe(Framework.StatisticsServiceTopic, async message =>
            {
                var commandMessage = message.ToCommandMessage();
                if (commandMessage == null)
                {
                    _logger.LogWarning($"接收到非法消息: {message}");
                    return;
                }

                switch (commandMessage.Command)
                {
                case "Success":
                    {
                        var ownerId = commandMessage.Message;
                        await _statisticsStore.IncrementSuccessAsync(ownerId);
                        break;
                    }

                case "Failed":
                    {
                        var data = commandMessage.Message.Split(',');
                        await _statisticsStore.IncrementFailedAsync(data[0], int.Parse(data[1]));
                        break;
                    }

                case "Start":
                    {
                        var ownerId = commandMessage.Message;
                        await _statisticsStore.StartAsync(ownerId);
                        break;
                    }

                case "Exit":
                    {
                        var ownerId = commandMessage.Message;
                        await _statisticsStore.ExitAsync(ownerId);
                        break;
                    }

                case "Total":
                    {
                        var data = commandMessage.Message.Split(',');
                        await _statisticsStore.IncrementTotalAsync(data[0], int.Parse(data[1]));

                        break;
                    }

                case "DownloadSuccess":
                    {
                        var data = commandMessage.Message.Split(',');
                        await _statisticsStore.IncrementDownloadSuccessAsync(data[0], int.Parse(data[1]),
                                                                             long.Parse(data[2]));
                        break;
                    }

                case "DownloadFailed":
                    {
                        var data = commandMessage.Message.Split(',');
                        await _statisticsStore.IncrementDownloadFailedAsync(data[0], int.Parse(data[1]),
                                                                            long.Parse(data[2]));
                        break;
                    }

                case "Print":
                    {
                        var ownerId    = commandMessage.Message;
                        var statistics = await _statisticsStore.GetSpiderStatisticsAsync(ownerId);
                        _logger.LogInformation(
                            $"任务 {ownerId} 总计 {statistics.Total}, 成功 {statistics.Success}, 失败 {statistics.Failed}, 剩余 {statistics.Total - statistics.Success - statistics.Failed}");
                        break;
                    }
                }
            });
#if NETFRAMEWORK
            return(DotnetSpider.Core.Framework.CompletedTask);
#else
            return(Task.CompletedTask);
#endif
        }
        private async Task HandleStatisticsMessageAsync(MessageData <string> message)
        {
            if (message == null)
            {
                _logger.LogWarning("Statistics center receive empty message");
                return;
            }

            switch (message.Type)
            {
            case "Success":
            {
                var ownerId = message.Data;
                await _statisticsStore.IncrementSuccessAsync(ownerId);

                break;
            }

            case "Failed":
            {
                var data = message.Data.Split(',');
                await _statisticsStore.IncrementFailedAsync(data[0], int.Parse(data[1]));

                break;
            }

            case "Start":
            {
                var ownerId = message.Data;
                await _statisticsStore.StartAsync(ownerId);

                break;
            }

            case "Exit":
            {
                var ownerId = message.Data;
                await _statisticsStore.ExitAsync(ownerId);

                break;
            }

            case "Total":
            {
                var data = message.Data.Split(',');
                await _statisticsStore.IncrementTotalAsync(data[0], int.Parse(data[1]));

                break;
            }

            case "DownloadSuccess":
            {
                var data = message.Data.Split(',');
                await _statisticsStore.IncrementDownloadSuccessAsync(data[0], int.Parse(data[1]),
                                                                     long.Parse(data[2]));

                break;
            }

            case "DownloadFailed":
            {
                var data = message.Data.Split(',');
                await _statisticsStore.IncrementDownloadFailedAsync(data[0], int.Parse(data[1]),
                                                                    long.Parse(data[2]));

                break;
            }

            case "Print":
            {
                var ownerId    = message.Data;
                var statistics = await _statisticsStore.GetSpiderStatisticsAsync(ownerId);

                if (statistics != null)
                {
                    _logger.LogInformation(
                        $"{ownerId} total {statistics.Total}, success {statistics.Success}, failed {statistics.Failed}, left {statistics.Total - statistics.Success - statistics.Failed}");
                }

                break;
            }
            }
        }
Exemple #3
0
        private async Task HandleStatisticsMessageAsync(Event message)
        {
            if (string.IsNullOrWhiteSpace(message.Data))
            {
                _logger.LogWarning($"接收到空消息");
                return;
            }

            switch (message.Type)
            {
            case "Success":
            {
                var ownerId = message.Data;
                await _statisticsStore.IncrementSuccessAsync(ownerId);

                break;
            }

            case "Failed":
            {
                var data = message.Data.Split(',');
                await _statisticsStore.IncrementFailedAsync(data[0], int.Parse(data[1]));

                break;
            }

            case "Start":
            {
                var ownerId = message.Data;
                await _statisticsStore.StartAsync(ownerId);

                break;
            }

            case "Exit":
            {
                var ownerId = message.Data;
                await _statisticsStore.ExitAsync(ownerId);

                break;
            }

            case "Total":
            {
                var data = message.Data.Split(',');
                await _statisticsStore.IncrementTotalAsync(data[0], int.Parse(data[1]));

                break;
            }

            case "DownloadSuccess":
            {
                var data = message.Data.Split(',');
                await _statisticsStore.IncrementDownloadSuccessAsync(data[0], int.Parse(data[1]),
                                                                     long.Parse(data[2]));

                break;
            }

            case "DownloadFailed":
            {
                var data = message.Data.Split(',');
                await _statisticsStore.IncrementDownloadFailedAsync(data[0], int.Parse(data[1]),
                                                                    long.Parse(data[2]));

                break;
            }

            case "Print":
            {
                var ownerId    = message.Data;
                var statistics = await _statisticsStore.GetSpiderStatisticsAsync(ownerId);

                if (statistics != null)
                {
                    _logger.LogInformation(
                        $"任务 {ownerId} 总计 {statistics.Total}, 成功 {statistics.Success}, 失败 {statistics.Failed}, 剩余 {statistics.Total - statistics.Success - statistics.Failed}");
                }

                break;
            }
            }
        }
        /// <summary>
        /// 启动统计服务中心
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="SpiderException"></exception>
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            if (IsRunning)
            {
                throw new SpiderException("统计中心正在运行中");
            }

            await _statisticsStore.EnsureDatabaseAndTableCreatedAsync();

            _logger.LogInformation("统计中心准备数据库完成");
            _eventBus.Subscribe(Framework.StatisticsServiceTopic, async message =>
            {
                var commandMessage = message.ToCommandMessage();
                if (commandMessage == null)
                {
                    _logger.LogWarning($"接收到非法消息: {message}");
                    return;
                }

                switch (commandMessage.Command)
                {
                case "Success":
                    {
                        var ownerId = commandMessage.Message;
                        await _statisticsStore.IncrementSuccessAsync(ownerId);
                        break;
                    }

                case "Failed":
                    {
                        var data = commandMessage.Message.Split(',');
                        await _statisticsStore.IncrementFailedAsync(data[0], int.Parse(data[1]));
                        break;
                    }

                case "Start":
                    {
                        var ownerId = commandMessage.Message;
                        await _statisticsStore.StartAsync(ownerId);
                        break;
                    }

                case "Exit":
                    {
                        var ownerId = commandMessage.Message;
                        await _statisticsStore.ExitAsync(ownerId);
                        break;
                    }

                case "Total":
                    {
                        var data = commandMessage.Message.Split(',');
                        await _statisticsStore.IncrementTotalAsync(data[0], int.Parse(data[1]));

                        break;
                    }

                case "DownloadSuccess":
                    {
                        var data = commandMessage.Message.Split(',');
                        await _statisticsStore.IncrementDownloadSuccessAsync(data[0], int.Parse(data[1]),
                                                                             long.Parse(data[2]));
                        break;
                    }

                case "DownloadFailed":
                    {
                        var data = commandMessage.Message.Split(',');
                        await _statisticsStore.IncrementDownloadFailedAsync(data[0], int.Parse(data[1]),
                                                                            long.Parse(data[2]));
                        break;
                    }

                case "Print":
                    {
                        var ownerId    = commandMessage.Message;
                        var statistics = await _statisticsStore.GetSpiderStatisticsAsync(ownerId);
                        _logger.LogInformation(
                            $"任务 {ownerId} 总计 {statistics.Total}, 成功 {statistics.Success}, 失败 {statistics.Failed}, 剩余 {statistics.Total - statistics.Success - statistics.Failed}");
                        break;
                    }
                }
            });

            IsRunning = true;
            _logger.LogInformation("统计中心启动");
        }