0

My question is similar to the below one.

Notification of when continuous Azure WebJob is stopping for NoAutomaticTrigger type jobs

I have used the idea from Amit's Blog but then hit a little roadblock

I have a file watcher set in the webjob which gets triggered if the webjob is shutdown from the portal.

I need to update a few flags in my storage tables before the webjob is terminated.

The problem is that my code seems to stop at a point where I am trying to retrive a record from storage table. I have exception handler around the below code and no exception message is written on the console.

Below is my code

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("my storage key");
var tableClient = storageAccount.CreateCloudTableClient();
var table = tableClient.GetTableReference("myTable");
TableOperation operation = TableOperation.Retrieve("partKey", "rowKey");
var result = table.Execute(operation); // stucks here
   if (result.Result != null)
     {
        MyEntity entity = (MyEntity)result.Result;
        if (entity != null)
         {
           entity.IsRunning = false; //reset the flag
           TableOperation update = TableOperation.InsertOrReplace(entity);
           table.Execute(update); //update the record
         }
     }

I have increased the stopping_wait_time in settings.job to 300 seconds but still no luck.

Community
  • 1
  • 1
Sandesh
  • 2,966
  • 1
  • 20
  • 34

1 Answers1

0

You could use Microsoft.Azure.WebJobs.WebJobsShutdownWatcher
This is an implementation of Amit solution : WebJobs Graceful Shutdown

So I've found a solution doing this :
No modification in the Program.cs

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

the graceful shutdown goes in your function :

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
        }

       // This code will be executed once the webjob is going to shutdown
       Console.Out.WriteLine("Webjob is shuting down")
    }
}

After the while loop, you could also stop started tasks.

Thomas
  • 24,234
  • 6
  • 81
  • 125