I have implemented a system where FrontEnd upload a photo into a blob container, then Blob fire an event on EventGrid on "BlobCreated" that trigger an Az Function which generate a thumbnail and save it into a MongoDb instance.
I follow the rules Microsoft suggest to avoid cold start, using an Elastic Premium Plan and configure one minimum Instance to be always warm.
I do not have lots of events per seconds, to be honest I could have 30 minutes without any events and then some minutes where users pushes images on blob.
I noticed from the Application Insight logs that if too much time passes between one upload event and another, function need some seconds (3 - 4 seconds) before first log (I have a log at just the begin of my function).
If I upload three photos consecutively, time between upload and function start is some milliseconds.
Function is initialized as following: ThumbnailGenerator.cs
namespace ThumbnailGeneratorV2;
public class ThumbnailGenerator
{
private readonly IReceiptDbService _dbManager;
private readonly IMimeInspector _mimeInspector;
private readonly int _thumbnailDimension;
public ThumbnailGenerator(
IReceiptDbService dbManager,
IMimeInspector mimeInspector)
{
_dbManager = dbManager;
_mimeInspector = mimeInspector;
int.TryParse(Environment.GetEnvironmentVariable("ThumbnailDimension"), out _thumbnailDimension);
}
[FunctionName("ThumbnailGenerator")]
public async Task Run(
[EventGridTrigger] EventGridEvent eventGridEvent,
[Blob("{data.url}", FileAccess.Read, Connection = "AzureWebJobsStorage")]
Stream myBlob,
ILogger log)
{
log.LogInformation("Function Start");
...
}
Startup.cs
[assembly: FunctionsStartup(typeof(ThumbnailGeneratorV2.Startup))]
namespace ThumbnailGeneratorV2;
class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<IMimeInspector, MimeInspector>();
builder.Services.AddSingleton<IReceiptDbService, ReceiptDbService>();
builder.Services.AddSingleton<IDbRepository, DbRepository>();
}
}
P.S. the Az Function is outbounded with a VNET cause database is in a private network.
Someone has some suggestions how to keep the function really alive?
Thanks.