Quartz.net db job只有作为单例添加时才能工作



我有一个程序,我将我的工作添加到数据库中,并根据需要安排它们。我曾经在program.cs中添加我的工作作为单例,它工作得很好,但只允许我为每个调度器添加单个工作,我无法检查它是否已经存在。因此,我将所有这些移到一个调度程序中,在那里我现在检查作业是否已经在我的数据库中,如果不是,那么我添加它。

它添加的工作很好,但他们根本不工作,每当我试图调用一个我得到一个nullreference异常从JobRunShell.Run()。我不能为我的生活弄清楚为什么,工作看起来完全一样在数据库中,我找不到任何错误与我的代码。难道我不应该增加这样的工作吗?

将job添加为单例的Program.cs

builder.Services.AddSingleton<IJobFactory, JobFactory>();
builder.Services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
builder.Services.AddSingleton<MailHRNewEmployee>();
builder.Services.AddSingleton(new JobMetadata(Guid.NewGuid(), typeof(MailHRNewEmployee), "HR First Contact", "job blueprint"));
builder.Services.AddHostedService<MyScheduler>();

MyScheduler.cs

public async Task StartAsync(CancellationToken cancellationToken)
{
//Creating Scheduler
Scheduler = await schedulerFactory.GetScheduler();
Scheduler.JobFactory = jobFactory;
//Start Scheduler
await Scheduler.Start(cancellationToken);
//Create Jobs
var istrue = await Scheduler.CheckExists(new JobKey("HR First Contact", "DEFAULT"));
if (!(bool)istrue)
{
jobMetaData = new JobMetadata(Guid.NewGuid(), typeof(MailHRNewEmployee), "HR First Contact", "job blueprint");
IJobDetail jobDetail = CreateJob(jobMetaData);
await Scheduler.AddJob(jobDetail, true);
}
...(checking all the other jobs)
private IJobDetail CreateJob(JobMetadata jobMetadata)
{
return JobBuilder.Create(jobMetaData.JobType).WithIdentity(jobMetaData.JobName.ToString()).StoreDurably(true).WithDescription(jobMetaData.JobDescription).Build();
}

NullReferenceException异常堆栈跟踪

[10:43:31 ERR] Job DEFAULT.HR First Contact threw an unhandled Exception: 
System.NullReferenceException: Object reference not set to an instance of an object.
at Quartz.Core.JobRunShell.Run(CancellationToken cancellationToken)
[10:43:32 ERR] Job DEFAULT.HR First Contact threw an exception.
Quartz.SchedulerException: Job threw an unhandled exception.
---> System.NullReferenceException: Object reference not set to an instance of an object.
at Quartz.Core.JobRunShell.Run(CancellationToken cancellationToken)
--- End of inner exception stack trace --- [See nested exception: System.NullReferenceException: Object reference not set to an instance of an object.
at Quartz.Core.JobRunShell.Run(CancellationToken cancellationToken)]

编辑:

我尝试使用MS DI集成库与石英托管服务,但无法让它与持久的作业存储工作&MySql .

builder.Services.Configure<QuartzOptions>(builder.Configuration.GetSection("Quartz"));
builder.Services.Configure<QuartzOptions>(options =>
{
options.Scheduling.IgnoreDuplicates = true;
options.Scheduling.OverWriteExistingData = true;
});
builder.Services.AddQuartz(q =>
{
q.SchedulerId = "Job Creator";
q.UseMicrosoftDependencyInjectionJobFactory();

var jobKey = new JobKey("HR First Contact", "DEFAULT");
q.AddJob<MailHRNewEmployee>(jobKey, j => j
.StoreDurably()
.WithDescription("job blueprint"));
q.UsePersistentStore(s =>
{
s.PerformSchemaValidation = true;
s.UseProperties = true;
s.RetryInterval = TimeSpan.FromSeconds(15);
//Neither .UseMySql nor UseMySqlConnector work
//MySqlConnector should be the one I want though
s.UseMySqlConnector(MySql =>
{
//I am loading the configuration above but don't know how to use the connection string here?
//I thought it would maybe be Configuration.GetConnectionString("xyz");
MySql.ConnectionString = "MyConnectionString";
MySql.TablePrefix = "QRTZ_";
});
s.UseJsonSerializer();
});
});
builder.Services.AddTransient<MailHRNewEmployee>();
builder.Services.AddQuartzHostedService(options =>
{
options.WaitForJobsToComplete = true;
});

Jobs应该被注册为transient, Quartz期望从job factory获得一个新的实例。您是否考虑过使用MS DI集成库和托管服务集成库?

最新更新