How to programmatically mark an Azure WebJob as failed?

僤鯓⒐⒋嵵緔 提交于 2019-12-12 14:34:18

问题


Is there a way to mark a WebJob (triggered, not continuous) as failed, without throwing an exception? I need to check that certain conditions are true to mark the job as successful.


回答1:


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 executed without any exceptions or not. We can't set the finial status of a running WebJob programmatically.

I need to check that certain conditions are true to mark the job as successful.

Throw an exception is the only way I found. Or you could store the webjob execute result in an additional place(For example, Azure Table Storage). We can get the current invocation id by ExecutionContext class. In your webjob, you could save the current invocation id and the status you wanted to an Azure Table Storage. You could query the status later if you needed from Azure Table Storage based on the invocation id.

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

To use ExecutionContext as parameter, you need to install Azure WebJobs SDK Extensions using NuGet and invoke UserCore method before your run your WebJob.

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



回答2:


Throwing an unmanaged exception will result in a Failed execution. But i have noticed that it will also result with a bad management of your message: i.e. your message will be dequeued but not moved to your poison queue regarding your configuration (but maybe it was due to my SDK version).




回答3:


@Jean NETR-VALERE the newer versions of the WebJobs packages do act as you say and if an exception is thrown the job will fail, and will continue to be run over and over and over until you finally clear your queue. This is absolutely horrible behavior and I have no clue why they changed this.

Yes they did change it to make it work this way, because I use an older version of the webjobs package just for this reason. About 3 months ago I upgraded to the newer version, and shortly after could not understand why the above behavior was happening . Once I reverted back to the older version, it started working correctly again and after failing 5 times is moved to poison queue and never ran again. My point is that if you want the correct (IMO) behavior, see if you can go back to using version 1.1.0 and you will be happy. Hope that helps.




回答4:


To mark a triggered web job as failed you just need to set process exit code to non-zero.

System.Environment.ExitCode = 1;

When you throw an unhandled exception it also sets the exit code, that is how Azure determines failure.



来源:https://stackoverflow.com/questions/43062948/how-to-programmatically-mark-an-azure-webjob-as-failed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!