Webjob is causing performance issue with ASP.NET core - azure

I created a .NET CORE console application and uploaded as a continuous mode webjob to an Azure application (ASP.NET Core). With webjob running, webapp is very slow responding to API request (Request time rise to few seconds).
WebJob code
static void Main(string[] args)
{
queueClient.RegisterMessageHandler(
async (message, token) =>
{
// Process the message
// Complete the message so that it is not received again.
// This can be done only if the queueClient is opened in ReceiveMode.PeekLock mode.
await queueClient.CompleteAsync(message.SystemProperties.LockToken);
},
new MessageHandlerOptions(exce => {
return Task.CompletedTask;
})
{ MaxConcurrentCalls = 1, AutoComplete = false });
//Console.ReadKey();
while (true) ;
}
And the processing the message operation takes few seconds.
from SCM console
Request time

The while(true) ; will pin the CPU so I suggest you don't do that.
Check the Queue message handling example for the proper way how to implement message handling: https://github.com/Azure/azure-webjobs-sdk/wiki/Queues
You will have to change your Main to:
static void Main(string[] args)
{
JobHost host = new JobHost();
host.RunAndBlock();
}
And then you can make a message handler in another file:
public static void ProcessQueueMessage([QueueTrigger("logqueue")] string logMessage, TextWriter logger)
{
logger.WriteLine(logMessage);
}
The 3.0.0 preview versions of the Microsoft.Azure.WebJobs library support .NET Standard 2.0, so it can be used with .NET Core 2.0 projects.
If you want to implement it yourself, you can check the Webjobs SDK source code for an example how they do it: https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs.Host/JobHost.cs

Related

Azure Web App Windows Container Terminated before gracefull shutdown

I've created a .NET Framework Console program which starts and runs some code, then upon exit it should logout of any external services before exiting (gracefully shutdown).
Here is a sample program:
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace delayed_shutdown
{
class Program
{
public enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWON_EVENT
}
[DllImport("Kernel32")]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine handler, bool Add);
public delegate bool HandlerRoutine(CtrlTypes CtrlType);
public static volatile HandlerRoutine handlerRoutine = new HandlerRoutine(ConsoleCtrlCheck), true)
public static volatile ManualResetEvent exitEvent = new ManualResetEvent(false);
public static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
{
switch (ctrlType)
{
case CtrlTypes.CTRL_C_EVENT:
Console.WriteLine("CTRL_C received");
exitEvent.Set();
return true;
case CtrlTypes.CTRL_CLOSE_EVENT:
Console.WriteLine("CTRL_CLOSE received");
exitEvent.Set();
return true;
case CtrlTypes.CTRL_BREAK_EVENT:
Console.WriteLine("CTRL_BREAK received");
exitEvent.Set();
return true;
case CtrlTypes.CTRL_LOGOFF_EVENT:
Console.WriteLine("CTRL_LOGOFF received");
exitEvent.Set();
return true;
case CtrlTypes.CTRL_SHUTDOWON_EVENT:
Console.WriteLine("CTRL_SHUTDOWN received");
exitEvent.Set();
return true;
default:
return false;
}
}
static int Main(string[] args)
{
if (!SetConsoleCtrlHandler(handlerRoutine))
{
Console.WriteLine("Error setting up control handler... :(");
return -1;
}
Console.WriteLine("Waiting for control event...");
exitEvent.WaitOne();
var i = 60;
Console.WriteLine($"Exiting in {i} seconds...");
while (i > 0)
{
Console.WriteLine($"{i}");
Thread.Sleep(TimeSpan.FromSeconds(1));
i--;
}
Console.WriteLine("Goodbye");
return 0;
}
}
}
I would have expected Windows Containers running as Azure App Service to trigger "docker stop" like function, which would send SIGTERM to my application.
But what happens is that Azure Web App Windows Container is terminated, after 1 sec of trying to stop the container. How do ask Azure Web App to wait X number of seconds before terminating the windows container?
We are currently working on signaling the process upon stop for Windows Containers on Azure App Service.
In Azure App Service we will default to 5 seconds for waiting for a container to exit upon shutdown but we will allow this to be configurable using the following app setting: WEBSITES_CONTAINER_STOP_TIME_LIMIT and we will allow to wait up to 2 min (WEBSITES_CONTAINER_STOP_TIME_LIMIT=00:02:00)
This capability will be deployed in the next update rollout and I hope to be available worldwide early next year and once it is out we will update our docs too, so please stay tuned.
Intercepting the SIGTERM event is something that isn't currently supported. Since App Service is tailored to HTTP workloads, I am curious though as to the reasoning of having a console app pick up such an event. If you could elaborate further, there may be an alternative such as running your console app as a Web Job instead.

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 ?
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
}
Please make sure you stop the slot, otherwise it will be in the running state (can receive message). If you don’t want it to receive information, you should stop it.

How to show event message in Azure Service Fabric Explorer

