예제 #1
0
        public static async Task <LSAMessage> ActionA2(
            [OrchestrationTrigger] DurableOrchestrationContextBase context)
        {
            var message = context.GetInput <LSAMessage>();


            message = await context.CallActivityAsync <LSAMessage>("StepA", message);

            context.SetCustomStatus(message);
            message = await context.CallActivityAsync <LSAMessage>("StepC", message);

            context.SetCustomStatus(message);


            return(message);
        }
예제 #2
0
        public static async Task <List <string> > Run(
            [OrchestrationTrigger] DurableOrchestrationContextBase context)
        {
            var outputs = new List <string>();

            outputs.Add(await context.CallActivityAsync <string>("E1_SayHello", "Tokyo"));
            context.SetCustomStatus("Tokyo");

            DateTime deadline = context.CurrentUtcDateTime.Add(TimeSpan.FromSeconds(3));
            await context.CreateTimer(deadline, CancellationToken.None);

            outputs.Add(await context.CallActivityAsync <string>("E1_SayHello", "Seattle"));
            context.SetCustomStatus("Tokyo, Seattle");

            await context.CreateTimer(deadline, CancellationToken.None);

            outputs.Add(await context.CallActivityAsync <string>("E1_SayHello", "London"));
            context.SetCustomStatus("Tokyo, Seattle, London");

            // returns ["Hello Tokyo!", "Hello Seattle!", "Hello London!"]
            return(outputs);
        }
예제 #3
0
        public static async Task <List <CartData> > Run([OrchestrationTrigger] DurableOrchestrationContextBase context, TraceWriter log)
        {
            var cart = context.GetInput <List <CartData> >() ?? new List <CartData>();

            var addItemTask     = context.WaitForExternalEvent <CartData>(CartEvents.AddItem);
            var removeItemTask  = context.WaitForExternalEvent <CartData>(CartEvents.RemoveItem);
            var isCompletedTask = context.WaitForExternalEvent <bool>(CartEvents.IsCompleted);
            var setCartReminder = context.WaitForExternalEvent <CartReminderData>(CartEvents.SetReminder);

            // Wait for any external event
            var resultingEvent = await Task.WhenAny(addItemTask, removeItemTask, isCompletedTask, setCartReminder);

            // Add item to cart
            if (resultingEvent == addItemTask)
            {
                cart.Add(addItemTask.Result);
                context.SetCustomStatus("readynow");
                if (!context.IsReplaying)
                {
                    log.Info($"Added {addItemTask.Result.ItemName} to the Shopping Cart.");
                }
            }

            // Remove Item from cart
            else if (resultingEvent == removeItemTask)
            {
                cart.Remove(cart.Find(x => x.ItemId == removeItemTask.Result.ItemId));
                context.SetCustomStatus("readynow");
                if (!context.IsReplaying)
                {
                    log.Info($"Removed {removeItemTask.Result.ItemName} from the Shopping Cart.");
                }
            }

            // Add reminder for cart (used to notify user of waiting cart with items)
            else if (resultingEvent == setCartReminder)
            {
                var provisionTask = context.CallSubOrchestratorAsync("SetCartNotificationTimer", setCartReminder.Result);
                if (!context.IsReplaying)
                {
                    log.Info($"Added timer ({setCartReminder.Result.RemindInMinutes} minutes) for Shopping Cart.");
                }
            }

            // Complete cart or stay running ?
            if (resultingEvent == isCompletedTask && isCompletedTask.Result)
            {
                if (!context.IsReplaying)
                {
                    log.Info("Completed the Shopping Cart.");
                }
            }
            else
            {
                context.ContinueAsNew(cart); // the magic line
                if (!context.IsReplaying)
                {
                    log.Info($"cartList Count: {cart.Count}");
                }
            }

            return(await Task.FromResult(cart));
        }
        public static async Task <OrderResult> ProcessOrder(
            [OrchestrationTrigger] DurableOrchestrationContextBase ctx,
            ILogger log)
        {
            var order = ctx.GetInput <Order>();

            order.OrchestrationId = ctx.InstanceId;

            if (!ctx.IsReplaying)
            {
                log.LogInformation($"Processing order #{order.Id}");
            }

            var total = order.Items.Sum(i => i.Amount);

            await ctx.CallActivityAsync("A_SaveOrderToDatabase", order);

            if (total > 1000)
            {
                if (!ctx.IsReplaying)
                {
                    log.LogWarning($"Need approval for {ctx.InstanceId}");
                }

                ctx.SetCustomStatus("Needs approval");
                await ctx.CallActivityAsync("A_RequestOrderApproval", order);

                var approvalResult = await ctx.WaitForExternalEvent <string>("OrderApprovalResult", TimeSpan.FromSeconds(180), null);

                ctx.SetCustomStatus(""); // clear the needs approval flag

                if (approvalResult != "Approved")
                {
                    // timed out or got a rejected
                    if (!ctx.IsReplaying)
                    {
                        log.LogWarning($"Not approved [{approvalResult}]");
                    }
                    await ctx.CallActivityAsync("A_SendNotApprovedEmail", order);

                    return(new OrderResult {
                        Status = "NotApproved"
                    });
                }
            }

            string[] downloads = null;
            try
            {
                // create files in parallel
                var tasks = new List <Task <string> >();
                foreach (var item in order.Items)
                {
                    tasks.Add(ctx.CallActivityAsync <string>("A_CreatePersonalizedPdf", (order, item)));
                }

                downloads = await Task.WhenAll(tasks);
            }
            catch (Exception ex)
            {
                if (!ctx.IsReplaying)
                {
                    log.LogError($"Failed to create files", ex);
                }
            }

            if (downloads != null)
            {
                await ctx.CallActivityWithRetryAsync("A_SendOrderConfirmationEmail",
                                                     new RetryOptions(TimeSpan.FromSeconds(30), 3),
                                                     (order, downloads));

                return(new OrderResult {
                    Status = "Success", Downloads = downloads
                });
            }
            await ctx.CallActivityWithRetryAsync("A_SendProblemEmail",
                                                 new RetryOptions(TimeSpan.FromSeconds(30), 3),
                                                 order);

            return(new OrderResult {
                Status = "Problem"
            });
        }
