diff --git a/src/FilterLists.Agent/Application/Archiver/CommitDownloadedLists.cs b/src/FilterLists.Agent/Application/Archiver/CommitDownloadedLists.cs deleted file mode 100644 index 9eaa30740..000000000 --- a/src/FilterLists.Agent/Application/Archiver/CommitDownloadedLists.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using LibGit2Sharp; -using MediatR; - -namespace FilterLists.Agent.Application.Archiver -{ - public static class CommitDownloadedLists - { - public class Command : IRequest - { - } - - public class Handler : RequestHandler - { - private const string GitRepoDirectory = @"archives"; - - protected override void Handle(Command request) - { - if (!Repository.IsValid(GitRepoDirectory)) - Repository.Init(GitRepoDirectory); - using (var repo = new Repository(GitRepoDirectory)) - { - Commands.Stage(repo, "*"); - var utcNow = DateTime.UtcNow; - var signature = new Signature("FilterLists.Agent", "noreply@filterlists.com", utcNow); - repo.Commit(utcNow.ToShortDateString() + " FilterLists archive by FilterLists.Agent", signature, signature); - } - } - } - } -} \ No newline at end of file diff --git a/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs index a24dbaa9f..440a4b7c8 100644 --- a/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs +++ b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs @@ -1,4 +1,6 @@ -using FilterLists.Agent.Infrastructure.Clients; +using FilterLists.Agent.Core.Interfaces; +using FilterLists.Agent.Infrastructure.Clients; +using FilterLists.Agent.Infrastructure.Repositories; using MediatR; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -10,22 +12,22 @@ public static class ServiceCollectionExtensions { public static void RegisterAgentServices(this IServiceCollection serviceCollection) { - var configuration = GetConfiguration(); + var configurationRoot = BuildConfigurationRoot(); serviceCollection.AddLogging(b => { b.AddConsole(); - b.AddApplicationInsights(configuration["ApplicationInsights:InstrumentationKey"] ?? ""); + b.AddApplicationInsights(configurationRoot["ApplicationInsights:InstrumentationKey"] ?? ""); }); serviceCollection.AddMediatR(typeof(Program).Assembly); serviceCollection.AddHttpClient(); + serviceCollection.AddSingleton(configurationRoot); serviceCollection.AddSingleton(); + serviceCollection.AddTransient(); } - private static IConfigurationRoot GetConfiguration() + private static IConfigurationRoot BuildConfigurationRoot() { - return new ConfigurationBuilder() - .AddEnvironmentVariables() - .Build(); + return new ConfigurationBuilder().AddEnvironmentVariables().Build(); } } } \ No newline at end of file diff --git a/src/FilterLists.Agent/Application/Archiver/CaptureLists.cs b/src/FilterLists.Agent/Features/Archiver/CaptureLists.cs similarity index 86% rename from src/FilterLists.Agent/Application/Archiver/CaptureLists.cs rename to src/FilterLists.Agent/Features/Archiver/CaptureLists.cs index c5a9e3c70..842a387b4 100644 --- a/src/FilterLists.Agent/Application/Archiver/CaptureLists.cs +++ b/src/FilterLists.Agent/Features/Archiver/CaptureLists.cs @@ -3,7 +3,7 @@ using FilterLists.Agent.Core.Interfaces; using MediatR; -namespace FilterLists.Agent.Application.Archiver +namespace FilterLists.Agent.Features.Archiver { public static class CaptureLists { @@ -18,15 +18,15 @@ public class Handler : AsyncRequestHandler public Handler(IMediator mediator, IListInfoRepository listInfoRepository) { - _repo = listInfoRepository; _mediator = mediator; + _repo = listInfoRepository; } protected override async Task Handle(Command request, CancellationToken cancellationToken) { var lists = await _repo.GetAll(); await _mediator.Send(new DownloadLists.Command(lists), cancellationToken); - await _mediator.Send(new CommitDownloadedLists.Command(), cancellationToken); + await _mediator.Send(new CommitLists.Command(), cancellationToken); } } } diff --git a/src/FilterLists.Agent/Features/Archiver/CommitLists.cs b/src/FilterLists.Agent/Features/Archiver/CommitLists.cs new file mode 100644 index 000000000..4994d2dd9 --- /dev/null +++ b/src/FilterLists.Agent/Features/Archiver/CommitLists.cs @@ -0,0 +1,34 @@ +using System; +using LibGit2Sharp; +using MediatR; + +namespace FilterLists.Agent.Features.Archiver +{ + public static class CommitLists + { + public class Command : IRequest + { + } + + public class Handler : RequestHandler + { + private const string RepoDirectory = @"archives"; + private const string SignatureName = @"FilterLists.Agent"; + private const string SignatureEmail = @"noreply@filterlists.com"; + private const string CommitMessageSuffix = " FilterLists archive by FilterLists.Agent"; + + protected override void Handle(Command request) + { + if (!Repository.IsValid(RepoDirectory)) + Repository.Init(RepoDirectory); + using (var repo = new Repository(RepoDirectory)) + { + Commands.Stage(repo, "*"); + var utcNow = DateTime.UtcNow; + var signature = new Signature(SignatureName, SignatureEmail, utcNow); + repo.Commit(utcNow.ToShortDateString() + CommitMessageSuffix, signature, signature); + } + } + } + } +} \ No newline at end of file diff --git a/src/FilterLists.Agent/Application/Archiver/DownloadList.cs b/src/FilterLists.Agent/Features/Archiver/DownloadList.cs similarity index 98% rename from src/FilterLists.Agent/Application/Archiver/DownloadList.cs rename to src/FilterLists.Agent/Features/Archiver/DownloadList.cs index 85254d3a7..43681c7a5 100644 --- a/src/FilterLists.Agent/Application/Archiver/DownloadList.cs +++ b/src/FilterLists.Agent/Features/Archiver/DownloadList.cs @@ -10,7 +10,7 @@ //TODO: upsert into MariaDB Rules table https://stackoverflow.com/questions/15271202/mysql-load-data-infile-with-on-duplicate-key-update -namespace FilterLists.Agent.Application.Archiver +namespace FilterLists.Agent.Features.Archiver { public static class DownloadList { diff --git a/src/FilterLists.Agent/Application/Archiver/DownloadLists.cs b/src/FilterLists.Agent/Features/Archiver/DownloadLists.cs similarity index 89% rename from src/FilterLists.Agent/Application/Archiver/DownloadLists.cs rename to src/FilterLists.Agent/Features/Archiver/DownloadLists.cs index 6a2f79c20..7e9797af9 100644 --- a/src/FilterLists.Agent/Application/Archiver/DownloadLists.cs +++ b/src/FilterLists.Agent/Features/Archiver/DownloadLists.cs @@ -6,7 +6,7 @@ using FilterLists.Agent.Core.Entities; using MediatR; -namespace FilterLists.Agent.Application.Archiver +namespace FilterLists.Agent.Features.Archiver { public static class DownloadLists { @@ -22,7 +22,7 @@ public Command(IEnumerable listInfo) public class Handler : AsyncRequestHandler { - private const int MaxDegreeOfParallelism = 5; //TODO: tune + private const int MaxDegreeOfParallelism = 5; private readonly IMediator _mediator; public Handler(IMediator mediator) @@ -48,7 +48,7 @@ protected override async Task Handle(Command request, CancellationToken cancella private static IEnumerable ShardByHost(IEnumerable listInfo) { return listInfo.GroupBy(l => l.ViewUrl.Host) - .SelectMany((g, gi) => g.Select((l, li) => new {Index = li, GroupIndex = gi, ListInfo = l})) + .SelectMany((g, gi) => g.Select((l, i) => new {Index = i, GroupIndex = gi, ListInfo = l})) .OrderBy(a => a.Index) .ThenBy(a => a.GroupIndex) .Select(a => a.ListInfo); diff --git a/src/FilterLists.Agent/Application/Archiver/FileType.cs b/src/FilterLists.Agent/Features/Archiver/FileType.cs similarity index 69% rename from src/FilterLists.Agent/Application/Archiver/FileType.cs rename to src/FilterLists.Agent/Features/Archiver/FileType.cs index e6eda328a..f9f3d564e 100644 --- a/src/FilterLists.Agent/Application/Archiver/FileType.cs +++ b/src/FilterLists.Agent/Features/Archiver/FileType.cs @@ -1,4 +1,4 @@ -namespace FilterLists.Agent.Application.Archiver +namespace FilterLists.Agent.Features.Archiver { public enum FileType { diff --git a/src/FilterLists.Agent/Application/Archiver/StreamExtensions.cs b/src/FilterLists.Agent/Features/Archiver/StreamExtensions.cs similarity index 97% rename from src/FilterLists.Agent/Application/Archiver/StreamExtensions.cs rename to src/FilterLists.Agent/Features/Archiver/StreamExtensions.cs index 3be344680..d05e1cd57 100644 --- a/src/FilterLists.Agent/Application/Archiver/StreamExtensions.cs +++ b/src/FilterLists.Agent/Features/Archiver/StreamExtensions.cs @@ -5,7 +5,7 @@ using SharpCompress.Archives.SevenZip; using SharpCompress.Readers; -namespace FilterLists.Agent.Application.Archiver +namespace FilterLists.Agent.Features.Archiver { public static class StreamExtensions { diff --git a/src/FilterLists.Agent/Program.cs b/src/FilterLists.Agent/Program.cs index 067489a6c..d16f4754f 100644 --- a/src/FilterLists.Agent/Program.cs +++ b/src/FilterLists.Agent/Program.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; -using FilterLists.Agent.Application.Archiver; using FilterLists.Agent.Extensions; +using FilterLists.Agent.Features.Archiver; using MediatR; using Microsoft.Extensions.DependencyInjection;