diff --git a/src/DevNews.Cli/Program.cs b/src/DevNews.Cli/Program.cs index 12e4450..42b59a5 100644 --- a/src/DevNews.Cli/Program.cs +++ b/src/DevNews.Cli/Program.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; using Cocona; +using Cocona.Application; using DevNews.Cli.Infrastructure; using DevNews.Core.DependencyInjection; using DevNews.Core.UseCases; @@ -7,20 +8,14 @@ using DevNews.Infrastructure.Parsers.DependencyInjection; using DevNews.Infrastructure.Persistence.DependencyInjection; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace DevNews.Cli { public class Program { - private ILogger _logger; - - public Program(ILogger logger) - { - _logger = logger; - } - - static async Task Main(string[] args) => + public static async Task Main(string[] args) => await CoconaApp.Create() .UseLogger("DevNews.Cli") .ConfigureServices((ctx, services) => @@ -30,6 +25,7 @@ await CoconaApp.Create() services.AddNotifiers(configuration); services.AddParsers(configuration); services.AddCore(); + services.AddTransient(); }) .ConfigureAppConfiguration(builder => { @@ -40,10 +36,30 @@ await CoconaApp.Create() .RunAsync(args); public async Task - ProduceNews(int articleQuantity, [FromService] ParseArticlesAndSendItUseCase parseArticlesAndSendItUseCase) + ProduceNews(int articleQuantity, [FromService] ProduceNewsCommand command) + { + await command.ProduceNews(articleQuantity); + } + } + + + public sealed class ProduceNewsCommand + { + private readonly ILogger _logger; + private readonly ICoconaAppContextAccessor _coconaAppContextAccessor; + private readonly ParseArticlesAndSendItUseCase _articlesAndSendItUseCase; + + public ProduceNewsCommand(ILogger logger, ICoconaAppContextAccessor coconaAppContextAccessor, ParseArticlesAndSendItUseCase articlesAndSendItUseCase) + { + _logger = logger; + _coconaAppContextAccessor = coconaAppContextAccessor; + _articlesAndSendItUseCase = articlesAndSendItUseCase; + } + + public async Task ProduceNews(int articleQuantity) { _logger.LogInformation("Start Producing News"); - await parseArticlesAndSendItUseCase.Execute(new ParseArticlesAndSendItParam(articleQuantity)); + await _articlesAndSendItUseCase.Execute(new ParseArticlesAndSendItParam(articleQuantity), _coconaAppContextAccessor.Current.CancellationToken); _logger.LogInformation("Finish Producing News"); } } diff --git a/src/DevNews.Core/Abstractions/INotifier.cs b/src/DevNews.Core/Abstractions/INotifier.cs index 766076d..67c1bb2 100644 --- a/src/DevNews.Core/Abstractions/INotifier.cs +++ b/src/DevNews.Core/Abstractions/INotifier.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using DevNews.Core.Model; @@ -6,11 +7,11 @@ namespace DevNews.Core.Abstractions { public interface INotifier { - Task Notify(IEnumerable
articles); + Task Notify(IEnumerable
articles, CancellationToken cancellationToken = default); } public interface INotificationBroadcaster { - Task Broadcast(IEnumerable
articles); + Task Broadcast(IEnumerable
articles, CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/src/DevNews.Core/Notifications/ChannelsBradcaster.cs b/src/DevNews.Core/Notifications/ChannelsBradcaster.cs index 719a33d..bd1196a 100644 --- a/src/DevNews.Core/Notifications/ChannelsBradcaster.cs +++ b/src/DevNews.Core/Notifications/ChannelsBradcaster.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using DevNews.Core.Abstractions; using DevNews.Core.Model; @@ -15,7 +16,7 @@ public ChannelsNotificationBroadcaster(IEnumerable notifiers) _notifiers = notifiers; } - public async Task Broadcast(IEnumerable
articles) + public async Task Broadcast(IEnumerable
articles, CancellationToken cancellationToken = default) { var tasks = _notifiers.Select(notifier => notifier.Notify(articles)); await Task.WhenAll(tasks); diff --git a/src/DevNews.Core/UseCases/ParseArticlesAndSendIt.cs b/src/DevNews.Core/UseCases/ParseArticlesAndSendIt.cs index d771004..c455dc9 100644 --- a/src/DevNews.Core/UseCases/ParseArticlesAndSendIt.cs +++ b/src/DevNews.Core/UseCases/ParseArticlesAndSendIt.cs @@ -30,7 +30,7 @@ public async Task Execute(ParseArticlesAndSendItParam param, CancellationToken c .Take(param.ArticleQuantity) .ToListAsync(cancellationToken); await _articlesRepository.InsertMany(articles, cancellationToken); - await _notificationBroadcaster.Broadcast(articles); + await _notificationBroadcaster.Broadcast(articles, cancellationToken); } } } \ No newline at end of file diff --git a/src/DevNews.Infrastructure.Notifications/Discord/DiscordWebHookNotifier.cs b/src/DevNews.Infrastructure.Notifications/Discord/DiscordWebHookNotifier.cs index 20c8dbd..54b5127 100644 --- a/src/DevNews.Infrastructure.Notifications/Discord/DiscordWebHookNotifier.cs +++ b/src/DevNews.Infrastructure.Notifications/Discord/DiscordWebHookNotifier.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using DevNews.Core.Abstractions; using DevNews.Core.Model; @@ -20,7 +21,7 @@ public DiscordWebHookNotifier(DiscordWebhookClient discordWebhookClient, ILogger _logger = logger; } - public async Task Notify(IEnumerable
articles) + public async Task Notify(IEnumerable
articles, CancellationToken cancellationToken = default) { _logger.LogInformation("Start Sending articles"); var embeds = articles