有没有一种方法可以将WebJob(触发的,不连续的)标记为失败而不会引发异常?我需要检查某些条件是否正确才能将作业标记为成功.
解决方法:
根据Azure WebJob SDK,代码来自TriggeredFunctionExecutor类.
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;
}
我们知道WebJobs的状态取决于您的WebJob / Function是否被执行而没有任何异常.我们无法以编程方式设置正在运行的WebJob的最终状态.
I need to check that certain conditions are true to mark the job as successful.
抛出异常是我发现的唯一方法.或者,您可以将webjob执行结果存储在其他位置(例如,Azure表存储).我们可以通过ExecutionContext类获取当前的调用ID.在您的Web作业中,您可以将当前调用ID和所需的状态保存到Azure表存储中.如果需要,可以稍后根据调用ID从Azure Table Storage查询状态.
public static void ProcessQueueMessage([QueueTrigger("myqueue")] string message, ExecutionContext context, TextWriter log)
{
log.WriteLine(message);
SaveStatusToTableStorage(context.InvocationId, "Fail/Success");
}
要将ExecutionContext用作参数,需要在运行WebJob之前使用NuGet安装Azure WebJobs SDK扩展并调用UserCore方法.
var config = new JobHostConfiguration();
config.UseCore();
var host = new JobHost(config);
host.RunAndBlock();