feat(Extensions): add Quartz job scheduling extension methods
- 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
This commit is contained in:
parent
428f71863d
commit
695d7c83e0
35
EnvelopeGenerator.Finalizer/Extensions.cs
Normal file
35
EnvelopeGenerator.Finalizer/Extensions.cs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user