本文介绍了Azure触发的Webjob-检测Webjob何时停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用 TimerTrigger .

在webjob停止之前,我需要处置一些对象,但是我不知道如何触发"webjob停止".

Before the webjob stops, I need to dispose some objects but I don't know how to trigger the "webjob stop".

具有NoAutomaticTrigger函数,我知道我可以使用 WebJobsShutdownWatcher 类,用于在Webjob停止但已触发作业的情况下进行处理,我需要一些帮助...

Having a NoAutomaticTrigger function, I know that I can use the WebJobsShutdownWatcher class to handle when the webjob is stopping but with a triggered job I need some help...

我看了使用Azure WebJobs SDK 1.1.0-alpha1 的可扩展触发器和绑定器.

I had a look at Extensible Triggers and Binders with Azure WebJobs SDK 1.1.0-alpha1.

创建一个自定义触发器(StopTrigger)是否是一个好主意,该触发器使用WebJobsShutdownWatcher类在webjob停止之前触发操作?

Is it a good idea to create a custom trigger (StopTrigger) that used the WebJobsShutdownWatcher class to fire action before the webjob stops ?

推荐答案

确定答案是在问题中:

是的,我可以使用 WebJobsShutdownWatcher 类,因为它具有Register函数,该函数在取消令牌被取消时(即在Webjob停止时)被调用.

Yes I can use the WebJobsShutdownWatcher class because it has a Register function that is called when the cancellation token is canceled, in other words when the webjob is stopping.

static void Main()
{
    var cancellationToken = new WebJobsShutdownWatcher().Token;
    cancellationToken.Register(() =>
    {
        Console.Out.WriteLine("Do whatever you want before the webjob is stopped...");
    });

    var host = new JobHost();
    // The following code ensures that the WebJob will be running continuously
    host.RunAndBlock();
}

编辑(基于马修的评论):

EDIT (Based on Matthew comment):

如果使用触发函数,则可以在函数签名中添加CancellationToken参数.当主机自动关闭时,运行时将取消该令牌,从而使您的函数可以接收通知.

If you use Triggered functions, you can add a CancellationToken parameter to your function signatures. The runtime will cancel that token when the host is shutting down automatically, allowing your function to receive the notification.

public static void QueueFunction(
        [QueueTrigger("QueueName")] string message,
        TextWriter log,
        CancellationToken cancellationToken)
{
    ...
    if(cancellationToken.IsCancellationRequested) return;
    ...
}

这篇关于Azure触发的Webjob-检测Webjob何时停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 06:47