Beispiel #1
0
 public async Task IncreaseTotalAsync(string id, long count)
 {
     await _messageQueue.PublishAsBytesAsync(Topics.Statistics,
                                             new Messages.Statistics.Total {
         SpiderId = id, Count = count
     });
 }
Beispiel #2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation(
                _messageQueue.IsDistributed
                                        ? $"Agent {_options.AgentId}, {_options.AgentName} is starting"
                                        : "Agent is starting");

            await _statisticsClient.RegisterAgentAsync(_options.AgentId, _options.AgentName);

            // 分布式才需要注册
            if (_messageQueue.IsDistributed)
            {
                await _messageQueue.PublishAsBytesAsync(Topics.AgentCenter,
                                                        new Messages.Agent.Register
                {
                    AgentId        = _options.AgentId,
                    AgentName      = _options.AgentName,
                    TotalMemory    = SystemInformation.MemoryStatus.TotalMemory,
                    ProcessorCount = Environment.ProcessorCount
                });
            }

            // 同类型下载器注册于相同的 topic,用于负载均衡
            await RegisterAgentAsync(_downloader.Name, stoppingToken);

            if (_messageQueue.IsDistributed)
            {
                // 注册 agent_{id} 用于固定节点下载
                await RegisterAgentAsync(string.Format(Topics.Spider, _options.AgentId), stoppingToken);
            }

            // 分布式才需要发送心跳
            if (_messageQueue.IsDistributed)
            {
                await Task.Factory.StartNew(async() =>
                {
                    while (!stoppingToken.IsCancellationRequested)
                    {
                        await HeartbeatAsync();
                        await Task.Delay(5000, stoppingToken);
                    }
                }, stoppingToken);
            }

            _logger.LogInformation(_messageQueue.IsDistributed
                                ? $"Agent {_options.AgentId}, {_options.AgentName} started"
                                : "Agent started");
        }