Exemplo n.º 1
0
        private async Task <IActionResult> FindOrchestrations(
            HttpRequest req,
            IDurableOrchestrationClient client,
            IEnumerable <OrchestrationRuntimeStatus> runtimeStatus,
            DateTime from,
            DateTime to,
            bool showInput = false)
        {
            // Define the cancellation token.
            CancellationTokenSource source = new CancellationTokenSource();
            CancellationToken       token  = source.Token;

            var instances = await client.ListInstancesAsync(
                new OrchestrationStatusQueryCondition
            {
                CreatedTimeFrom = from,
                CreatedTimeTo   = to,
                RuntimeStatus   = runtimeStatus,
                ShowInput       = showInput
            },
                token
                );

            return(new OkObjectResult(instances));
        }
Exemplo n.º 2
0
        public virtual async Task <string> runAction(IDurableOrchestrationClient starter, ILogger log)
        {
            var entLookup = Environment.GetEnvironmentVariable("LCU-ENTERPRISE-LOOKUP");

            try
            {
                var listInstanceQuery = new OrchestrationStatusQueryCondition()
                {
                    PageSize      = 1000,
                    RuntimeStatus = new[] { OrchestrationRuntimeStatus.Running, OrchestrationRuntimeStatus.Pending }
                };

                var instances = await starter.ListInstancesAsync(listInstanceQuery, new System.Threading.CancellationToken());

                var running = new HashSet <string>(instances.DurableOrchestrationState.Select(instance => instance.InstanceId));

                var terminateTasks = running.Select(instanceId =>
                {
                    return(starter.TerminateAsync(instanceId, "Cleanup"));
                });

                await Task.WhenAll(terminateTasks);

                while (running.Count > 0)
                {
                    var results = await Task.WhenAll(instances.DurableOrchestrationState.Select(instance => starter.GetStatusAsync(instance.InstanceId)));

                    foreach (var status in results)
                    {
                        // Remove any terminated or completed instances from the hashset
                        if (status != null &&
                            status.RuntimeStatus != OrchestrationRuntimeStatus.Pending &&
                            status.RuntimeStatus != OrchestrationRuntimeStatus.Running)
                        {
                            running.Remove(status.InstanceId);
                        }
                    }

                    await Task.Delay(TimeSpan.FromMilliseconds(1000));
                }

                var instanceId = await starter.StartAction("RenewCertificatesOrchestration", new StateDetails()
                {
                    EnterpriseLookup = entLookup
                }, new ExecuteActionRequest()
                {
                    Arguments = new
                    {
                        EnterpriseLookup = entLookup
                    }.JSONConvert <MetadataModel>()
                }, log);

                return(instanceId);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Exemplo n.º 3
0
        public static async Task <IEnumerable <DurableOrchestrationStatus> > GetInstancesAsync(this IDurableOrchestrationClient durableOrchestrationClient)
        {
            var instances = await durableOrchestrationClient.ListInstancesAsync(
                new OrchestrationStatusQueryCondition(),
                CancellationToken.None
                );

            return(instances.DurableOrchestrationState);
        }
        public static async Task Run([DurableClient] IDurableOrchestrationClient client)
        {
            var all = await client.ListInstancesAsync(new OrchestrationStatusQueryCondition
            {
                RuntimeStatus = new[] { OrchestrationRuntimeStatus.Pending, OrchestrationRuntimeStatus.Running, OrchestrationRuntimeStatus.ContinuedAsNew }
            }, CancellationToken.None);

            await Task.WhenAll(all.DurableOrchestrationState.Select(async o => await client.TerminateAsync(o.InstanceId, "Clean up test data.")));
        }
Exemplo n.º 5
0
        public static async Task Run([DurableClient] IDurableOrchestrationClient client)
        {
            var failed = await client.ListInstancesAsync(new OrchestrationStatusQueryCondition { RuntimeStatus = new[] { OrchestrationRuntimeStatus.Failed } }, CancellationToken.None);

            if (failed.DurableOrchestrationState.Any())
            {
                throw new AggregateException(failed.DurableOrchestrationState.Select(x => new Exception(x.Output.ToString())));
            }
        }
        private async Task <HttpResponseMessage> HandleGetStatusRequestAsync(
            HttpRequestMessage request)
        {
            IDurableOrchestrationClient client = this.GetClient(request);
            var queryNameValuePairs            = request.GetQueryNameValuePairs();

            var condition = new OrchestrationStatusQueryCondition();

            if (TryGetDateTimeQueryParameterValue(queryNameValuePairs, CreatedTimeFromParameter, out DateTime createdTimeFrom))
            {
                condition.CreatedTimeFrom = createdTimeFrom;
            }

            if (TryGetDateTimeQueryParameterValue(queryNameValuePairs, CreatedTimeToParameter, out DateTime createdTimeTo))
            {
                condition.CreatedTimeTo = createdTimeTo;
            }

            if (TryGetIEnumerableQueryParameterValue <OrchestrationRuntimeStatus>(queryNameValuePairs, RuntimeStatusParameter, out IEnumerable <OrchestrationRuntimeStatus> runtimeStatus))
            {
                condition.RuntimeStatus = runtimeStatus;
            }

            if (TryGetBooleanQueryParameterValue(queryNameValuePairs, ShowInputParameter, out bool showInput))
            {
                condition.ShowInput = showInput;
            }

            if (TryGetIntQueryParameterValue(queryNameValuePairs, PageSizeParameter, out int pageSize))
            {
                condition.PageSize = pageSize;
            }

            if (request.Headers.TryGetValues("x-ms-continuation-token", out var headerValues))
            {
                condition.ContinuationToken = headerValues.FirstOrDefault();
            }

            IList <DurableOrchestrationStatus> statusForAllInstances;

            var context = await client.ListInstancesAsync(condition, CancellationToken.None);

            statusForAllInstances = context.DurableOrchestrationState.ToList();
            var nextContinuationToken = context.ContinuationToken;

            var results = new List <StatusResponsePayload>(statusForAllInstances.Count);

            foreach (var state in statusForAllInstances)
            {
                results.Add(ConvertFrom(state));
            }

            var response = request.CreateResponse(HttpStatusCode.OK, results);

            response.Headers.Add("x-ms-continuation-token", nextContinuationToken);
            return(response);
        }
        public async Task Run(
            [TimerTrigger(PurgeFrequencyVariable)] TimerInfo myTimer,
            [DurableClient] IDurableOrchestrationClient client,
            ILogger log,
            CancellationToken hostCancellationToken)
        {
            EnsureArg.IsNotNull(client, nameof(client));
            EnsureArg.IsNotNull(myTimer, nameof(myTimer));
            EnsureArg.IsNotNull(log, nameof(log));

            var orchestrationInstanceIdList = new List <string>();

            log.LogInformation("Purging orchestration instance history at: {Timestamp}", _getUtcNow());
            if (myTimer.IsPastDue)
            {
                log.LogWarning("Current function invocation is later than scheduled.");
            }

            // Specify conditions for orchestration instances.
            var condition = new OrchestrationStatusQueryCondition
            {
                RuntimeStatus     = _purgeConfig.RuntimeStatuses,
                CreatedTimeFrom   = DateTime.MinValue,
                CreatedTimeTo     = _getUtcNow().AddDays(-_purgeConfig.MinimumAgeDays),
                ContinuationToken = null
            };

            do
            {
                OrchestrationStatusQueryResult listOfOrchestrators =
                    await client.ListInstancesAsync(condition, hostCancellationToken);

                condition.ContinuationToken = listOfOrchestrators.ContinuationToken;

                // Loop through the orchestration instances and purge them.
                foreach (DurableOrchestrationStatus orchestration in listOfOrchestrators.DurableOrchestrationState)
                {
                    orchestrationInstanceIdList.Add(orchestration.InstanceId);
                    await client.PurgeInstanceHistoryAsync(orchestration.InstanceId);
                }
            } while (condition.ContinuationToken != null);

            if (orchestrationInstanceIdList.Count != 0)
            {
                log.LogInformation("{Count} Durable Functions cleaned up successfully.", orchestrationInstanceIdList.Count);
                log.LogDebug("List of cleaned instance IDs: {list}", string.Join(", ", orchestrationInstanceIdList));
            }
            else
            {
                log.LogInformation("No Orchestration instances found within given conditions.");
            }
        }
Exemplo n.º 8
0
        public static async Task GetAllStatuses(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req,
            [DurableClient] IDurableOrchestrationClient client,
            ILogger log)
        {
            var noFilter = new OrchestrationStatusQueryCondition();
            var result   = await client.ListInstancesAsync(
                noFilter,
                CancellationToken.None);

            foreach (var instance in result.DurableOrchestrationState)
            {
                log.LogInformation(JsonConvert.SerializeObject(instance));
            }
        }
Exemplo n.º 9
0
        public static async Task AF_WAITINGFORAPPROVAL(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "raiseEvent/{instanceId}")] HttpRequest req, string instanceId, [DurableClient] IDurableOrchestrationClient client,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var noFilter = new OrchestrationStatusQueryCondition();
            OrchestrationStatusQueryResult result = await client.ListInstancesAsync(
                noFilter,
                CancellationToken.None);

            foreach (DurableOrchestrationStatus instance in result.DurableOrchestrationState)
            {
                log.LogInformation(JsonConvert.SerializeObject(instance));
            }

            await client.RaiseEventAsync(instanceId, "WaitingForApproval", true);
        }
