Azure Webjob 定时器触发器不触发
Azure Webjob timer trigger does not fire
我们有两个 Azure Webjob 部署(生产和测试)运行 TimerTrigger。这两个网络应用程序都有单个实例。根据 this article,TimeTriggers 使用单例锁来确保没有并行调用。这两个实例使用相同的存储帐户。我们面临的问题是,只有一个部署似乎获得了锁,而其他部署无法获得锁。如果我们停止第一个 webjob,第二个 webjob 获取锁并开始处理,反之亦然。
锁定是否取决于存储帐户?我们如何确保这两个部署同时具有单独的锁定机制和 运行?
如果您使用 TimerTrigger,请确保在 Web 应用程序上启用了 Always On。
使用不同的存储帐户或设置主机
var config = new JobHostConfiguration();
config.HostId = "dev|prod"
var host = new JobHost(config);
host.RunAndBlock();
你说得对,你必须使用不同的存储帐户。
Behind the scenes, TimerTrigger uses the Singleton feature of the WebJobs SDK to ensure that only a single instance of your triggered function is running at any given time. When the JobHost starts up, for each of your TimerTrigger functions a blob lease (the Singleton Lock) is taken. This distrubuted lock ensures that only a single instance of your scheduled function is running at any time. If the blob for that function is not currently leased, the function will acquire the lease and start running on schedule immediately. If the blob lease cannot be acquired, it generally means that another instance of that function is running, so the function is not started in the current host. When this happens, the host will continue to periodically check to see if it can acquire the lease. This is done as a sort of "recovery" mode to ensure that when running steady state, if an instance goes down, another instance notice and pick up where the other left off.
如果您查看锁定机制的实现 (StorageScheduleMonitor.cs):
- 作业获取容器内的锁(blob)。
- Blob 位于特定目录中(基于 HostId)。
- Blob 的名称不可配置。
所以根据@volodymyr-bilyachat 的回答,有两种可能性:
具有单独的存储帐户:如果每个环境都有一个存储帐户,这很有意义 (dev/staging/prod)
指定 JobHostConfiguration 的 HosId 属性 class:
var config = new JobHostConfiguration();
config.HostId = "dev|staging|prod";
我们有两个 Azure Webjob 部署(生产和测试)运行 TimerTrigger。这两个网络应用程序都有单个实例。根据 this article,TimeTriggers 使用单例锁来确保没有并行调用。这两个实例使用相同的存储帐户。我们面临的问题是,只有一个部署似乎获得了锁,而其他部署无法获得锁。如果我们停止第一个 webjob,第二个 webjob 获取锁并开始处理,反之亦然。
锁定是否取决于存储帐户?我们如何确保这两个部署同时具有单独的锁定机制和 运行?
如果您使用 TimerTrigger,请确保在 Web 应用程序上启用了 Always On。
使用不同的存储帐户或设置主机
var config = new JobHostConfiguration();
config.HostId = "dev|prod"
var host = new JobHost(config);
host.RunAndBlock();
你说得对,你必须使用不同的存储帐户。
Behind the scenes, TimerTrigger uses the Singleton feature of the WebJobs SDK to ensure that only a single instance of your triggered function is running at any given time. When the JobHost starts up, for each of your TimerTrigger functions a blob lease (the Singleton Lock) is taken. This distrubuted lock ensures that only a single instance of your scheduled function is running at any time. If the blob for that function is not currently leased, the function will acquire the lease and start running on schedule immediately. If the blob lease cannot be acquired, it generally means that another instance of that function is running, so the function is not started in the current host. When this happens, the host will continue to periodically check to see if it can acquire the lease. This is done as a sort of "recovery" mode to ensure that when running steady state, if an instance goes down, another instance notice and pick up where the other left off.
如果您查看锁定机制的实现 (StorageScheduleMonitor.cs):
- 作业获取容器内的锁(blob)。
- Blob 位于特定目录中(基于 HostId)。
- Blob 的名称不可配置。
所以根据@volodymyr-bilyachat 的回答,有两种可能性:
具有单独的存储帐户:如果每个环境都有一个存储帐户,这很有意义 (dev/staging/prod)
指定 JobHostConfiguration 的 HosId 属性 class:
var config = new JobHostConfiguration(); config.HostId = "dev|staging|prod";