2015-04-17 34 views
0

我的问题类似于下面的问题。当webjob关闭时更新存储表

Notification of when continuous Azure WebJob is stopping for NoAutomaticTrigger type jobs

我已经使用这个想法从Amit's Blog但随后打了一个小障碍

我有,如果webjob是从门户网站关闭其被触发的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 
     } 
    } 

我在settings.job为300秒增加stopping_wait_time但仍没有运气。

回答

0

你可以使用Microsoft.Azure.WebJobs.WebJobsShutdownWatcher
这是艾米特的解决方案的实现: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循环之后,您也可以停止启动任务。