Search code examples
azureazure-functionsazure-web-app-serviceazure-deployment-slots

Staging Slot processing messages for Web App background task


I am using Azure App service plan to host web App which process Service Bus Topic message. I am using Azure Function App also which has http trigger to execute Event grid data. Both Web App(App Service Plan) and Function App(Elastic Premium plan) have staging slots in production.

At the time of swapping slot I observed stgaing slot for web app is processing message. Is this expected behaviour? For function app staging slot, I am not observing this behaviour. Why so ?


Solution

  • Add an App Setting in each slot called "IsStaging" with true and false values then when app service warms up (Startup or method consuming messages) stop requests so messages are not consumed from the Staging slot.

                    if (CurrentConfiguration.IsStaging)
                    {
                       logger.LogWarn("Staging slot cannot consume messages");
                       return;
                    }
    

    UPDATE for WebJob:

            static void Main(string[] args)
            {
                JobHost host = CreateJobHost();
                if (CurrentConfiguration.IsStaging)
                {
                    host.Call(typeof(Program).GetMethod("DoStagingInfiniteLoop"));
                }
                else
                {
                    host.Call(typeof(Program).GetMethod("ProcessQueue"));
                }
            }
    
    
            private static JobHost CreateJobHost()
            {
                JobHostConfiguration jobHostConfig = new JobHostConfiguration();
                jobHostConfig.DashboardConnectionString = "DashboardCS";
                jobHostConfig.StorageConnectionString = "StorageCS";
    
                var JobHost = new JobHost(jobHostConfig);
                return JobHost;
            }
    
    
            [NoAutomaticTrigger]
            public static void DoStagingInfiniteLoop(TextWriter logger, CancellationToken token)
            {
                const int LOOP_TRACE_INTERVAL = 10000;
                ProcessLogger.WriteTrace("This is a staging environment, waiting...");
                while (true)
                {
                    Task.Delay(LOOP_TRACE_INTERVAL).Wait(token);
                }
            }
    
            [NoAutomaticTrigger]
            public static void ProcessQueue(TextWriter logger, CancellationToken token)
            {
                //Your processing code here
            }