I'm new to Azure Service Fabric. I follow the tutorial to create hello demo service for Stateless.
It's simple service and I can find the Event Message in local VS IDE Diagnostic Events to show the message that I print
ServiceEventSource.Current.ServiceMessage(this.Context, "Working-{0}", ++iterations);
Like below picture:
But I can't see any log for cluster manager explorer.
Is it possible to show the event log in this explorer? How to do it?
There are my demo event source class code;
[NonEvent]
public void Message(string message, params object[] args)
{
if (this.IsEnabled())
{
string finalMessage = string.Format(message, args);
Message(finalMessage);
}
}
private const int MessageEventId = 1;
[Event(MessageEventId, Level = EventLevel.Informational, Message = "{0}")]
public void Message(string message)
{
if (this.IsEnabled())
{
WriteEvent(MessageEventId, message);
}
}
Pretty sure that currently the Service Fabric Explorer (SFX) only shows node level events and not application specific events.
According to the resent 7.0 release announcement (https://techcommunity.microsoft.com/t5/Azure-Service-Fabric/Service-Fabric-7-0-Release/ba-p/1015482) work is ongoing to display application specific events in SFX

Azure web jobs - parallel message processing from queues not working properly

I need to provision SharePoint Online team rooms using azure queues and web jobs.
I have created a console application and published as continuous web job with the following settings:
config.Queues.BatchSize = 1;
config.Queues.MaxDequeueCount = 4;
config.Queues.MaxPollingInterval = TimeSpan.FromSeconds(15);
JobHost host = new JobHost();
host.RunAndBlock();
The trigger function looks like this:
public static void TriggerFunction([QueueTrigger("messagequeue")]CloudQueueMessage message)
{
ProcessQueueMsg(message.AsString);
}
Inside ProcessQueueMsg function i'm deserialising the received json message in a class and run the following operations:
I'm creating a sub site in an existing site collection;
Using Pnp provisioning engine i'm provisioning content in the sub
site (lists,upload files,permissions,quick lunch etc.).
If in the queue I have only one message to process, everything works correct.
However, when I send two messages in the queue with a few seconds delay,while the first message is processed, the next one is overwriting the class properties and the first message is finished.
Tried to run each message in a separate thread but the trigger functions are marked as succeeded before the processing of the message inside my function.This way I have no control for potential exceptions / message dequeue.
Tried also to limit the number of threads to 1 and use semaphore, but had the same behavior:
private const int NrOfThreads = 1;
private static readonly SemaphoreSlim semaphore_ = new SemaphoreSlim(NrOfThreads, NrOfThreads);
//Inside TriggerFunction
try
{
semaphore_.Wait();
new Thread(ThreadProc).Start();
}
catch (Exception e)
{
Console.Error.WriteLine(e);
}
public static void ThreadProc()
{
try
{
DoWork();
}
catch (Exception e)
{
Console.Error.WriteLine(">>> Error: {0}", e);
}
finally
{
// release a slot for another thread
semaphore_.Release();
}
}
public static void DoWork()
{
Console.WriteLine("This is a web job invocation: Process Id: {0}, Thread Id: {1}.", System.Diagnostics.Process.GetCurrentProcess().Id, Thread.CurrentThread.ManagedThreadId);
ProcessQueueMsg();
Console.WriteLine(">> Thread Done. Processing next message.");
}
Is there a way I can run my processing function for parallel messages in order to provision my sites without interfering?
Please let me know if you need more details.
Thank you in advance!
You're not passing in the config object to your JobHost on construction - that's why your config settings aren't having an effect. Change your code to:
JobHost host = new JobHost(config);
host.RunAndBlock();

How to integration test Azure Web Jobs?

I have a ASP.NET Web API application with supporting Azure Web Job with functions that are triggered by messages added to a storage queue by the API's controllers. Testing the Web API is simple enough using OWIN but how do I test the web jobs?
Do I run a console app in memory in the test runner? Execute the function directly (that wouldn't be a proper integration test though)? It is a continious job so the app doesn't exit. To make matters worse Azure Web Job-functions are void so there's no output to assert.
There is no need to run console app in memory. You can run JobHost in the memory of your integration test.
var host = new JobHost();
You could use host.Call() or host.RunAndBlock(). You would need to point to Azure storage account as webjobs are not supported in localhost.
It depends on what your function is doing, but you could manually add a message to a queue, add a blob or whatever. You could assert by querying the storage where your webjob executed result, etc.
While #boris-lipschitz is correct, when your job is continious (as op says it is), you can't do anything after calling host.RunAndBlock().
However, if you run the host in a separate thread, you can continue with the test as desired. Although, you have to do some kind of polling in the end of the test to know when the job has run.
Example
Function to be tested (A simple copy from one blob to another, triggered by created blob):
public void CopyBlob(
[BlobTrigger("input/{name}")] TextReader input,
[Blob("output/{name}")] out string output)
{
output = input.ReadToEnd();
}
Test function:
[Test]
public void CopyBlobTest()
{
var blobClient = GetBlobClient("UseDevelopmentStorage=true;");
//Start host in separate thread
var thread = new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
var host = new JobHost();
host.RunAndBlock();
});
thread.Start();
//Trigger job by writing some content to a blob
using (var stream = new MemoryStream())
using (var stringWriter = new StreamWriter(stream))
{
stringWriter.Write("TestContent");
stringWriter.Flush();
stream.Seek(0, SeekOrigin.Begin);
blobClient.UploadStream("input", "blobName", stream);
}
//Check every second for up to 20 seconds, to see if blob have been created in output and assert content if it has
var maxTries = 20;
while (maxTries-- > 0)
{
if (!blobClient.Exists("output", "blobName"))
{
Thread.Sleep(1000);
continue;
}
using (var stream = blobClient.OpenRead("output", "blobName"))
using (var streamReader = new StreamReader(stream))
{
Assert.AreEqual("TestContent", streamReader.ReadToEnd());
}
break;
}
}
I've been able to simulate this really easily by simply doing the following, and it seems to work fine for me:
private JobHost _webJob;
[OneTimeSetUp]
public void StartupFixture()
{
_webJob = Program.GetHost();
_webJob.Start();
}
[OneTimeTearDown]
public void TearDownFixture()
{
_webJob?.Stop();
}
Where the WebJob Code looks like:
public class Program
{
public static void Main()
{
var host = GetHost();
host.RunAndBlock();
}
public static JobHost GetHost()
{
...
}
}

Resources