- Introduced ScheduleJobDefault<TJob> extension for IServiceCollectionQuartzConfigurator - Allows scheduling jobs using either a cron expression or configuration-based cron settings - Includes validation for missing or invalid cron expressions
35 lines
1.2 KiB
C#
35 lines
1.2 KiB
C#
using EnvelopeGenerator.Finalizer.Job;
|
|
using Quartz;
|
|
|
|
namespace EnvelopeGenerator.Finalizer;
|
|
|
|
public static class Extensions
|
|
{
|
|
public static IServiceCollectionQuartzConfigurator ScheduleJobDefault<TJob>(this IServiceCollectionQuartzConfigurator q,
|
|
string croneEpression)
|
|
where TJob : IJob
|
|
{
|
|
var name = $"{typeof(TJob).FullName}";
|
|
var jobKey = new JobKey(name);
|
|
|
|
return q.ScheduleJob<TJob>(trigger => trigger
|
|
.WithIdentity(name + "-trigger")
|
|
.WithCronSchedule(croneEpression),
|
|
job => job.WithIdentity(jobKey)
|
|
);
|
|
}
|
|
|
|
public static IServiceCollectionQuartzConfigurator ScheduleJobDefault<TJob>(this IServiceCollectionQuartzConfigurator q,
|
|
IConfiguration configuration)
|
|
where TJob : IJob
|
|
{
|
|
var expression = configuration[$"{nameof(TJob)}: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(TJob)}:CronExpression'.");
|
|
|
|
return q.ScheduleJobDefault<TJob>(expression);
|
|
}
|
|
} |