【问题标题】:Azure Durable Orchestration - Raise event after the orchestration is completedAzure 持久编排 - 编排完成后引发事件
【发布时间】:2021-09-08 00:42:41
【问题描述】:

我使用 Azure 编排,我想在编排完成后引发一个事件:

    [FunctionName(nameof(RunComputingQueueUpdate))]
    public async Task RunComputingQueueUpdate(
        [QueueTrigger("test-route", Connection = "TestQueue:QueueConnectionString")] string message,
        [DurableClient] IDurableOrchestrationClient starter)
    {
        var command = queueCommunicator.Read<MyCommand>(message);
        var instanceId = await starter.StartNewAsync(nameof(RunOrchestratorComputing), command.PartitionKey, command);

        // How can I wait here ?
        await starter.WaitForCompletionOrCreateCheckStatusResponseAsync(new HttpRequestMessage(), instanceId);

        if (!string.IsNullOrEmpty(command.EventInstanceId) && !string.IsNullOrEmpty(command.EventName))
        {
            await starter.RaiseEventAsync(command.EventInstanceId, command.EventName, command.EventParam);
        }
    }

我想等待管弦乐队完成,但我不知道如何创建我的 HttpRequest 来执行此操作。 我需要使用 client.CreateHttpManagementPayload(instanceId) 吗?

提前致谢!

【问题讨论】:

    标签: azure-functions orchestration


    【解决方案1】:

    好吧,除了等待编排完成的 while 循环之外,我找不到任何其他解决方案:

        [FunctionName(nameof(RunComputingQueueUpdate))]
        public async Task RunComputingQueueUpdate(
            [QueueTrigger("test-route", Connection = "TestQueue:QueueConnectionString")] string message,
            [DurableClient] IDurableOrchestrationClient starter)
        {
            Contract.Assume(starter != null);
            if (string.IsNullOrEmpty(message))
                throw new ArgumentNullException(nameof(message));
    
            var command = queueCommunicator.Read<MyCommand>(message);
    
            // Check if an instance with the specified ID already exists or an existing one stopped running(completed/failed/terminated).
            var existingInstance = await starter.GetStatusAsync(command.PartitionKey);
            if (existingInstance == null
                || existingInstance.RuntimeStatus == OrchestrationRuntimeStatus.Completed
                || existingInstance.RuntimeStatus == OrchestrationRuntimeStatus.Failed
                || existingInstance.RuntimeStatus == OrchestrationRuntimeStatus.Terminated)
            {
                // An instance with the specified ID doesn't exist or an existing one stopped running, create one.
                var instanceId = await starter.StartNewAsync(nameof(RunOrchestratorComputing), command.PartitionKey, command);
    
                // Wait the task is completed/failed/terminated
                var status = await starter.GetStatusAsync(instanceId);
                while (status.RuntimeStatus == OrchestrationRuntimeStatus.Running
                    || status.RuntimeStatus == OrchestrationRuntimeStatus.Pending)
                {
                    await Task.Delay(200);
                    status = await starter.GetStatusAsync(instanceId);
                }
    
                // Raise the event
                if (!string.IsNullOrEmpty(command.EventInstanceId) && !string.IsNullOrEmpty(command.EventName))
                {
                    await starter.RaiseEventAsync(command.EventInstanceId, command.EventName, eventData:command.EventParam);
                }
    
            }
            else
            {
                // Exception will re-enqueue the command (wait 10min by default and re-enqueue the command
                throw new Exception("Instance already running");
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 2019-09-12
      • 2015-05-21
      • 1970-01-01
      • 2023-03-30
      • 2016-06-14
      • 1970-01-01
      • 2011-09-08
      • 2023-03-19
      相关资源
      最近更新 更多