QUARTZ.NET-作业在多个实例上运行时不会等待完成先前的执行
在多个实例上运行Quartz.net作业时,我有问题。我想确保在一个实例上运行的作业完成,然后在第二个实例上触发另一个实例之前。
我尝试使用[disallowConcurrentExecution]
注释,但它仅阻止作业同时运行。
这是我的石英配置:
NameValueCollection properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "TestScheduler";
properties["quartz.scheduler.instanceId"] = "instance_one";
properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";
properties["quartz.jobStore.useProperties"] = "true";
properties["quartz.jobStore.dataSource"] = "default";
properties["quartz.jobStore.tablePrefix"] = "QRTZ_";
properties["quartz.jobStore.lockHandler.type"] = "Quartz.Impl.AdoJobStore.UpdateLockRowSemaphore, Quartz";
properties["quartz.dataSource.default.connectionString"] = postgresConnectionString;
properties["quartz.dataSource.default.provider"] = "Npgsql";
services.AddQuartz(q =>
{
q.Properties.Add(properties);
q.UseDefaultThreadPool(x => x.MaxConcurrency = 5);
q.UsePersistentStore(x =>
{
x.UsePostgres(Configuration.GetConnectionString("QuartzDatabase"));
x.UseClustering(t =>
{
t.CheckinInterval = TimeSpan.FromSeconds(1);
t.CheckinMisfireThreshold = TimeSpan.FromSeconds(1);
});
x.UseJsonSerializer();
});
q.AddJob<HelloJob>(x => x.WithIdentity("job1", "group1").StoreDurably(true));
q.AddTrigger(x => x
.ForJob("job1", "group1")
.WithIdentity("hellojob-trigger")
.WithSimpleSchedule(y =>
{
y.WithIntervalInSeconds(5).RepeatForever();
y.WithMisfireHandlingInstructionIgnoreMisfires();
})
);
});
这是我的工作:
[DisallowConcurrentExecution]
public class HelloJob : IJob
{
public Task Execute(IJobExecutionContext context)
{
LogData();
return Task.CompletedTask;
}
void LogData()
{
Log.Information("Log test {@date}", DateTime.Now);
Thread.Sleep(9000);
}
}
以下是第一个实例的日志:
[20:45:30 INF] Log test 04/28/2022 20:45:30
[20:45:39 INF] Log test 04/28/2022 20:45:39
[20:45:48 INF] Log test 04/28/2022 20:45:48
这是第二个实例的日志:
[20:44:36 INF] Log test 04/28/2022 20:44:36
[20:44:45 INF] Log test 04/28/2022 20:44:45
[20:44:54 INF] Log test 04/28/2022 20:44:54
I have an issue while running Quartz.Net job across multiple instances. I want to be sure that my job that is running on one instance is finished before another one is triggered on the second instance.
I have tried using [DisallowConcurrentExecution]
annotation, but it only prevents jobs from running at the same time accross instances.
Here is my Quartz configuration:
NameValueCollection properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "TestScheduler";
properties["quartz.scheduler.instanceId"] = "instance_one";
properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";
properties["quartz.jobStore.useProperties"] = "true";
properties["quartz.jobStore.dataSource"] = "default";
properties["quartz.jobStore.tablePrefix"] = "QRTZ_";
properties["quartz.jobStore.lockHandler.type"] = "Quartz.Impl.AdoJobStore.UpdateLockRowSemaphore, Quartz";
properties["quartz.dataSource.default.connectionString"] = postgresConnectionString;
properties["quartz.dataSource.default.provider"] = "Npgsql";
services.AddQuartz(q =>
{
q.Properties.Add(properties);
q.UseDefaultThreadPool(x => x.MaxConcurrency = 5);
q.UsePersistentStore(x =>
{
x.UsePostgres(Configuration.GetConnectionString("QuartzDatabase"));
x.UseClustering(t =>
{
t.CheckinInterval = TimeSpan.FromSeconds(1);
t.CheckinMisfireThreshold = TimeSpan.FromSeconds(1);
});
x.UseJsonSerializer();
});
q.AddJob<HelloJob>(x => x.WithIdentity("job1", "group1").StoreDurably(true));
q.AddTrigger(x => x
.ForJob("job1", "group1")
.WithIdentity("hellojob-trigger")
.WithSimpleSchedule(y =>
{
y.WithIntervalInSeconds(5).RepeatForever();
y.WithMisfireHandlingInstructionIgnoreMisfires();
})
);
});
And here is my job:
[DisallowConcurrentExecution]
public class HelloJob : IJob
{
public Task Execute(IJobExecutionContext context)
{
LogData();
return Task.CompletedTask;
}
void LogData()
{
Log.Information("Log test {@date}", DateTime.Now);
Thread.Sleep(9000);
}
}
Here are the logs from the first instance:
[20:45:30 INF] Log test 04/28/2022 20:45:30
[20:45:39 INF] Log test 04/28/2022 20:45:39
[20:45:48 INF] Log test 04/28/2022 20:45:48
And here are the logs from the second instance:
[20:44:36 INF] Log test 04/28/2022 20:44:36
[20:44:45 INF] Log test 04/28/2022 20:44:45
[20:44:54 INF] Log test 04/28/2022 20:44:54
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论