diff --git a/src/BirdsiteLive.Pipeline/Processors/SaveProgressionProcessor.cs b/src/BirdsiteLive.Pipeline/Processors/SaveProgressionProcessor.cs index c7cbc36..c2f3ff5 100644 --- a/src/BirdsiteLive.Pipeline/Processors/SaveProgressionProcessor.cs +++ b/src/BirdsiteLive.Pipeline/Processors/SaveProgressionProcessor.cs @@ -5,27 +5,57 @@ using BirdsiteLive.DAL.Contracts; using BirdsiteLive.Pipeline.Contracts; using BirdsiteLive.Pipeline.Models; +using Microsoft.Extensions.Logging; namespace BirdsiteLive.Pipeline.Processors { public class SaveProgressionProcessor : ISaveProgressionProcessor { private readonly ITwitterUserDal _twitterUserDal; - + private readonly ILogger _logger; + #region Ctor - public SaveProgressionProcessor(ITwitterUserDal twitterUserDal) + public SaveProgressionProcessor(ITwitterUserDal twitterUserDal, ILogger logger) { _twitterUserDal = twitterUserDal; + _logger = logger; } #endregion public async Task ProcessAsync(UserWithTweetsToSync userWithTweetsToSync, CancellationToken ct) { - var userId = userWithTweetsToSync.User.Id; - var lastPostedTweet = userWithTweetsToSync.Tweets.Select(x => x.Id).Max(); - var minimumSync = userWithTweetsToSync.Followers.Select(x => x.FollowingsSyncStatus[userId]).Min(); - var now = DateTime.UtcNow; - await _twitterUserDal.UpdateTwitterUserAsync(userId, lastPostedTweet, minimumSync, now); + try + { + if (userWithTweetsToSync.Tweets.Length == 0) + { + _logger.LogWarning("No tweets synchronized"); + return; + } + if(userWithTweetsToSync.Followers.Length == 0) + { + _logger.LogWarning("No Followers found for {User}", userWithTweetsToSync.User.Acct); + return; + } + + var userId = userWithTweetsToSync.User.Id; + var followingSyncStatuses = userWithTweetsToSync.Followers.Select(x => x.FollowingsSyncStatus[userId]).ToList(); + + if (followingSyncStatuses.Count == 0) + { + _logger.LogWarning("No Followers sync found for {User}, Id: {UserId}", userWithTweetsToSync.User.Acct, userId); + return; + } + + var lastPostedTweet = userWithTweetsToSync.Tweets.Select(x => x.Id).Max(); + var minimumSync = followingSyncStatuses.Min(); + var now = DateTime.UtcNow; + await _twitterUserDal.UpdateTwitterUserAsync(userId, lastPostedTweet, minimumSync, now); + } + catch (Exception e) + { + _logger.LogError(e, "SaveProgressionProcessor.ProcessAsync() Exception"); + throw; + } } } } \ No newline at end of file diff --git a/src/BirdsiteLive.Twitter/Statistics/TwitterStatisticsHandler.cs b/src/BirdsiteLive.Twitter/Statistics/TwitterStatisticsHandler.cs index afea0d0..04a5649 100644 --- a/src/BirdsiteLive.Twitter/Statistics/TwitterStatisticsHandler.cs +++ b/src/BirdsiteLive.Twitter/Statistics/TwitterStatisticsHandler.cs @@ -87,7 +87,7 @@ public ApiStatistics GetStatistics() UserCallsCountMin = userCalls.Any() ? userCalls.Min() : 0, UserCallsCountAvg = userCalls.Any() ? (int)userCalls.Average() : 0, UserCallsCountMax = userCalls.Any() ? userCalls.Max() : 0, - UserCallsMax = 900, + UserCallsMax = 300, TweetCallsCountMin = tweetCalls.Any() ? tweetCalls.Min() : 0, TweetCallsCountAvg = tweetCalls.Any() ? (int)tweetCalls.Average() : 0, TweetCallsCountMax = tweetCalls.Any() ? tweetCalls.Max() : 0, diff --git a/src/BirdsiteLive/BirdsiteLive.csproj b/src/BirdsiteLive/BirdsiteLive.csproj index 8b8febc..038392d 100644 --- a/src/BirdsiteLive/BirdsiteLive.csproj +++ b/src/BirdsiteLive/BirdsiteLive.csproj @@ -4,7 +4,7 @@ netcoreapp3.1 d21486de-a812-47eb-a419-05682bb68856 Linux - 0.14.4 + 0.14.5 diff --git a/src/BirdsiteLive/Services/FederationService.cs b/src/BirdsiteLive/Services/FederationService.cs index 7ff4c1f..26583f9 100644 --- a/src/BirdsiteLive/Services/FederationService.cs +++ b/src/BirdsiteLive/Services/FederationService.cs @@ -33,6 +33,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) } finally { + await Task.Delay(1000 * 30); _applicationLifetime.StopApplication(); } } diff --git a/src/BirdsiteLive/appsettings.json b/src/BirdsiteLive/appsettings.json index 3dc47ff..106b4e5 100644 --- a/src/BirdsiteLive/appsettings.json +++ b/src/BirdsiteLive/appsettings.json @@ -15,7 +15,7 @@ "AdminEmail": "me@domain.name", "ResolveMentionsInProfiles": true, "PublishReplies": false, - "MaxUsersCapacity": 1400 + "MaxUsersCapacity": 800 }, "Db": { "Type": "postgres", diff --git a/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/SaveProgressionProcessorTests.cs b/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/SaveProgressionProcessorTests.cs index b2a99b9..c95eed6 100644 --- a/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/SaveProgressionProcessorTests.cs +++ b/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/SaveProgressionProcessorTests.cs @@ -8,6 +8,7 @@ using BirdsiteLive.Pipeline.Processors; using BirdsiteLive.Twitter.Models; using Castle.DynamicProxy.Contributors; +using Microsoft.Extensions.Logging; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; @@ -53,6 +54,8 @@ public async Task ProcessAsync_Test() }, User = user }; + + var loggerMock = new Mock>(); #endregion #region Mocks @@ -67,11 +70,12 @@ public async Task ProcessAsync_Test() .Returns(Task.CompletedTask); #endregion - var processor = new SaveProgressionProcessor(twitterUserDalMock.Object); + var processor = new SaveProgressionProcessor(twitterUserDalMock.Object, loggerMock.Object); await processor.ProcessAsync(usersWithTweets, CancellationToken.None); #region Validations twitterUserDalMock.VerifyAll(); + loggerMock.VerifyAll(); #endregion } @@ -129,13 +133,16 @@ public async Task ProcessAsync_PartiallySynchronized_Test() It.IsAny() )) .Returns(Task.CompletedTask); + + var loggerMock = new Mock>(); #endregion - var processor = new SaveProgressionProcessor(twitterUserDalMock.Object); + var processor = new SaveProgressionProcessor(twitterUserDalMock.Object, loggerMock.Object); await processor.ProcessAsync(usersWithTweets, CancellationToken.None); #region Validations twitterUserDalMock.VerifyAll(); + loggerMock.VerifyAll(); #endregion } @@ -201,13 +208,16 @@ public async Task ProcessAsync_PartiallySynchronized_MultiUsers_Test() It.IsAny() )) .Returns(Task.CompletedTask); + + var loggerMock = new Mock>(); #endregion - var processor = new SaveProgressionProcessor(twitterUserDalMock.Object); + var processor = new SaveProgressionProcessor(twitterUserDalMock.Object, loggerMock.Object); await processor.ProcessAsync(usersWithTweets, CancellationToken.None); #region Validations twitterUserDalMock.VerifyAll(); + loggerMock.VerifyAll(); #endregion } }