icube
icube

Reputation: 2808

Azure WebJob: stop trigger when create/overwrite a blob in the same container

i have a webjob that resize and overwrites the same image in the blob container. How can i prevent the webjob from triggering again on the same file? i'm using the imageresizer library btw

public class Functions
{
    public static void ProcessBlob(
        [BlobTrigger("media/{filename}")] Stream input,
        [Blob("media/{filename}", FileAccess.Write)] Stream output,
        string filename,
        ILogger logger)
    {
        logger.LogInformation("blob path: {0}", filename);
        var instructions = new Instructions
        {
            Width = 1920,
            Mode = FitMode.Max,
            Scale = ScaleMode.DownscaleOnly,
            JpegQuality = 80
        };
        ImageBuilder.Current.Build(new ImageJob(input, output, instructions));
    }
}

Upvotes: 0

Views: 284

Answers (1)

Imre Pühvel
Imre Pühvel

Reputation: 4994

A recursive method needs an exit condition.

The simplest solution is to write converted files to a different container, ex media-resized:

public static void ProcessBlob(
    [BlobTrigger("media/{filename}")] Stream input,
    [Blob("media-resized/{filename}", FileAccess.Write)] Stream output,
    string filename,
    ILogger logger) { ... }

Alternative is to write to the same container but to a different file with a recognizable suffix (ex: {filename}.1920px.jpg) and add condition to your function to stop processing files with that suffix.

Should you really-really want in-place upgrade then you could also set custom "converted" marker in blob metadata on conversion and let the function check it before conversion to avoid recursion.

Note that for the last 2 options you most likely need to bind to CloudBlockBlob instead of Stream. See documentation for options.

Upvotes: 2

Related Questions