-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathProgram.cs
More file actions
72 lines (57 loc) · 3.17 KB
/
Copy pathProgram.cs
File metadata and controls
72 lines (57 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using Hangfire;
using Hangfire.PostgreSql;
using OpenShock.Common;
using OpenShock.Common.Extensions;
using OpenShock.Cron;
using OpenShock.Cron.Services.Email;
using OpenShock.Cron.Utils;
using OpenShock.Common.Swagger;
var builder = OpenShockApplication.CreateDefaultBuilder<Program>(args);
var redisOptions = builder.RegisterRedisOptions();
var databaseOptions = builder.RegisterDatabaseOptions();
builder.RegisterMetricsOptions();
// The outbox dispatcher builds activation/reset links, so it needs the frontend base URL.
builder.RegisterFrontendOptions();
builder.Services.AddOpenShockMemDB(redisOptions);
builder.Services.AddOpenShockDB(databaseOptions);
builder.Services.AddOpenShockServices();
// Hangfire workers fetch enqueued jobs by polling the queue table. The default interval (15s) is
// left as-is in production - email delivery within that window is fine and it adds no DB load. Only
// the integration test overrides it (via OpenShock:Hangfire:QueuePollInterval) to run fast.
var hangfireStorageOptions = new PostgreSqlStorageOptions();
if (builder.Configuration.GetValue<TimeSpan?>("OpenShock:Hangfire:QueuePollInterval") is { } queuePollInterval)
hangfireStorageOptions.QueuePollInterval = queuePollInterval;
builder.Services.AddHangfire(hangfire =>
hangfire.UsePostgreSqlStorage(
c => c.UseNpgsqlConnection(databaseOptions.Conn),
hangfireStorageOptions));
builder.Services.AddHangfireServer();
// Registers the email providers, the outbox dispatcher, and the Redis notification listener. Delivery
// is the EmailOutboxDeliveryJob, driven through Hangfire (recurring every-minute sweep auto-registered
// via [CronJob], plus on-demand enqueue from the listener); all retry/lease/state lives on the
// email_outbox row, not in Hangfire. The API host only writes outbox rows; all sending happens here.
await builder.AddEmailService();
builder.AddSwaggerExt<Program>();
var app = builder.Build();
await app.UseCommonOpenShockMiddleware();
// The Cron host does not own migrations (the API is the sole migrator). Its OpenShockContext binds
// Postgres enum types by name at the pooled data source's first connection and caches them for the
// process's life, so it must not open that context before a newly-added enum exists - otherwise every
// claim query fails permanently ("data type name 'email_status' could not be found"). Block until the
// migrator has applied all pending migrations, which happens before Hangfire or any job runs below.
await app.WaitForOpenShockSchemaReady(databaseOptions);
var hangfireOptions = new DashboardOptions();
if (app.Environment.IsProduction() || Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true")
{
hangfireOptions.Authorization = [ ];
hangfireOptions.AsyncAuthorization = [ new DashboardAdminAuth() ];
}
app.UseHangfireDashboard(options: hangfireOptions);
var jobManager = app.Services.GetRequiredService<IRecurringJobManagerV2>();
foreach (var cronJob in CronJobCollector.GetAllCronJobs())
{
jobManager.AddOrUpdate(cronJob.Name, cronJob.Job, cronJob.Schedule);
}
await app.RunAsync();
// Expose Program for integration tests (so the test host can boot the Cron pipeline in-process).
public partial class Program;