Add another variable for saving the status of the child logic app and initialize the variable with an empty value.
Add a new step, use Until loop and select the Azure Monitor Logs, we will select Run query and list results action. Subsequently, fill the details.
After filling in the details, use delay connector. Why? If you’re trying to fetch the log data of the logic app in run time it takes a few minutes to reflect the log data in the log analytics and to fetch the data from log analytics. The delay happens due to multiple reasons. One of the reasons is due to the Azure region selected, using until loop without a delay connector speeds up the execution without fetching the log records as there are no records available at that time.
We will set the query at the end and see how it works.
I added a delay connector with four seconds of delay. You, on the other hand, can input the delay time based on your expectation and analysis.
Insert a new step to add action set variable that comes under variable connector.
Select status from the dropdown list.
Now, when you will click on value field, you will see the dynamic content appearing, thanks to the AI (Artificial intelligence) function available in the logic app. Scroll down to find Run query and list results section.
Search for the status in search dynamic content field, to see status_s in Run query and list results. Select the status_s and you will find foreach loop and variable status in it. Again, thanks to artificial intelligence in the logic app that understands what you’re tying to do.
The Until loop section fetches the logs from log analytics and stores the status_s in status variable. Now, modify the query in Run query and list results for fetching the only row that is required.
AzureDiagnostics
| where resource_runId_s==”ChildApp Correlationid”
| where OperationName =="Microsoft.Logic/workflows/workflowRunCompleted"
Here in the code (above), resource_runId_s is the unique identifier of the logic app run. We will use this identifier variable and to determine the final status of the child app run. I have used operationName as the final indicator that says logic app has completed a particular run and I will have the status of the logic app.
After modifying the query based on our expectation (only one row that shows the logic app run is completed) we will add conditions in until loop.
How long will this loop go on?
In the image below I have applied a condition that ends the loop following the change in the value of the status variable.
equals(variables('Status'), 'empty') == false
Once the until loop condition is completed check for the “succeeded” status in the status variable.
Add if else condition to check the status of child logic app in order to terminate the parent logic app.
Select a value from the dynamic content (status variable) and match it with string Succeeded.
If it matches with succeeded string it will terminate successfully, if it receive a different status (i.e. Cancelled, failed.) the parent app will terminate as Failed.
"@variables('Status')"=="Succeeded"
In if true condition add and action named terminate and select the status from the drop down list of the terminate action. Then click succeeded. If false we will have Failed termination status.
In the overview of the logic app you can see the run history with failed and succeeded status based on child logic app run.
................Keep Learning !!!