首页 > 解决方案 > 在 Azure Webjobs SDK v3 中的 ServiceBusTrigger 函数中处理 Webjob 关闭

问题描述

我正在尝试在我的函数中优雅地处理 webjob 关闭。我正在使用 Azure Webjobs SDK 的 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!!!");           
    }

但是,当我关闭网络作业时,不会记录取消。

我也尝试过捕获 TaskCanceledException,如下例所示:https ://github.com/mathewc/samples/blob/master/WebJobSamples/ContinuousJobGracefulShutdown/Functions.cs

那对我也不起作用。任何想法如何在我的功能中实现这一点?

更新(18 年 12 月 18 日):

虽然我还没有弄清楚这一点,但我有一个适合我目的的解决方法。在我的 Program 类中,我声明了一个public static CancellationToken shutdownToken变量,并在我的 Main 方法中将其设置为

shutdownToken = new WebJobsShutdownWatcher().Token;

然后我在我的函数中注册一个回调,如下所示:

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

标签: c#azure-webjobs

解决方案


我参考您的链接代码并编写了一个WebJobwith QueueTrigger,然后上传并在运行一段时间后停止它。我的输出日志显示它工作正常。也许你可以参考一下。

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 Trigger 做了更多的测试。

这是我的 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并且状态正常更改。


推荐阅读