Newsbot.Collector/Newsbot.Collector.Api/Program.cs
James Tombleson 17e97b4e09
Features/table clients (#5)
* inital table clients are almost ready

* adding new repo interfaces

* adding MS DI to the tables

* adding tables and MS DI to jobs

* updated how the config builder works and pass Iconfig to jobs

* Updated how articles interface returns a value

* updated constructors to support DI and removed static call

* added source consts and model notes

* updated sources.type to contain the value to link what job collects it

* added RssWatcherJob to hangfire

* DI and hangfire jobs wont work.  Defering to options classes

* Services updated to have options exposed over DI

* Tests have been updated.. more to come
2023-02-19 21:39:03 -08:00

71 lines
1.8 KiB
C#

using Hangfire;
using Hangfire.MemoryStorage;
using Newsbot.Collector.Services.Jobs;
using Newsbot.Collector.Domain.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Build the conifg
var config = GetConfiguration();
builder.Configuration.AddConfiguration(config);
builder.Services.AddHangfire(f => f.UseMemoryStorage());
builder.Services.AddHangfireServer();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseHangfireDashboard();
SetupRecurringJobs(config);
app.UseAuthorization();
app.MapControllers();
app.Run();
static IConfiguration GetConfiguration()
{
return new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true)
.AddEnvironmentVariables()
.Build();
}
static void SetupRecurringJobs(IConfiguration configuration)
{
var databaseConnectionString = configuration.GetConnectionString("database");
if (databaseConnectionString is null)
{
databaseConnectionString = "";
}
RecurringJob.AddOrUpdate<HelloWorldJob>("Example", x => x.InitAndExecute(new HelloWorldJobOptions
{
Message = "Hello from the background!"
}), "0/2 * * * *");
//RecurringJob.AddOrUpdate<RssWatcherJob>("RSS", x => x.InitAndExecute(config), "15 0-23 * * *");
var c = new RssWatcherJob();
BackgroundJob.Enqueue(() => c.InitAndExecute(new RssWatcherJobOptions
{
ConnectionString = databaseConnectionString
}));
}