QUARTZ.NET-作业在多个实例上运行时不会等待完成先前的执行

发布于 2025-01-25 07:41:28 字数 2704 浏览 2 评论 0原文

在多个实例上运行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文