8

I don't get the mechanics behind the cancellation token and the web jobs.

I know I can use Microsoft.Azure.WebJobs.WebJobsShutdownWatcher().Token to get the token and react on token.IsCancellationRequested eg when the WebJobs are updated.

Scenario: Continuous job, triggered by a service bus message. The job calls a method in my data layer. This method does some updates on different tables on an azure sql database. This method runs for about two minutes.

Now I would pass the token to the data layer and there I would do my work while no cancellation is requested; else I would end my updates.

Now the questions:

Does the job host wait for my method until it is finished and then stops?

Do I have to set the "stopping_wait_time" to a value (what is the default anyway?) high enough to be sure my job finishes properly?

If a new message is written to the service bus queue, does this message trigger a new job despite the fact that a cancellation is pending?

Thanks for any clarifications!

Selva
  • 338
  • 2
  • 12
Herr häck
  • 215
  • 2
  • 12

1 Answers1

6

No the WebJob runtime does not wait for your method to complete. The cancellation token that you can retrieve using the WebJobShutdownWatcher only allows you to be notified that the webjob is stopping. It is only allowing a bit of time to your code to shutdown properly.

The default wait time for continuous webjobs is 5 seconds, but you can increase it using the "stopping_wait_time" settings, as you suggested.

For what I have seen, if your webjob is stopping, it does not accept new message triggers anymore, until it is restarted. But I did not find anything confirming this point in the documentation.

Graceful shutdown for webjobs is described here: https://github.com/projectkudu/kudu/wiki/Web-Jobs

Hope this helps,

Julien

Julien Corioland
  • 1,115
  • 6
  • 9
  • Thank you for the help, you confirmed my vague understanding. – Herr häck Dec 30 '15 at 14:34
  • You're welcome ! Did you have another question on this subject ? If not, could you please mark the question as answered so other users will know an answer is available :) thx ! – Julien Corioland Dec 30 '15 at 15:10
  • 3
    Note that you don't need to new up your own `WebJobsShutdownWatcher`. The SDK does that automatically for you. You can simply add a `CancellationToken` to your function signature - the token the runtime passes into your function is from a linked token source that is also monitoring the shutdown file. More info on SDK graceful shutdown [here](https://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-webjobs-sdk-storage-queues-how-to/#graceful) – mathewc Dec 30 '15 at 15:39
  • 1
    You can find a full sample of WebJobs SDK Graceful Shutdown and CancellationToken handling [here](https://github.com/mathewc/samples/blob/master/WebJobSamples/ContinuousJobGracefulShutdown/readme.md). It shows how to configure the `stopping_wait_time` value for your job and how to monitor the CancellationToken for cancellation. – mathewc Dec 31 '15 at 18:31