How to programmatically mark an Azure WebJob as failed?

Is there a way to mark a WebJob (called, not continuous) as failing without throwing an exception? I need to check that certain conditions are correct in order to mark the job as successful.

+3


source to share


4 answers


According to Azure WebJob SDK , Code from TriggeredFunctionExecutor class.

public async Task<FunctionResult> TryExecuteAsync(TriggeredFunctionData input, CancellationToken cancellationToken)
{

    IFunctionInstance instance = _instanceFactory.Create((TTriggerValue)input.TriggerValue, input.ParentId);
    IDelayedException exception = await _executor.TryExecuteAsync(instance, cancellationToken);
    FunctionResult result = exception != null ?
        new FunctionResult(exception.Exception)

        : new FunctionResult(true);
    return result;  
}

      

We know that the WebJobs status depends on whether your WebJob / Function is being executed without any exceptions. We cannot set the final status of a running WebJob programmatically.

I need to check that certain conditions are correct in order to mark the job as successful.



Throwing an exception is the only way I've found. Or, you can store the result of the webjob in an additional location (for example, Azure Table Storage). We can get the current call id of the ExecutionContext class. On your web blog, you can save the current Call ID and the status you want to use in Azure Table Storage. You can query the status later if you need from Azure Table Storage based on Call ID.

public static void ProcessQueueMessage([QueueTrigger("myqueue")] string message, ExecutionContext context, TextWriter log)
{
    log.WriteLine(message);
    SaveStatusToTableStorage(context.InvocationId, "Fail/Success");
}

      

To use the ExecutionContext as a parameter, you need to install the Azure WebJobs SDK extensions using NuGet and call the UserCore method before launching the WebJob.

var config = new JobHostConfiguration();
config.UseCore();
var host = new JobHost(config);
host.RunAndBlock();

      

+3


source


Throwing an unmanaged exception will result in unsuccessful execution. But I noticed that this would also lead to poor management of your post: i.e. Your post will be removed, but won't be pushed to your poison queue regarding your config (but it might have something to do with my SDK version).



+1


source


@Jean NETR-VALERE, newer versions of WebJobs packages act as you say and if an exception is thrown the job will fail and will run over and over and over again until you finally clear your queue. This is absolutely terrible behavior and I have no idea why they changed it.

Yes, they changed it to work that way, because I am using an older version of the webjobs package for this very reason. About 3 months ago I upgraded to a newer version and soon after that I couldn't figure out why the above behavior was happening. After I went back to the old version, it started working correctly again and after failure 5 times moves to the poisoning queue and never starts again. My point is that if you want the correct behavior (IMO), see if you can go back to using version 1.1.0 and you will be happy. Hope this helps.

0


source


To mark a running web job as unsuccessful, you just need to set a non-zero process exit code.

System.Environment.ExitCode = 1;

      

When you throw an unhandled exception, it also sets an exit code so Azure detects the error.

0


source







All Articles