예제 #5
0
        public static async Task <int> RunTest(
            [OrchestrationTrigger] DurableOrchestrationContextBase runTestContext, ILogger log, ExecutionContext exCtx)
        {
            var startTime = await runTestContext.CallActivityAsync <DateTime>("whatsTheTime", null);

            if (!runTestContext.IsReplaying)
            {
                log.LogInformation($"Setting up activities to run time: {startTime}");
            }

            List <int> collectionOfInt = new List <int>();

            for (int i = 0; i < 400; i++)
            {
                collectionOfInt.Add(i);
            }

            #region Allow groups of 50
            int total     = 0;
            var groupby50 = collectionOfInt.GroupElements(50);
            foreach (var intValues in groupby50)
            {
                var processTestTasks = new List <Task <int> >();
                //This is processing 50 at a time.
                foreach (var intValue in intValues)
                {
                    Task <int> processTest = runTestContext.CallActivityAsync <int>("delayedActivity", intValue);
                    processTestTasks.Add(processTest);
                    if (!runTestContext.IsReplaying)
                    {
                        log.LogInformation($"Processing {intValue}");
                    }
                }

                if (!runTestContext.IsReplaying)
                {
                    log.LogInformation("Calling When All");
                }
                await Task.WhenAll(processTestTasks);

                total += processTestTasks.Sum(t => t.Result);

                if (!runTestContext.IsReplaying)
                {
                    log.LogInformation($"Total value so far {total} ");
                }
            }
            #endregion

            var endTime = await runTestContext.CallActivityAsync <DateTime>("whatsTheTime", null);

            double totalTime = 0;

            try
            {
                totalTime = endTime.Subtract(startTime).TotalMinutes;
            }
            catch (Exception ex)
            {
                log.LogError("There has been an error" + ex.Message);
            }

            if (!runTestContext.IsReplaying)
            {
                log.LogInformation($"Total Minutes: {totalTime} ");
            }

            runTestContext.SetCustomStatus($"Total Minutes: {totalTime}");
            return(total);
        }