103 lines
3.1 KiB
C#
103 lines
3.1 KiB
C#
using FictionArchive.Service.SchedulerService.GraphQL;
|
|
using FictionArchive.Service.SchedulerService.Services;
|
|
using FictionArchive.Service.Shared;
|
|
using FictionArchive.Service.Shared.Extensions;
|
|
using FictionArchive.Service.Shared.Services.EventBus.Implementations;
|
|
using Quartz;
|
|
using Quartz.Impl.AdoJobStore;
|
|
|
|
namespace FictionArchive.Service.SchedulerService;
|
|
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
var isSchemaExport = SchemaExportDetector.IsSchemaExportMode(args);
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Services
|
|
builder.Services.AddDefaultGraphQl<Query, Mutation>()
|
|
.AddAuthorization();
|
|
builder.Services.AddHealthChecks();
|
|
builder.Services.AddTransient<JobManagerService>();
|
|
|
|
// Authentication & Authorization
|
|
builder.Services.AddOidcAuthentication(builder.Configuration);
|
|
builder.Services.AddFictionArchiveAuthorization();
|
|
|
|
#region Database
|
|
|
|
builder.Services.RegisterDbContext<SchedulerServiceDbContext>(
|
|
builder.Configuration.GetConnectionString("DefaultConnection"),
|
|
skipInfrastructure: isSchemaExport);
|
|
|
|
#endregion
|
|
|
|
#region Event Bus
|
|
|
|
if (!isSchemaExport)
|
|
{
|
|
builder.Services.AddRabbitMQ(opt =>
|
|
{
|
|
builder.Configuration.GetSection("RabbitMQ").Bind(opt);
|
|
});
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Quartz
|
|
|
|
if (isSchemaExport)
|
|
{
|
|
// Schema export mode: use in-memory store (no DB connection needed)
|
|
builder.Services.AddQuartz(opt =>
|
|
{
|
|
opt.UseInMemoryStore();
|
|
});
|
|
}
|
|
else
|
|
{
|
|
builder.Services.AddQuartz(opt =>
|
|
{
|
|
opt.UsePersistentStore(pso =>
|
|
{
|
|
pso.UsePostgres(pgsql =>
|
|
{
|
|
pgsql.ConnectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
|
pgsql.UseDriverDelegate<PostgreSQLDelegate>();
|
|
pgsql.TablePrefix = "quartz.qrtz_"; // Needed for Postgres due to the differing schema used
|
|
});
|
|
pso.UseNewtonsoftJsonSerializer();
|
|
});
|
|
});
|
|
builder.Services.AddQuartzHostedService(opt =>
|
|
{
|
|
opt.WaitForJobsToComplete = true;
|
|
});
|
|
}
|
|
|
|
#endregion
|
|
|
|
var app = builder.Build();
|
|
|
|
// Update database (skip in schema export mode)
|
|
if (!isSchemaExport)
|
|
{
|
|
using var scope = app.Services.CreateScope();
|
|
var dbContext = scope.ServiceProvider.GetRequiredService<SchedulerServiceDbContext>();
|
|
dbContext.UpdateDatabase();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.MapHealthChecks("/healthz");
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapGraphQL();
|
|
|
|
app.RunWithGraphQLCommands(args);
|
|
}
|
|
} |