Azure Triggered Webjob - detection while stopping webjob

I am developing a running website that uses TimerTrigger .

Before the website stops, I will need to delete some objects, but I do not know how to cause the “stop the web game”.

Having the function NoAutomaticTrigger, I know that I can use the WebJobsShutdownWatcher class to handle when the webjob stops, but with the Job Start I need help ...

I looked at Extensible Triggers and Bindings with the Azure WebJobs SDK 1.1.0-alpha1 .

Is it possible to create a custom trigger (StopTrigger) that used the class WebJobsShutdownWatcherto trigger an action before the webjob stopped?

+4
source share
2 answers

Ok The answer was in the question:

Yes, I can use the WebJobsShutdownWatcher class because it has a function Registerthat is called when the cancel marker is canceled, in other words, when the webjob stops.

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's comment):

If you use Triggered functions, you can add a parameter CancellationTokento your function signatures. Runtime cancels this token when the host automatically shuts down, which allows your function to receive a notification.

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

, WebJobs SDK, WebJobShutdownWatcher, .

( , WebJobsShutdownWatcher), , %WEBJOBS_SHUTDOWN_FILE%. , webjob, ( 5 , 30 ), .

, SDK Azure WebJobs, WebJobsShutdownWatcher, , - Azure Web Job, , , .

, , , : https://github.com/projectkudu/kudu/wiki/WebJobs#graceful-shutdown

0

Source: https://habr.com/ru/post/1627070/


All Articles