Azure WebJob Continuous Start Notification for NoAutomaticTrigger Jobs

All,

I am migrating my existing worker role code to work with Azure Web. I am trying to use the WebJob SDK (1.0) so that I have full integration with an Azure website.

My complication is that JobHost doesn't play well with jobs outside of regular attribute based functions (Queues, Blobs, etc.).

I already have some standard code that I cannot change to listen to Azure Queues / Topics, etc., so I cannot use the WebJob code to do this.

So I need to use the WebJob Call method:

var cancelTokenSource = new CancellationTokenSource();
var onStartMethod = typeof(Program).GetMethod("OnStart", BindingFlags.Static | BindingFlags.Public);

host.CallAsync(onStartMethod, cancelTokenSource.Token)
    .ConfigureAwait(false)
    .GetAwaiter()
    .GetResult();

      

NB: I am using my own CallAsync as all advice is to use ConfigureAwait (false) when using libraries, but the internals of the JobHost do not, so I copy the "call" code myself, but using ConfigureAwait. As far as I can tell, the cancellation token does nothing in the JobHost.

My problem is that I then need to call host.RunAndBlock (); to terminate the job, which is good, but then I need to run some cleanup. I cannot make a new call to "CallAsync" using the OnStop method because the host has already been canceled, so all I can do is call my OnStop method directly. Unfortunately I am losing the ability to write to the WebSite log via the provided TextWriter class.

I think I need the JobHost to call my method on the RunAndBlock, so I can pick up the cancellation token that was fired when the host shut down and then execute my cleanup code .... but there doesn't seem to be.

Is there an obvious way I'm missing? JobHost seems to be very bad at handling scripts outside of its norm :(

+3


source to share


2 answers


As the winner said, you can use Microsoft.Azure.WebJobs.WebJobsShutdownWatcher
This is the implementation of the Amit solution: WebJobs Graceful Shutdown

So I found the solution:
No change in Program.cs

class Program
{
    static void Main()
    {
        var host = new JobHost();
        host.Call(typeof(Startup).GetMethod("Start"));
        host.RunAndBlock();
    }
}

      



graceful shutdown occurs in Startup.cs:

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
        }
    }
}

      

After the while loop, you can also stop running tasks.

+4


source


NB: I am using my own CallAsync as all advice is to use ConfigureAwait (false) when using libraries, but the internals of the JobHost do not, so I copy the "call" code myself, but using ConfigureAwait. As far as I can tell, the cancellation token does nothing in the JobHost.

This was expected because you are passing your own cancellation token, which nobody has canceled. The webjobs cancellation token is canceled when a shutdown notification is sent or the host is stopped / removed. If you want to get a reference to the cancellation token from outside the webjob function, you must store it in a static variable or something similar.

If you want to use your own cancellation token, you can use Microsoft.Azure.WebJobs.WebJobsShutdownWatcher

to get disconnected notification.



My problem is that I then need to call host.RunAndBlock (); to terminate the job, which is good, but then I need to run some cleanup. I cannot make a new call to "CallAsync" using the OnStop method because the host has already been canceled, so all I can do is call my OnStop method directly. Unfortunately I am losing the ability to write to the WebSite log via the provided TextWriter class.

There is no ready-made solution for this, but you can call the cleanup function from the running webjob (if any). If you need cleanup outside of a function and you also want to log, you can only use console logging - write to the console. The logs will appear in the WebJobs dashboard on the webJob page. I'm curious if you need cleanup outside of a function, what's the scenario?

+1


source







All Articles