-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
177 lines (148 loc) · 9.17 KB
/
Copy pathProgram.cs
File metadata and controls
177 lines (148 loc) · 9.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using Microsoft.EntityFrameworkCore;
using SW.Scheduler;
using SW.Scheduler.EfCore;
using SW.Scheduler.MySql;
using SW.Scheduler.PgSql;
using SW.Scheduler.SqlServer;
using SW.Scheduler.Viewer;
using SampleApplication.Data;
using SampleApplication;
var builder = WebApplication.CreateBuilder(args);
var cfg = builder.Configuration;
// ─────────────────────────────────────────────────────────────────────────────
// Read scheduler config
//
// Scheduler:Provider values:
// "pgsql" → PostgreSQL (ConnectionStrings:Scheduler required)
// "mssql" → SQL Server (ConnectionStrings:Scheduler required)
// "mysql" → MySQL (ConnectionStrings:Scheduler required)
// (default) → in-memory (no connection string needed)
//
// Scheduler:Schema is used by pgsql and mssql (default: "quartz").
// ─────────────────────────────────────────────────────────────────────────────
var provider = cfg.GetValue<string>("Scheduler:Provider")?.ToLowerInvariant() ?? "inmemory";
var schema = cfg.GetValue<string>("Scheduler:Schema") ?? "quartz";
// ─────────────────────────────────────────────────────────────────────────────
// Infrastructure
// ─────────────────────────────────────────────────────────────────────────────
builder.Services.AddControllers();
builder.Services.AddControllersWithViews();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new() { Title = "SW.Scheduler Sample", Version = "v1" });
});
// ─────────────────────────────────────────────────────────────────────────────
// Scheduler options — shared regardless of provider
// ─────────────────────────────────────────────────────────────────────────────
void ConfigureSchedulerOptions(SchedulerOptions options)
{
options.SystemUserIdentifier = cfg.GetValue<string>("Scheduler:SystemUserIdentifier") ?? "sample-scheduler";
options.RetentionDays = cfg.GetValue<int>("Scheduler:RetentionDays", 30);
options.CleanupCronExpression = cfg.GetValue<string>("Scheduler:CleanupCronExpression") ?? "0 0 2 * * ?";
options.EnableArchive = cfg.GetValue<bool>("Scheduler:EnableArchive", false);
options.CloudFilesPrefix = cfg.GetValue<string>("Scheduler:CloudFilesPrefix") ?? "sample-app/";
}
// ─────────────────────────────────────────────────────────────────────────────
// EF Core + Scheduler — wired per provider
// ─────────────────────────────────────────────────────────────────────────────
switch (provider)
{
case "pgsql":
{
var connStr = cfg.GetConnectionString("Scheduler")
?? throw new InvalidOperationException("ConnectionStrings:Scheduler is required for provider 'pgsql'");
builder.Services.AddDbContext<AppDbContext>(opt => opt.UseNpgsql(connStr));
builder.Services.AddPgSqlScheduler(
connectionString: connStr,
schema: schema,
configureOptions: ConfigureSchedulerOptions,
assemblies: typeof(Program).Assembly);
break;
}
case "mssql":
{
var connStr = cfg.GetConnectionString("Scheduler")
?? throw new InvalidOperationException("ConnectionStrings:Scheduler is required for provider 'mssql'");
builder.Services.AddDbContext<AppDbContext>(opt => opt.UseSqlServer(connStr));
builder.Services.AddSqlServerScheduler(
connectionString: connStr,
configureOptions: ConfigureSchedulerOptions,
configure: o => o.Schema = schema,
assemblies: typeof(Program).Assembly);
break;
}
case "mysql":
{
var connStr = cfg.GetConnectionString("Scheduler")
?? throw new InvalidOperationException("ConnectionStrings:Scheduler is required for provider 'mysql'");
builder.Services.AddDbContext<AppDbContext>(opt =>
opt.UseMySql(connStr, ServerVersion.AutoDetect(connStr)));
builder.Services.AddMySqlScheduler(
connectionString: connStr,
configureOptions: ConfigureSchedulerOptions,
assemblies: typeof(Program).Assembly);
break;
}
default:
{
// In-memory Quartz store — no migrations, no persistence across restarts.
builder.Services.AddDbContext<AppDbContext>(opt =>
opt.UseInMemoryDatabase("SampleAppDb"));
builder.Services.AddScheduler(
configureOptions: ConfigureSchedulerOptions,
assemblies: typeof(Program).Assembly);
break;
}
}
// ─────────────────────────────────────────────────────────────────────────────
// Job execution monitoring
// ─────────────────────────────────────────────────────────────────────────────
builder.Services.AddSchedulerMonitoring<AppDbContext>();
// ─────────────────────────────────────────────────────────────────────────────
// Scheduler Admin UI
// ─────────────────────────────────────────────────────────────────────────────
builder.Services.AddSchedulerViewer(opts =>
{
opts.Title = "Sample App Scheduler";
opts.PathPrefix = "/scheduler-management";
// opts.AuthorizeAsync = ctx => Task.FromResult(ctx.User.IsInRole("Admin"));
});
// ─────────────────────────────────────────────────────────────────────────────
// Build & configure pipeline
// ─────────────────────────────────────────────────────────────────────────────
var app = builder.Build();
// Seed sample customers so the jobs have data to work with from the start.
await SeedAsync(app);
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "SW.Scheduler Sample v1"));
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.UseSchedulerViewer();
app.MapSchedulerViewer();
app.MapControllers();
app.Run();
// ─────────────────────────────────────────────────────────────────────────────
// Seed helper
// ─────────────────────────────────────────────────────────────────────────────
static async Task SeedAsync(WebApplication app)
{
using var scope = app.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
// Create the database and schema if they don't exist.
// EnsureCreatedAsync works for InMemory and real providers.
// It creates tables directly from the model without migrations — suitable for a sample app.
await db.Database.EnsureCreatedAsync();
if (await db.Customers.AnyAsync()) return;
db.Customers.AddRange(
new Customer { Name = "Alice Smith", Email = "alice@acme.com" },
new Customer { Name = "Bob Jones", Email = "bob@acme.com" },
new Customer { Name = "Carol White", Email = "carol@globex.com" },
new Customer { Name = "Dan Brown", Email = "dan@globex.com" },
new Customer { Name = "Eva Green", Email = "eva@initech.com" }
);
await db.SaveChangesAsync();
}