protected override async Task ExecuteAsync(CancellationToken stoppingToken) { try { _logger.LogInformation("Agent center service is starting"); await _agentStore.EnsureDatabaseAndTableCreatedAsync(); _consumer = new AsyncMessageConsumer <byte[]>(TopicNames.AgentCenter); _consumer.Received += async bytes => { var message = await bytes.DeserializeAsync(stoppingToken); if (message == null) { _logger.LogWarning("Received empty message"); return; } if (message is Register register) { if (_distributed) { _logger.LogInformation($"Register agent: {register.AgentId}, {register.AgentName}"); } await _agentStore.RegisterAsync(new AgentInfo(register.AgentId, register.AgentName, register.ProcessorCount, register.TotalMemory)); } else if (message is Heartbeat heartbeat) { if (_distributed) { _logger.LogInformation($"Receive heartbeat: {heartbeat.AgentId}, {heartbeat.AgentName}"); } await _agentStore.HeartbeatAsync(new AgentHeartbeat(heartbeat.AgentId, heartbeat.AgentName, heartbeat.FreeMemory, heartbeat.CpuLoad)); } else { var msg = Encoding.UTF8.GetString(JsonSerializer.SerializeToUtf8Bytes(message)); _logger.LogWarning($"Not supported message: {msg}"); } }; await _messageQueue.ConsumeAsync(_consumer, stoppingToken); _logger.LogInformation("Agent center service started"); } catch (Exception e) { _logger.LogCritical(e.ToString()); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("Agent register service starting"); await _agentStore.EnsureDatabaseAndTableCreatedAsync(); _consumer = new AsyncMessageConsumer <byte[]>(TopicNames.AgentRegister); _consumer.Received += async bytes => { var message = await bytes.DeserializeAsync(stoppingToken); if (message == null) { _logger.LogWarning("Received empty message"); return; } if (message is Register register) { if (_distributed) { _logger.LogInformation($"Register agent: {register.Id}, {register.Name}"); } await _agentStore.RegisterAsync(new AgentInfo(register.Id, register.Name, register.ProcessorCount, register.TotalMemory)); } else if (message is Heartbeat heartbeat) { if (_distributed) { _logger.LogInformation($"Heartbeat: {heartbeat.AgentId}, {heartbeat.AgentName}"); } await _agentStore.HeartbeatAsync(new AgentHeartbeat(heartbeat.AgentId, heartbeat.AgentName, heartbeat.FreeMemory, heartbeat.CpuLoad)); } else { _logger.LogWarning($"Not supported message: {JsonConvert.SerializeObject(message)}"); } }; await _messageQueue.ConsumeAsync(_consumer, stoppingToken); _logger.LogInformation("Agent register service started"); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { try { _logger.LogInformation("Agent center service is starting"); await _agentStore.EnsureDatabaseAndTableCreatedAsync(); _consumer = new AsyncMessageConsumer <byte[]>(Topics.AgentCenter); _consumer.Received += async bytes => { object message; try { message = await bytes.DeserializeAsync(stoppingToken); if (message == null) { return; } } catch (Exception e) { _logger.LogError($"Deserialize message failed: {e}"); return; } switch (message) { case Messages.Agent.Register register: { if (_distributed) { _logger.LogInformation($"Register agent: {register.AgentId}, {register.AgentName}"); } await _agentStore.RegisterAsync(new AgentInfo(register.AgentId, register.AgentName, register.ProcessorCount, register.Memory)); break; } case Messages.Agent.Heartbeat heartbeat: { if (_distributed) { _logger.LogInformation( $"Receive heartbeat: {heartbeat.AgentId}, {heartbeat.AgentName}"); } await _agentStore.HeartbeatAsync(new AgentHeartbeat(heartbeat.AgentId, heartbeat.AgentName, heartbeat.AvailableMemory, heartbeat.CpuLoad)); break; } default: { var msg = JsonSerializer.Serialize(message); _logger.LogWarning($"Message not supported: {msg}"); break; } } }; await _messageQueue.ConsumeAsync(_consumer, stoppingToken); _logger.LogInformation("Agent center service started"); } catch (Exception e) { _logger.LogCritical(e.ToString()); } }