gpt4 book ai didi

c# - 不同部署槽中的计时器触发的 Azure Webjob

转载 作者:行者123 更新时间:2023-12-02 23:39:37 28 4
gpt4 key购买 nike

我有一个关于 azure 应用程序服务的网站,在 5 个不同的部署槽(Dev、UAT、Staging、Prelive-1、Prelive-2)中运行,每个槽都包含一个用 .NET Core 2.2 编写的计时器触发的 azure webjob(正确放置)在 App_Data 中的正确位置)。每个 Webjob 都包含一个 appsettings.json 文件,该文件具有不同的要访问的 URL。

网络作业每 5 分钟运行一次,并点击应用程序设置中的 url,但是当我检查 azure Insights 中的日志(配置正确)时,我看到只有一个部署槽正在运行(90% 的情况是第一次部署)插槽——“DEV”之一)。我想让所有 webjob 在所有部署槽中运行。

还有一个问题假设我是否重新启动任何部署槽,例如Prelive-1 然后立即执行网络作业,5 分钟后它不会执行。

有没有办法让它们同时运行或一个一个运行,但我只想让所有部署槽每 5 分钟运行一次网络作业。

下面是我的 Program.cs

public class Program
{
public static async Task Main()
{
HostBuilder hostBuilder = new HostBuilder();
hostBuilder.ConfigureWebJobs(builder =>
{
builder.AddAzureStorageCoreServices();
builder.AddAzureStorage();
builder.AddTimers();
})
.ConfigureAppConfiguration((hostContext, configurationBuilder) =>
{
configurationBuilder.AddJsonFile($"appsettings.json", optional: false, reloadOnChange: false);
})
.ConfigureLogging((context, loggingBuilder) =>
{
string instrumentationKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
if (!string.IsNullOrEmpty(instrumentationKey))
loggingBuilder.AddApplicationInsightsWebJobs(logBuilder => logBuilder.InstrumentationKey = instrumentationKey);
})
.ConfigureServices(services =>
{
services.AddHttpClient();
})
.UseConsoleLifetime();



using (var host = hostBuilder.Build())
{
await host.RunAsync();
}
}

函数.cs

public class Function1
{
const string CheckPendingFulfillmentsUrlConfigKey = "CheckPendingFulfillmentsUrl";
const string WebJobTimerExpression = "0 */5 * * * *";



readonly IHttpClientFactory httpClientFactory;
readonly ILogger<Function1> logger;
readonly string CheckPendingFulfillmentsUrl;



public Function1(IHttpClientFactory HttpClientFactory, ILogger<Function1> Logger, IConfiguration config)
{
httpClientFactory = HttpClientFactory;
logger = Logger;
CheckPendingFulfillmentsUrl = config[CheckPendingFulfillmentsUrlConfigKey];
}



public async Task TriggerCheckPendingFulfillments([TimerTrigger(WebJobTimerExpression, RunOnStartup = false)] TimerInfo timerInfo, CancellationToken cancellationToken)
{
using (var httpClient = httpClientFactory.CreateClient())
{
if (!string.IsNullOrEmpty(CheckPendingFulfillmentsUrl))
{
try
{
Console.WriteLine("Calling the url " + CheckPendingFulfillmentsUrl);
await httpClient.GetAsync(CheckPendingFulfillmentsUrl);
}
catch(Exception Ex)
{
Console.WriteLine("Error -- "+ Ex.Message);
}
logger.LogInformation(string.Format(Properties.Resources.Info_CheckPendingFulfillmentsTriggered, CheckPendingFulfillmentsUrl));
}
}
}
}

最佳答案

这可能是因为您对所有这些 Web 作业使用相同的存储。

来自documentation :

Behind the scenes, TimerTrigger uses the Singleton feature of the WebJobs SDK to ensurethat 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.

所以有两种可能性:

  • 拥有独立的存储帐户:如果每个环境(开发/暂存/产品)都有一个存储帐户,则有意义

  • 指定 JobHostConfiguration 类的 HosId 属性:

var config = new JobHostConfiguration();
config.HostId = "dev|staging|prod";

关于c# - 不同部署槽中的计时器触发的 Azure Webjob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64101550/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com