【问题标题】:Considerations about Quartz.NET hosted inside scaled out instance of Azure App Service关于托管在 Azure 应用服务横向扩展实例中的 Quartz.NET 的注意事项
【发布时间】:2021-05-13 12:58:21
【问题描述】:

如何处理在 ASP.NET Core 中创建并托管在 Azure App Service 中且横向扩展至多个实例的 API 的 Quartz 配置?

API 当前始终托管在单个 IIS 应用程序中,因此 Quartz 配置如下所示,未使用集群配置

private static NameValueCollection SchedulerConfiguration(IConfiguration configuration)
{
    var schedulerConfiguration = new NameValueCollection();
    schedulerConfiguration["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";
    schedulerConfiguration["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz";
    schedulerConfiguration["quartz.jobStore.tablePrefix"] = "QRTZ_";
    schedulerConfiguration["quartz.jobStore.dataSource"] = "default";
    schedulerConfiguration["quartz.dataSource.default.connectionString"] = configuration["ConnectionString"];
    schedulerConfiguration["quartz.dataSource.default.provider"] = "SqlServer";
    schedulerConfiguration["quartz.serializer.type"] = "json";
    return schedulerConfiguration;
}

它利用QuartzHostedService 处理预定的后台作业

services.AddHostedService<QuartzHostedService>();

我做了一个小实验,并将 API 部署到 Azure 应用服务实例中,然后在 API 中创建了一个 Quartz 作业,并在触发器触发之前将实例数量扩展到 4 个。 我的假设是,不做任何更改上述配置作业将被执行 4 次,因为当新的 3 个实例启动时,它们都会从数据库中读取作业详细信息并注册触发器以在特定时间触发。但令我惊讶的是,这项工作只执行了一次。

  1. 知道为什么作业只执行一次吗?
  2. 在横向扩展的 Azure 应用服务中托管 Quartz 调度程序时是否应该利用集群配置?

【问题讨论】:

    标签: asp.net-core azure-web-app-service quartz.net


    【解决方案1】:
    1. 我认为这可能只是纯粹的运气,如果您的作业运行得很快,它们可能只能由集群中的单个实例运行。但是如果没有集群设置,两个节点可能会运行相同的作业并导致数据库更新冲突。

    2. 是的,在使用基于数据库的锁时,性能会略有下降,但这是您可以安全运行繁忙实例的唯一方法。

    我还建议您查看ASP.NET Core integration package。它有助于编译安全配置,例如,我可以看到您现在为 SQL Server 使用了错误(效率低下)的委托,有单独的 SqlServerDelegate。

    【讨论】:

    • 非常感谢您的时间和回答。我配置了集群,似乎一切正常,我只需要使用SELECT * FROM {0}LOCKS UPDLOCK WHERE SCHED_NAME = @schedulerName AND LOCK_NAME = @lockName 查询quartz.jobStore.selectWithLockSQL,否则我会从 Azure SQL 收到锁定错误。谢谢指出SqlServerDelegate?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 2020-02-24
    • 2017-09-28
    • 1970-01-01
    • 2021-10-13
    • 2017-03-27
    相关资源
    最近更新 更多