RabbitMQ 和 Webjob SDK。故意失败的功能

RabbitMQ and Webjob SDK. Intentionally failing function

我有一个正在侦听 rabbitMQ 队列的 Web 作业,它将选择队列中的消息并 post 到另一个服务。

但是,如果服务失败(returns 5xx 错误或类似错误),我想将消息放回队列中,稍后再试。

我正在使用 rabbitmq 扩展到 webjob sdk (https://github.com/Sarmaad/WebJobs.Extensions.RabbitMQ/tree/master/WebJobs.Extensions.RabbitMQ),如果我理解正确的话,如果 webjob "function" 失败,就会发生这种情况。有没有办法故意使它失败? (抛出异常除外)

来自扩展源代码:

            var result = _executor.TryExecuteAsync(new TriggeredFunctionData{TriggerValue = triggerValue}, CancellationToken.None).Result;

            if (result.Succeeded)
                _channel.BasicAck(args.DeliveryTag, false);
            else
                _channel.BasicNack(args.DeliveryTag, false, false);

根据Azure WebJob SDK,我们知道WebJobs的状态取决于你的WebJob/Function是否无异常执行。我们无法以编程方式设置 运行 WebJob 的最终状态。

来自 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;  
}

Is there a way of failing it intentionally? (Other than throwing exceptions)

所以你的问题的答案是否定的。抛出异常是唯一的方法。