簡體   English   中英

在Azure Webjobs SDK v3中處理ServiceBusTrigger函數中的Webjob關閉

[英]Handling Webjob Shutdown in ServiceBusTrigger Function in Azure Webjobs SDK v3

我正在嘗試在我的函數中正常處理Webjob關閉。 我正在使用Azure Webjobs SDK和Service Bus Extensions的v3。

這是我基於此處的一些示例代碼編寫的測試函數: https : //github.com/Azure/azure-webjobs-sdk-samples/blob/master/BasicSamples/MiscOperations/Functions.cs

    public async Task ProcessQueueMessageAsync([ServiceBusTrigger("testqueue")] Message message, CancellationToken cancellationToken, ILogger logWriter)
    {
        logWriter.LogError("GOT MESSAGE");
        while (!cancellationToken.IsCancellationRequested)
        {
            await Task.Delay(2000);
            logWriter.LogError("Not Cancelled");
        }

        logWriter.LogError("CANCELLED!!!");           
    }

但是,當我關閉webjob時,取消不會被記錄下來。

我也嘗試過捕獲TaskCanceledException,如以下示例所示: https : //github.com/mathewc/samples/blob/master/WebJobSamples/ContinuousJobGracefulShutdown/Functions.cs

那對我也不起作用。 任何想法如何在我的職能中實現這一點?

更新(12/18/18):

雖然我仍然沒有弄清楚,但我有一個適合我目的的解決方法。 在我的Program類中,我聲明了一個public static CancellationToken shutdownToken變量,並將其在Main方法中設置為

shutdownToken = new WebJobsShutdownWatcher().Token;

然后在函數中注冊回調,如下所示:

Program.shutdownToken.Register(() => logWriter.LogWarning("Webjob is shutting down!"));

我參考了您的鏈接代碼,並使用QueueTrigger編寫了一個WebJob ,然后將其上傳並在運行了一段時間后將其停止。 我的輸出日志顯示它工作正常。 也許您可以參考它。

public static string ShutDownFilePath
    {
        get
        {
            return shutDownFile;
        }
    }

    public static void ShutdownMonitorJob(
         [QueueTrigger("myqueue")] string message,
         TextWriter log,
         CancellationToken cancellationToken)
    {

        new Thread(new ThreadStart(() =>
        {
            log.WriteLine("From thread: In about 10 seconds, the job will be signaled to stop");
            Thread.Sleep(10000);

            // Modify the shutdown file
            File.WriteAllText(shutDownFile, string.Empty);
        })).Start();

        log.WriteLine("From function: Received a message: " + message);

        while (!cancellationToken.IsCancellationRequested)
        {
            log.WriteLine("From function: Cancelled: No");
            Thread.Sleep(2000);
        }

        // Perform the graceful shutdown logic here
        log.WriteLine("From function: Cancelled: Yes");
    }

這是我的輸出日志。已取消的狀態更改為是。

[12/18/2018 02:14:07 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:07 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:07 > dd4ec8: INFO] info: Function[0]
[12/18/2018 02:14:07 > dd4ec8: INFO]       From function: Cancelled: No
[12/18/2018 02:14:07 > dd4ec8: INFO]       
[12/18/2018 02:14:09 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:09 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:09 > dd4ec8: INFO] info: Function[0]
[12/18/2018 02:14:09 > dd4ec8: INFO]       From function: Cancelled: No
[12/18/2018 02:14:09 > dd4ec8: INFO]       
[12/18/2018 02:14:09 > dd4ec8: SYS INFO] Detected WebJob file/s were updated, 
refreshing 
WebJob
[12/18/2018 02:14:10 > dd4ec8: SYS INFO] Status changed to Stopping
[12/18/2018 02:14:11 > dd4ec8: INFO] From function: Cancelled: Yes
[12/18/2018 02:14:11 > dd4ec8: INFO] From function: Cancelled: Yes
[12/18/2018 02:14:11 > dd4ec8: INFO] info: Function[0]
[12/18/2018 02:14:11 > dd4ec8: INFO]       From function: Cancelled: Yes
[12/18/2018 02:14:11 > dd4ec8: INFO]       
[12/18/2018 02:14:11 > dd4ec8: INFO] Executed 'Functions.ShutdownMonitorJob' 
(Succeeded, Id=9ce21c43-c113-4794-b565-81cb615a29ab)
[12/18/2018 02:14:11 > dd4ec8: INFO] info: Host.Results[0]
[12/18/2018 02:14:11 > dd4ec8: INFO]       Executed 'Functions.ShutdownMonitorJob' 
(Succeeded, Id=9ce21c43-c113-4794-b565-81cb615a29ab)
[12/18/2018 02:14:11 > dd4ec8: INFO] Job host stopped

我使用ServiceBus觸發器進行了更多測試。

這是我的Program.cs內容。

static void Main(string[] args)
    {

        JobHostConfiguration config = new JobHostConfiguration();
        config.UseServiceBus();
        JobHost host = new JobHost(config);
        host.RunAndBlock();

    }

這是我的功能內容。

private static string shutDownFile = Path.GetTempFileName();

    public static string ShutDownFilePath
    {
        get
        {
            return shutDownFile;
        }
    }


    public static void ShutdownMonitorJob(
         [ServiceBusTrigger("myqueue")]
         string myQueueItem,
         TextWriter log,
         CancellationToken cancellationToken)
    {

        new Thread(new ThreadStart(() =>
        {
            log.WriteLine("From thread: In about 10 seconds, the job will be signaled to stop");
            Thread.Sleep(10000);

            // Modify the shutdown file
            File.WriteAllText(shutDownFile, string.Empty);
        })).Start();

        log.WriteLine("From function: Received a message: " + myQueueItem);

        while (!cancellationToken.IsCancellationRequested)
        {
            log.WriteLine("From function: Cancelled: No");
            Thread.Sleep(2000);
        }

        // Perform the graceful shutdown logic here
        log.WriteLine("From function: Cancelled: Yes");
    }

}

這是我的新日志圖片。 在此處輸入圖片說明

我停止了網絡webjob ,狀態正常變化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM