refactoring + init workerservice to delete tweets

This commit is contained in:
Nicolas Constant
2023-01-06 00:28:45 -05:00
parent 5b32a9021f
commit b223bb0216
30 changed files with 124 additions and 31 deletions

View File

@@ -19,6 +19,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Processors\TweetsCleanUp\" />
<Folder Include="Tools\" /> <Folder Include="Tools\" />
</ItemGroup> </ItemGroup>

View File

@@ -3,7 +3,7 @@ using System.Threading.Tasks;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Pipeline.Models; using BirdsiteLive.Pipeline.Models;
namespace BirdsiteLive.Pipeline.Contracts namespace BirdsiteLive.Pipeline.Contracts.Federation
{ {
public interface IRefreshTwitterUserStatusProcessor public interface IRefreshTwitterUserStatusProcessor
{ {

View File

@@ -3,7 +3,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using BirdsiteLive.Pipeline.Models; using BirdsiteLive.Pipeline.Models;
namespace BirdsiteLive.Pipeline.Contracts namespace BirdsiteLive.Pipeline.Contracts.Federation
{ {
public interface IRetrieveFollowersProcessor public interface IRetrieveFollowersProcessor
{ {

View File

@@ -3,7 +3,7 @@ using System.Threading.Tasks;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Pipeline.Models; using BirdsiteLive.Pipeline.Models;
namespace BirdsiteLive.Pipeline.Contracts namespace BirdsiteLive.Pipeline.Contracts.Federation
{ {
public interface IRetrieveTweetsProcessor public interface IRetrieveTweetsProcessor
{ {

View File

@@ -3,7 +3,7 @@ using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow; using System.Threading.Tasks.Dataflow;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
namespace BirdsiteLive.Pipeline.Contracts namespace BirdsiteLive.Pipeline.Contracts.Federation
{ {
public interface IRetrieveTwitterUsersProcessor public interface IRetrieveTwitterUsersProcessor
{ {

View File

@@ -2,7 +2,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using BirdsiteLive.Pipeline.Models; using BirdsiteLive.Pipeline.Models;
namespace BirdsiteLive.Pipeline.Contracts namespace BirdsiteLive.Pipeline.Contracts.Federation
{ {
public interface ISaveProgressionProcessor public interface ISaveProgressionProcessor
{ {

View File

@@ -2,7 +2,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using BirdsiteLive.Pipeline.Models; using BirdsiteLive.Pipeline.Models;
namespace BirdsiteLive.Pipeline.Contracts namespace BirdsiteLive.Pipeline.Contracts.Federation
{ {
public interface ISendTweetsToFollowersProcessor public interface ISendTweetsToFollowersProcessor
{ {

View File

@@ -6,12 +6,12 @@ using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Moderation.Actions; using BirdsiteLive.Moderation.Actions;
using BirdsiteLive.Pipeline.Contracts; using BirdsiteLive.Pipeline.Contracts.Federation;
using BirdsiteLive.Pipeline.Models; using BirdsiteLive.Pipeline.Models;
using BirdsiteLive.Twitter; using BirdsiteLive.Twitter;
using BirdsiteLive.Twitter.Models; using BirdsiteLive.Twitter.Models;
namespace BirdsiteLive.Pipeline.Processors namespace BirdsiteLive.Pipeline.Processors.Federation
{ {
public class RefreshTwitterUserStatusProcessor : IRefreshTwitterUserStatusProcessor public class RefreshTwitterUserStatusProcessor : IRefreshTwitterUserStatusProcessor
{ {

View File

@@ -2,10 +2,10 @@
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.Pipeline.Contracts; using BirdsiteLive.Pipeline.Contracts.Federation;
using BirdsiteLive.Pipeline.Models; using BirdsiteLive.Pipeline.Models;
namespace BirdsiteLive.Pipeline.Processors namespace BirdsiteLive.Pipeline.Processors.Federation
{ {
public class RetrieveFollowersProcessor : IRetrieveFollowersProcessor public class RetrieveFollowersProcessor : IRetrieveFollowersProcessor
{ {

View File

@@ -5,14 +5,14 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Pipeline.Contracts; using BirdsiteLive.Pipeline.Contracts.Federation;
using BirdsiteLive.Pipeline.Models; using BirdsiteLive.Pipeline.Models;
using BirdsiteLive.Twitter; using BirdsiteLive.Twitter;
using BirdsiteLive.Twitter.Models; using BirdsiteLive.Twitter.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Tweetinvi.Models; using Tweetinvi.Models;
namespace BirdsiteLive.Pipeline.Processors namespace BirdsiteLive.Pipeline.Processors.Federation
{ {
public class RetrieveTweetsProcessor : IRetrieveTweetsProcessor public class RetrieveTweetsProcessor : IRetrieveTweetsProcessor
{ {
@@ -64,7 +64,7 @@ namespace BirdsiteLive.Pipeline.Processors
private ExtractedTweet[] RetrieveNewTweets(SyncTwitterUser user) private ExtractedTweet[] RetrieveNewTweets(SyncTwitterUser user)
{ {
var tweets = new ExtractedTweet[0]; var tweets = new ExtractedTweet[0];
try try
{ {
if (user.LastTweetPostedId == -1) if (user.LastTweetPostedId == -1)

View File

@@ -7,18 +7,18 @@ using BirdsiteLive.Common.Extensions;
using BirdsiteLive.Common.Settings; using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Pipeline.Contracts; using BirdsiteLive.Pipeline.Contracts.Federation;
using BirdsiteLive.Pipeline.Tools; using BirdsiteLive.Pipeline.Tools;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace BirdsiteLive.Pipeline.Processors namespace BirdsiteLive.Pipeline.Processors.Federation
{ {
public class RetrieveTwitterUsersProcessor : IRetrieveTwitterUsersProcessor public class RetrieveTwitterUsersProcessor : IRetrieveTwitterUsersProcessor
{ {
private readonly ITwitterUserDal _twitterUserDal; private readonly ITwitterUserDal _twitterUserDal;
private readonly IMaxUsersNumberProvider _maxUsersNumberProvider; private readonly IMaxUsersNumberProvider _maxUsersNumberProvider;
private readonly ILogger<RetrieveTwitterUsersProcessor> _logger; private readonly ILogger<RetrieveTwitterUsersProcessor> _logger;
public int WaitFactor = 1000 * 60; //1 min public int WaitFactor = 1000 * 60; //1 min
#region Ctor #region Ctor
@@ -42,7 +42,7 @@ namespace BirdsiteLive.Pipeline.Processors
var users = await _twitterUserDal.GetAllTwitterUsersAsync(maxUsersNumber, false); var users = await _twitterUserDal.GetAllTwitterUsersAsync(maxUsersNumber, false);
var userCount = users.Any() ? users.Length : 1; var userCount = users.Any() ? users.Length : 1;
var splitNumber = (int) Math.Ceiling(userCount / 15d); var splitNumber = (int)Math.Ceiling(userCount / 15d);
var splitUsers = users.Split(splitNumber).ToList(); var splitUsers = users.Split(splitNumber).ToList();
foreach (var u in splitUsers) foreach (var u in splitUsers)

View File

@@ -5,11 +5,11 @@ using System.Threading.Tasks;
using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Moderation.Actions; using BirdsiteLive.Moderation.Actions;
using BirdsiteLive.Pipeline.Contracts; using BirdsiteLive.Pipeline.Contracts.Federation;
using BirdsiteLive.Pipeline.Models; using BirdsiteLive.Pipeline.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace BirdsiteLive.Pipeline.Processors namespace BirdsiteLive.Pipeline.Processors.Federation
{ {
public class SaveProgressionProcessor : ISaveProgressionProcessor public class SaveProgressionProcessor : ISaveProgressionProcessor
{ {
@@ -36,13 +36,13 @@ namespace BirdsiteLive.Pipeline.Processors
await UpdateUserSyncDateAsync(userWithTweetsToSync.User); await UpdateUserSyncDateAsync(userWithTweetsToSync.User);
return; return;
} }
if(userWithTweetsToSync.Followers.Length == 0) if (userWithTweetsToSync.Followers.Length == 0)
{ {
_logger.LogInformation("No Followers found for {User}", userWithTweetsToSync.User.Acct); _logger.LogInformation("No Followers found for {User}", userWithTweetsToSync.User.Acct);
await _removeTwitterAccountAction.ProcessAsync(userWithTweetsToSync.User); await _removeTwitterAccountAction.ProcessAsync(userWithTweetsToSync.User);
return; return;
} }
var userId = userWithTweetsToSync.User.Id; var userId = userWithTweetsToSync.User.Id;
var followingSyncStatuses = userWithTweetsToSync.Followers.Select(x => x.FollowingsSyncStatus[userId]).ToList(); var followingSyncStatuses = userWithTweetsToSync.Followers.Select(x => x.FollowingsSyncStatus[userId]).ToList();
var lastPostedTweet = userWithTweetsToSync.Tweets.Select(x => x.Id).Max(); var lastPostedTweet = userWithTweetsToSync.Tweets.Select(x => x.Id).Max();

View File

@@ -10,7 +10,7 @@ using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Domain; using BirdsiteLive.Domain;
using BirdsiteLive.Moderation.Actions; using BirdsiteLive.Moderation.Actions;
using BirdsiteLive.Pipeline.Contracts; using BirdsiteLive.Pipeline.Contracts.Federation;
using BirdsiteLive.Pipeline.Models; using BirdsiteLive.Pipeline.Models;
using BirdsiteLive.Pipeline.Processors.SubTasks; using BirdsiteLive.Pipeline.Processors.SubTasks;
using BirdsiteLive.Twitter; using BirdsiteLive.Twitter;
@@ -18,7 +18,7 @@ using BirdsiteLive.Twitter.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Tweetinvi.Models; using Tweetinvi.Models;
namespace BirdsiteLive.Pipeline.Processors namespace BirdsiteLive.Pipeline.Processors.Federation
{ {
public class SendTweetsToFollowersProcessor : ISendTweetsToFollowersProcessor public class SendTweetsToFollowersProcessor : ISendTweetsToFollowersProcessor
{ {
@@ -83,7 +83,7 @@ namespace BirdsiteLive.Pipeline.Processors
} }
} }
} }
private async Task ProcessFollowersWithInboxAsync(ExtractedTweet[] tweets, List<Follower> followerWtInbox, SyncTwitterUser user) private async Task ProcessFollowersWithInboxAsync(ExtractedTweet[] tweets, List<Follower> followerWtInbox, SyncTwitterUser user)
{ {
foreach (var follower in followerWtInbox) foreach (var follower in followerWtInbox)
@@ -114,7 +114,7 @@ namespace BirdsiteLive.Pipeline.Processors
{ {
follower.PostingErrorCount++; follower.PostingErrorCount++;
if (follower.PostingErrorCount > _instanceSettings.FailingFollowerCleanUpThreshold if (follower.PostingErrorCount > _instanceSettings.FailingFollowerCleanUpThreshold
&& _instanceSettings.FailingFollowerCleanUpThreshold > 0 && _instanceSettings.FailingFollowerCleanUpThreshold > 0
|| follower.PostingErrorCount > 2147483600) || follower.PostingErrorCount > 2147483600)
{ {

View File

@@ -0,0 +1,7 @@
namespace BirdsiteLive.Pipeline.Processors.TweetsCleanUp
{
public class DeleteTweetsProcessor
{
}
}

View File

@@ -0,0 +1,7 @@
namespace BirdsiteLive.Pipeline.Processors.TweetsCleanUp
{
public class RetrieveTweetsToDeleteProcessor
{
}
}

View File

@@ -0,0 +1,7 @@
namespace BirdsiteLive.Pipeline.Processors.TweetsCleanUp
{
public class SaveDeletedTweetStatusProcessor
{
}
}

View File

@@ -4,7 +4,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow; using System.Threading.Tasks.Dataflow;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Pipeline.Contracts; using BirdsiteLive.Pipeline.Contracts.Federation;
using BirdsiteLive.Pipeline.Models; using BirdsiteLive.Pipeline.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;

View File

@@ -0,0 +1,18 @@
using System.Threading.Tasks;
using System.Threading;
namespace BirdsiteLive.Pipeline
{
public interface ITweetCleanUpPipeline
{
Task ExecuteAsync(CancellationToken ct);
}
public class TweetCleanUpPipeline : ITweetCleanUpPipeline
{
public async Task ExecuteAsync(CancellationToken ct)
{
throw new System.NotImplementedException();
}
}
}

View File

@@ -29,6 +29,7 @@ namespace BirdsiteLive
.ConfigureServices(services => .ConfigureServices(services =>
{ {
services.AddHostedService<FederationService>(); services.AddHostedService<FederationService>();
services.AddHostedService<TweetCleanUpService>();
}); });
} }
} }

View File

@@ -6,6 +6,7 @@ using BirdsiteLive.DAL;
using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.Moderation; using BirdsiteLive.Moderation;
using BirdsiteLive.Pipeline; using BirdsiteLive.Pipeline;
using BirdsiteLive.Tools;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
namespace BirdsiteLive.Services namespace BirdsiteLive.Services
@@ -32,6 +33,7 @@ namespace BirdsiteLive.Services
try try
{ {
await _databaseInitializer.InitAndMigrateDbAsync(); await _databaseInitializer.InitAndMigrateDbAsync();
InitStateSynchronization.IsDbInitialized = true;
await _moderationPipeline.ApplyModerationSettingsAsync(); await _moderationPipeline.ApplyModerationSettingsAsync();
await _statusPublicationPipeline.ExecuteAsync(stoppingToken); await _statusPublicationPipeline.ExecuteAsync(stoppingToken);
} }

View File

@@ -0,0 +1,43 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using BirdsiteLive.Pipeline;
using BirdsiteLive.Tools;
using Microsoft.Extensions.Hosting;
namespace BirdsiteLive.Services
{
public class TweetCleanUpService : BackgroundService
{
private readonly ITweetCleanUpPipeline _cleanUpPipeline;
private readonly IHostApplicationLifetime _applicationLifetime;
#region Ctor
public TweetCleanUpService(IHostApplicationLifetime applicationLifetime, ITweetCleanUpPipeline cleanUpPipeline)
{
_applicationLifetime = applicationLifetime;
_cleanUpPipeline = cleanUpPipeline;
}
#endregion
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
// Wait for initialization
while (!InitStateSynchronization.IsDbInitialized)
{
if (stoppingToken.IsCancellationRequested) return;
await Task.Delay(250, stoppingToken);
}
await _cleanUpPipeline.ExecuteAsync(stoppingToken);
}
catch (Exception)
{
await Task.Delay(1000 * 30);
_applicationLifetime.StopApplication();
}
}
}
}

View File

@@ -0,0 +1,7 @@
namespace BirdsiteLive.Tools
{
public static class InitStateSynchronization
{
public static bool IsDbInitialized { get; set; }
}
}

View File

@@ -5,7 +5,7 @@ using System.Threading.Tasks;
using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Pipeline.Models; using BirdsiteLive.Pipeline.Models;
using BirdsiteLive.Pipeline.Processors; using BirdsiteLive.Pipeline.Processors.Federation;
using BirdsiteLive.Twitter; using BirdsiteLive.Twitter;
using BirdsiteLive.Twitter.Models; using BirdsiteLive.Twitter.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks.Dataflow;
using BirdsiteLive.Common.Settings; using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Pipeline.Processors; using BirdsiteLive.Pipeline.Processors.Federation;
using BirdsiteLive.Pipeline.Tools; using BirdsiteLive.Pipeline.Tools;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;

View File

@@ -2,7 +2,7 @@
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Moderation.Actions; using BirdsiteLive.Moderation.Actions;
using BirdsiteLive.Pipeline.Models; using BirdsiteLive.Pipeline.Models;
using BirdsiteLive.Pipeline.Processors; using BirdsiteLive.Pipeline.Processors.Federation;
using BirdsiteLive.Twitter.Models; using BirdsiteLive.Twitter.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;

View File

@@ -7,7 +7,7 @@ using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Moderation.Actions; using BirdsiteLive.Moderation.Actions;
using BirdsiteLive.Pipeline.Models; using BirdsiteLive.Pipeline.Models;
using BirdsiteLive.Pipeline.Processors; using BirdsiteLive.Pipeline.Processors.Federation;
using BirdsiteLive.Pipeline.Processors.SubTasks; using BirdsiteLive.Pipeline.Processors.SubTasks;
using BirdsiteLive.Twitter.Models; using BirdsiteLive.Twitter.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;

View File

@@ -2,7 +2,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow; using System.Threading.Tasks.Dataflow;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Pipeline.Contracts; using BirdsiteLive.Pipeline.Contracts.Federation;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq; using Moq;