Exemplo n.º 10
0
        public static async Task <IActionResult> AF_GETINSTANCES(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "instances/{instanceId}")] HttpRequest req, string instanceId, [DurableClient] IDurableOrchestrationClient client,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var noFilter = new OrchestrationStatusQueryCondition();
            OrchestrationStatusQueryResult result = await client.ListInstancesAsync(
                noFilter,
                CancellationToken.None);

            foreach (DurableOrchestrationStatus instance in result.DurableOrchestrationState)
            {
                log.LogInformation(JsonConvert.SerializeObject(instance.InstanceId));
            }

            return(new OkObjectResult(result.DurableOrchestrationState));
        }
Exemplo n.º 11
0
        public static async Task Wait(this IDurableOrchestrationClient client,
                                      Func <IEnumerable <DurableOrchestrationStatus>, bool> until, CancellationToken token)
        {
            while (!token.IsCancellationRequested)
            {
                var instances = await client.ListInstancesAsync(new OrchestrationStatusQueryCondition
                {
                    RuntimeStatus = new[] { OrchestrationRuntimeStatus.Pending, OrchestrationRuntimeStatus.Running, OrchestrationRuntimeStatus.ContinuedAsNew },
                    TaskHubNames  = new[] { client.TaskHubName }
                }, token);

                if (until(instances.DurableOrchestrationState))
                {
                    break;
                }

                await Task.Delay(TimeSpan.FromMilliseconds(100), token);
            }
        }