-
-
Save mdmsua/5734aaf317abbd2828e7d2177c91dba3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Common; | |
using CountryPull; | |
using Dapr.Client; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
await Host.CreateDefaultBuilder(args).ConfigureServices((context, services) => | |
{ | |
services.AddSingleton(provider => new DaprClientBuilder().Build()); | |
services.AddDbContext<AppleSystemStatusDbContext>(configuration => configuration.UseSqlServer(context.Configuration.GetConnectionString("Database")).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)); | |
services.Configure<Settings>(context.Configuration.GetSection(nameof(Settings))); | |
services.AddHostedService<Worker>(); | |
}).Build().RunAsync(); | |
record Settings(string ChannelName, string TopicName); | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Common; | |
using Dapr.Client; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.Options; | |
namespace CountryPull | |
{ | |
public class Worker : BackgroundService | |
{ | |
private readonly AppleSystemStatusDbContext context; | |
private readonly DaprClient client; | |
private readonly Settings settings; | |
internal Worker(AppleSystemStatusDbContext context, DaprClient client, IOptions<Settings> settings) | |
{ | |
this.context = context; | |
this.client = client; | |
this.settings = settings.Value; | |
} | |
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
{ | |
if (stoppingToken.IsCancellationRequested) | |
{ | |
return; | |
} | |
var countries = await context.Countries.Select(country => country.Id).ToListAsync(stoppingToken); | |
await Task.WhenAll(countries.Select(country => client.PublishEventAsync(settings.ChannelName, settings.TopicName, country, stoppingToken))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment