Skip to content

Instantly share code, notes, and snippets.

@mdmsua
Created August 13, 2021 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdmsua/5734aaf317abbd2828e7d2177c91dba3 to your computer and use it in GitHub Desktop.
Save mdmsua/5734aaf317abbd2828e7d2177c91dba3 to your computer and use it in GitHub Desktop.
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