2015-04-17 39 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循環之後,您也可以停止啓動任務。