Skip to content

Commit

Permalink
add cancelation token
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikus1993 committed Aug 28, 2021
1 parent ac4896b commit 0bf00c7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
36 changes: 26 additions & 10 deletions src/DevNews.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
using System.Threading.Tasks;
using Cocona;
using Cocona.Application;
using DevNews.Cli.Infrastructure;
using DevNews.Core.DependencyInjection;
using DevNews.Core.UseCases;
using DevNews.Infrastructure.Notifications.DependencyInjection;
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<Program> _logger;

public Program(ILogger<Program> 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) =>
Expand All @@ -30,6 +25,7 @@ await CoconaApp.Create()
services.AddNotifiers(configuration);
services.AddParsers(configuration);
services.AddCore();
services.AddTransient<ProduceNewsCommand>();
})
.ConfigureAppConfiguration(builder =>
{
Expand All @@ -40,10 +36,30 @@ await CoconaApp.Create()
.RunAsync<Program>(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<Program> _logger;
private readonly ICoconaAppContextAccessor _coconaAppContextAccessor;
private readonly ParseArticlesAndSendItUseCase _articlesAndSendItUseCase;

public ProduceNewsCommand(ILogger<Program> 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");
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/DevNews.Core/Abstractions/INotifier.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using DevNews.Core.Model;

namespace DevNews.Core.Abstractions
{
public interface INotifier
{
Task Notify(IEnumerable<Article> articles);
Task Notify(IEnumerable<Article> articles, CancellationToken cancellationToken = default);
}

public interface INotificationBroadcaster
{
Task Broadcast(IEnumerable<Article> articles);
Task Broadcast(IEnumerable<Article> articles, CancellationToken cancellationToken = default);
}
}
3 changes: 2 additions & 1 deletion src/DevNews.Core/Notifications/ChannelsBradcaster.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,7 +16,7 @@ public ChannelsNotificationBroadcaster(IEnumerable<INotifier> notifiers)
_notifiers = notifiers;
}

public async Task Broadcast(IEnumerable<Article> articles)
public async Task Broadcast(IEnumerable<Article> articles, CancellationToken cancellationToken = default)
{
var tasks = _notifiers.Select(notifier => notifier.Notify(articles));
await Task.WhenAll(tasks);
Expand Down
2 changes: 1 addition & 1 deletion src/DevNews.Core/UseCases/ParseArticlesAndSendIt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,7 +21,7 @@ public DiscordWebHookNotifier(DiscordWebhookClient discordWebhookClient, ILogger
_logger = logger;
}

public async Task Notify(IEnumerable<Article> articles)
public async Task Notify(IEnumerable<Article> articles, CancellationToken cancellationToken = default)
{
_logger.LogInformation("Start Sending articles");
var embeds = articles
Expand Down

0 comments on commit 0bf00c7

Please sign in to comment.