/// <inheritdoc/> protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _delayTime = _settingsProvider.GetMessageCheckInterval(); _logger.LogInformation($"Checking new messages every {_delayTime.TotalSeconds} seconds"); try { await _telegramClient.InitializeAsync().ConfigureAwait(false); await _rocketChatClient.InitializeAsync().ConfigureAwait(false); } catch (Exception ex) { _logger.LogCritical(ex.Message); return; } _lastMessageTimeStamp = await GetLastMessageTimeStampAsync().ConfigureAwait(false); while (!stoppingToken.IsCancellationRequested) { if (_chatClientErrorsCount >= MaxErrorsCount || _telegramErrorsCount >= MaxErrorsCount) { _logger.LogCritical("Maximum number of errors reached, execution stopped"); return; } await Task.Delay(_delayTime, stoppingToken).ConfigureAwait(false); var recentMessages = await GetRecentMessagesAsync().ConfigureAwait(false); var newMessageTimeStamp = recentMessages.LatestMessageTimeStamp; if (newMessageTimeStamp > _lastMessageTimeStamp) { _lastMessageTimeStamp = newMessageTimeStamp; _logger.LogInformation("New Rocket.Chat messages received"); await NotifySubscribersAsync(recentMessages).ConfigureAwait(false); } } }
public void InitializeAsync_NoServer_ShouldThrowException() { _settingsProviderMock.Setup(x => x.GetServer()).Returns(string.Empty); Assert.ThrowsAsync <InvalidOperationException>(() => _rocketChatClient.InitializeAsync()); }