如何取消 azure 持久函数中的 运行 触发器函数?

How to cancel a running trigger function within azure durable functions?

我正在尝试扇出、扇入模式。这是我的代码

[FunctionName("af_cancellation")]
        public static async Task<string> RunOrchestrator(
            [OrchestrationTrigger] DurableOrchestrationContext context, ILogger log)
        {

            var taskList = new List<Task<string>>();
            var tokenSource = new CancellationTokenSource();

            taskList.Add(context.CallActivityAsync<string>("af_cancellation_Hello", new { ct = tokenSource.Token, city = "Tokyo" }));
            taskList.Add(context.CallActivityAsync<string>("af_cancellation_Hello", new { ct = tokenSource.Token, city = "Seattle" }));
            taskList.Add(context.CallActivityAsync<string>("af_cancellation_Hello", new { ct = tokenSource.Token, city = "London" }));


            try
            {
                await Task.WhenAll(taskList);
            }
            catch (FunctionException)
            {
                log.LogError("trigger function failed");
                tokenSource.Cancel();
            }

            string output = "";
            foreach (var t in taskList)
            {
                output += t.Result;
            }
            return output;
        }

如果其中任何一个抛出异常,我想取消 taskList 中的所有任务。我注意到的是 await Task.WhenAll 在继续之前完成所有任务。

下面是示例触发函数

[FunctionName("af_cancellation_Hello")]
        public static string SayHello([ActivityTrigger] DurableActivityContext context, ILogger log)
        {
            var data = context.GetInput<dynamic>();
            var name = (string)data.city;
            // unable to de-serialize cancellation token here but we'll ignore that. 
            var ct = JsonConvert.DeserializeObject<CancellationToken>(data.ct);
            if (name != "London")
            {
                System.Threading.Thread.Sleep(1000 * 30);
            }
            else
            {
                System.Threading.Thread.Sleep(1000 * 10);
                throw new FunctionException("don't like london");
            }
            log.LogInformation($"Saying hello to {name}.");
            return $"Hello {name}!";
        }

我怎样才能做到这一点?

根据 this 我认为这是不可能的。如果您需要从其他成功的活动中撤消作业,则必须查看 Saga 模式并启动补偿活动。

更多信息:

https://microservices.io/patterns/data/saga.html https://www.enterpriseintegrationpatterns.com/patterns/conversation/CompensatingAction.html