0

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

[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}!";
        }

我怎样才能做到这一点?

4

1 回答 1

0

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

更多信息:

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

于 2020-01-23T19:09:05.503 回答