0

我的问题类似于下面的问题。

通知 NoAutomaticTrigger 类型作业的连续 Azure WebJob 何时停止

我使用了Amit 博客中的想法,但遇到了一点障碍

我在 webjob 中设置了一个文件观察程序,如果 webjob 从门户关闭,则会触发该文件观察程序。

在网络作业终止之前,我需要更新存储表中的一些标志。

问题是我的代码似乎停在我试图从存储表中检索记录的地方。我在以下代码周围有异常处理程序,并且控制台上没有写入异常消息。

下面是我的代码

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("my storage key");
var tableClient = storageAccount.CreateCloudTableClient();
var table = tableClient.GetTableReference("myTable");
TableOperation operation = TableOperation.Retrieve("partKey", "rowKey");
var result = table.Execute(operation); // stucks here
   if (result.Result != null)
     {
        MyEntity entity = (MyEntity)result.Result;
        if (entity != null)
         {
           entity.IsRunning = false; //reset the flag
           TableOperation update = TableOperation.InsertOrReplace(entity);
           table.Execute(update); //update the record
         }
     }

我已将时间增加到stopping_wait_time300settings.job秒,但仍然没有运气。

4

1 回答 1

0

您可以使用Microsoft.Azure.WebJobs.WebJobsShutdownWatcher
这是 Amit 解决方案的实现:WebJobs Graceful Shutdown

所以我找到了一个解决方案:
在 Program.cs 中没有修改

class Program
{
    static void Main()
    {
        var host = new JobHost();
        host.Call(typeof(Startup).GetMethod("Start"));
        host.RunAndBlock();
    }
}

优雅的关机进入你的功能:

public class Startup
{
    [NoAutomaticTrigger]
    public static void Start(TextWriter log)
    {
        var token = new Microsoft.Azure.WebJobs.WebJobsShutdownWatcher().Token;
        //Shut down gracefully
        while (!token.IsCancellationRequested)
        {
            // Do somethings
        }

       // This code will be executed once the webjob is going to shutdown
       Console.Out.WriteLine("Webjob is shuting down")
    }
}

在 while 循环之后,您还可以停止已启动的任务。

于 2015-12-15T09:21:45.700 回答