feat(Program): make Quartz cron schedule configurable via appsettings
- Replaced hardcoded cron expression with configuration-based `Worker:CronExpression`. - Throws descriptive exception if cron expression is missing. - Keeps previous worker and DB context setup unchanged.
This commit is contained in:
@@ -42,18 +42,23 @@ try
|
||||
var jobKey = new JobKey(name);
|
||||
q.AddJob<Worker>(opts => opts.WithIdentity(jobKey));
|
||||
|
||||
var expression = config[nameof(Worker) + ":CronExpression"];
|
||||
if (string.IsNullOrWhiteSpace(expression))
|
||||
throw new InvalidOperationException(
|
||||
"Cron expression for the Worker job is not configured. " +
|
||||
"Please provide a valid cron schedule in the configuration under " +
|
||||
$"'{nameof(Worker)}:CronExpression'.");
|
||||
|
||||
q.AddTrigger(opts => opts
|
||||
.ForJob(jobKey)
|
||||
.WithIdentity(name + "-trigger")
|
||||
.WithCronSchedule("* * * * * ?"));
|
||||
.WithCronSchedule(expression));
|
||||
});
|
||||
|
||||
builder.Services.AddQuartzHostedService(opt =>
|
||||
{
|
||||
opt.WaitForJobsToComplete = true;
|
||||
});
|
||||
|
||||
builder.Services.Configure<WorkerOptions>(config.GetSection("Worker"));
|
||||
#endregion
|
||||
|
||||
#region Add DB Context, EG Inf. and Services
|
||||
|
||||
Reference in New Issue
Block a user