Пример #1
0
        public async Task <IActionResult> SagaStatusChecker(
            [HttpTrigger(AuthorizationLevel.Function, methods: "get", Route = "saga/state/{id}")] HttpRequestMessage request,
            string id,
            [DurableClient] IDurableOrchestrationClient client,
            [CosmosDB(
                 databaseName: @"%CosmosDbDatabaseName%",
                 collectionName: @"%CosmosDbOrchestratorCollectionName%",
                 ConnectionStringSetting = @"CosmosDbConnectionString")] IDocumentClient documentClient,
            ILogger log)
        {
            Uri collectionUri = UriUtils.CreateTransactionCollectionUri();

            TransactionItem item = documentClient
                                   .CreateDocumentQuery <TransactionItem>(collectionUri)
                                   .ToList()
                                   .Where(document => document.Id == id)
                                   .FirstOrDefault();

            if (item == null)
            {
                return(new NotFoundObjectResult($@"Saga with transaction ID = {id} not found."));
            }

            HttpResponseMessage clientResponse = client.CreateCheckStatusResponse(request, id);

            var durableOrchestratorFactory = new DurableClientFactory(httpClient, log);
            var orchestratorRuntimeStatus  = await durableOrchestratorFactory.GetRuntimeStatusAsync(clientResponse);

            var response = new SagaStatusResponse(item.State.ToString(), orchestratorRuntimeStatus);

            return(new OkObjectResult(response));
        }
Пример #2
0
        public static async Task <TransactionItem> SagaOrchestratorActivity(
            [ActivityTrigger] TransactionItem item,
            [CosmosDB(
                 databaseName: @"%CosmosDbDatabaseName%",
                 collectionName: @"%CosmosDbOrchestratorCollectionName%",
                 ConnectionStringSetting = @"CosmosDbConnectionString")]
            IAsyncCollector <TransactionItem> documentCollector,
            [CosmosDB(
                 databaseName: @"%CosmosDbDatabaseName%",
                 collectionName: @"%CosmosDbOrchestratorCollectionName%",
                 ConnectionStringSetting = @"CosmosDbConnectionString")] IDocumentClient client)
        {
            if (item.State == SagaState.Pending.ToString())
            {
                await documentCollector.AddAsync(item);

                return(item);
            }

            Uri collectionUri = UriUtils.CreateTransactionCollectionUri();

            var document = client
                           .CreateDocumentQuery(collectionUri)
                           .Where(t => t.Id == item.Id)
                           .AsEnumerable()
                           .FirstOrDefault();

            document.SetPropertyValue("state", item.State);
            await client.ReplaceDocumentAsync(document);

            return(item);
        }