azure-webjobs-sdk icon indicating copy to clipboard operation
azure-webjobs-sdk copied to clipboard

WebJobs Shutdown signal not working

Open DRAirey1 opened this issue 2 years ago • 1 comments

WebJobsShutdownWatcher.Token.IsCancellationRequested doesn't work.

Repro steps

Provide the steps required to reproduce the problem

  1. Put the following code in a stock Worker Service template in Visual Studio:
        private readonly ILogger logger;
        private readonly WebJobsShutdownWatcher webJobsShutdownWatcher = new ();

        /// <inheritdoc/>
        public Worker(ILogger<Worker> logger, IHostApplicationLifetime appLifetime)
        {
            this.logger = logger;
            webJobsShutdownWatcher.Token.Register(() => this.logger.LogInformation("Got a shutdown from WebJobsShutdownWatcher!"));
        }

        public override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            this.logger.LogInformation("Starting ExecuteAsync");

            while (!cancellationToken.IsCancellationRequested)
            {
                if (this.webJobsShutdownWatcher.Token.IsCancellationRequested)
                {
                    this.logger.LogInformation("Web Job shutdown requested.");
                    break;
                }

                this.logger.LogInformation($"Doing work at {DateTime.Now}");
                await Task.Delay(10000, this.appLifetime.ApplicationStopping);
            }

            this.logger.LogInformation("Exiting ExecuteAsync");
        }

  1. Shut down the Web Job in the Azure portal.

Expected behavior

You expect "Web Job shutdown requested" event in the log.

Actual behavior

The Web Job stops posting messages to the log, but no indication is ever given that the WebJobsShutdownWatcher signal was received in the worker task.

Package Version: Microsoft.Azure.WebJobs 3.0.31

Deployed directly from Visual Studio to an Azure App Service using the 'Publish' function.

DRAirey1 avatar Apr 05 '22 19:04 DRAirey1

Hi @pragnagopa , Could you please take a look at this issue

v-bbalaiagar avatar Apr 12 '22 09:04 v-bbalaiagar