public static async Task RunOrchestration(
            [OrchestrationTrigger] IDurableOrchestrationContext functionContext,
            ILogger log)
        {
            if (functionContext is null)
            {
                throw new ArgumentNullException(nameof(functionContext));
            }

            if (log is null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            var command       = functionContext.GetInput <OrchestratorProjectUserUpdateCommand>();
            var commandResult = command.CreateResult();
            var user          = command.Payload;

            using (log.BeginCommandScope(command))
            {
                try
                {
                    functionContext.SetCustomStatus($"Updating user.", log);

                    using (await functionContext.LockContainerDocumentAsync(user).ConfigureAwait(true))
                    {
                        user = await functionContext
                               .SetUserProjectMembershipAsync(user, command.ProjectId)
                               .ConfigureAwait(true);
                    }

                    functionContext.SetCustomStatus("Sending commands", log);

                    var providerCommand = new ProviderProjectUserUpdateCommand
                                          (
                        command.User.PopulateExternalModel(),
                        user.PopulateExternalModel(),
                        command.ProjectId,
                        command.CommandId
                                          );

                    var providerResults = await functionContext
                                          .SendProviderCommandAsync(providerCommand, null)
                                          .ConfigureAwait(true);

                    var providerException = providerResults.Values?
                                            .SelectMany(result => result.Errors ?? new List <CommandError>())
                                            .ToException();

                    if (providerException != null)
                    {
                        throw providerException;
                    }
                }
                catch (Exception exc)
                {
                    commandResult ??= command.CreateResult();
                    commandResult.Errors.Add(exc);

                    throw;
                }
                finally
                {
                    var commandException = commandResult.Errors?.ToException();

                    if (commandException is null)
                    {
                        functionContext.SetCustomStatus($"Command succeeded", log);
                    }
                    else
                    {
                        functionContext.SetCustomStatus($"Command failed", log, commandException);
                    }

                    commandResult.Result = user;

                    functionContext.SetOutput(commandResult);
                }
            }
        }
Example #2
0
        public static async Task RunOrchestration(
            [OrchestrationTrigger] IDurableOrchestrationContext functionContext,
            ILogger log)
        {
            if (functionContext is null)
            {
                throw new ArgumentNullException(nameof(functionContext));
            }

            if (log is null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            var command        = functionContext.GetInput <OrchestratorProjectUserUpdateCommand>();
            var commandResult  = command.CreateResult();
            var commandProject = default(Project);

            using (log.BeginCommandScope(command))
            {
                try
                {
                    functionContext.SetCustomStatus($"Deleting user", log);

                    using (await functionContext.LockAsync <Project>(command.ProjectId.ToString()).ConfigureAwait(true))
                    {
                        commandProject = await functionContext
                                         .GetProjectAsync(command.ProjectId.GetValueOrDefault())
                                         .ConfigureAwait(true);

                        if (commandProject.Users.Remove(command.Payload))
                        {
                            commandProject = await functionContext
                                             .SetProjectAsync(commandProject)
                                             .ConfigureAwait(true);
                        }
                    }

                    functionContext.SetCustomStatus("Sending commands", log);

                    var providerCommand = new ProviderProjectUserUpdateCommand
                                          (
                        command.User,
                        command.Payload,
                        commandProject.Id,
                        command.CommandId
                                          );

                    var providerResults = await functionContext
                                          .SendCommandAsync <ProviderProjectUserUpdateCommand>(providerCommand, commandProject)
                                          .ConfigureAwait(true);

                    var providerException = providerResults.Values?
                                            .SelectMany(result => result.Errors ?? new List <CommandError>())
                                            .ToException();

                    if (providerException != null)
                    {
                        throw providerException;
                    }
                }
                catch (Exception exc)
                {
                    commandResult ??= command.CreateResult();
                    commandResult.Errors.Add(exc);

                    throw;
                }
                finally
                {
                    var commandException = commandResult.Errors?.ToException();

                    if (commandException is null)
                    {
                        functionContext.SetCustomStatus($"Command succeeded", log);
                    }
                    else
                    {
                        functionContext.SetCustomStatus($"Command failed", log, commandException);
                    }

                    commandResult.Result = command.Payload;

                    functionContext.SetOutput(commandResult);
                }
            }
        }