我是Azure Webjobs的新手。我试图实现GraceFul Shutdown。使用WebJobsShutdownWatcher类时。
Public static void Main()
{
try
{
var config = new JobHostConfiguration();
if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
var watcher = new WebJobsShutdownWatcher();
Task.Run(() =>
{
bool isCancelled = false;
while (!isCancelled)
{
if (watcher.Token.IsCancellationRequested)
{
Console.WriteLine("WebJob cancellation Token Requested!");
isCancelled = true;
}
}
}, watcher.Token).Wait();
var host = new JobHost();
The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
catch (Exception)
{
Console.WriteLine("Error");
}
}
要实现GraceFul ShutDown,我已经停止了Webjob并再次托管在azure上。 在Azure上托管后,队列没有获得触发器。当我调试代码时,控件在WebJobsShutdownWatcher类停止。 我做错了什么?
答案 0 :(得分:2)
正如托马斯所说,你的代码有问题。
由于WebJobsShutdownWatcher类将继续监视webjobs状态,并且您使用wait方法等待WebJobsShutdownWatcher类获取取消令牌,因此永远不会命中host.RunAndBlock方法。
您可以删除等待方法,代码也能正常运行。
在这里,我编写了一个测试演示,它运行良好。
static void Main()
{
var config = new JobHostConfiguration();
if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
var watcher = new WebJobsShutdownWatcher();
Task.Run(() =>
{
bool isCancelled = false;
while (!isCancelled)
{
if (watcher.Token.IsCancellationRequested)
{
Console.WriteLine("WebJob cancellation Token Requested!");
isCancelled = true;
}
}
}, watcher.Token);
var host = new JobHost(config);
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}