Пример #1
0
 public static void Run([ActivityTrigger] ApprovalResponseMetadata responseMetadata, TraceWriter log)
 {
     log.Info($"Moving Blob {responseMetadata.ReferenceUrl} to {responseMetadata.DestinationContainer}");
     try
     {
         CloudStorageAccount account = CloudStorageAccount.Parse(System.Environment.GetEnvironmentVariable("Blob:StorageConnection", EnvironmentVariableTarget.Process));
         var client               = account.CreateCloudBlobClient();
         var sourceBlob           = client.GetBlobReferenceFromServerAsync(new Uri(responseMetadata.ReferenceUrl)).Result;
         var destinationContainer = client.GetContainerReference(responseMetadata.DestinationContainer);
         var destinationBlob      = destinationContainer.GetBlobReference(sourceBlob.Name);
         destinationBlob.StartCopyAsync(sourceBlob.Uri);
         Task.Delay(TimeSpan.FromSeconds(15)).Wait();
         sourceBlob.DeleteAsync();
         log.Info($"Blob '{responseMetadata.ReferenceUrl}' moved to container '{responseMetadata.DestinationContainer}'");
     }
     catch (Exception ex)
     {
         log.Error(ex.ToString());
         throw;
     }
 }
Пример #2
0
        public static async Task <bool> Run(
            [OrchestrationTrigger] DurableOrchestrationContext context,
            ILogger log)
        {
            if (!context.IsReplaying)
            {
                log.LogInformation($"Starting the orchestration {context.InstanceId}");
            }

            context.SetCustomStatus("The application has been received.");

            var    isApproved      = false;
            string meansOfApproval = Environment.GetEnvironmentVariable("Workflow:MeansOfApproval");
            ApprovalRequestMetadata approvalRequestMetadata = context.GetInput <ApprovalRequestMetadata>();

            approvalRequestMetadata.InstanceId = context.InstanceId;

            await context.CallActivityAsync("PersistWorkflowCorrelation",
                                            new WorkflowCorrelation {
                EntityId           = $"{approvalRequestMetadata.ApplicantId}",
                WorkflowInstanceId = context.InstanceId
            });

            context.SetCustomStatus("We are reviewing your application. Bear with us.");

            // Check whether the approval request is to be sent via Email or Slack based on App Settings
            if (meansOfApproval.Equals("email", StringComparison.OrdinalIgnoreCase))
            {
                if (!context.IsReplaying)
                {
                    log.LogInformation("Sending Approval Request Via Email");
                }

                await context.CallActivityAsync("SendApprovalRequestViaEmail", approvalRequestMetadata);
            }
            else
            {
                if (!context.IsReplaying)
                {
                    log.LogInformation("Sending Approval Request Via Slack");
                }

                await context.CallActivityAsync("SendApprovalRequestViaSlack", approvalRequestMetadata);
            }

            // Wait for Response as an external event or a time out.
            // The approver has a limit to approve otherwise the request will be rejected.
            using (var timeoutCts = new CancellationTokenSource())
            {
                int timeout;
                if (!int.TryParse(Environment.GetEnvironmentVariable("Workflow:Timeout"), out timeout))
                {
                    timeout = 5;
                }
                DateTime expiration  = context.CurrentUtcDateTime.AddMinutes(timeout);
                Task     timeoutTask = context.CreateTimer(expiration, timeoutCts.Token);

                // This event can come from a click on the Email sent via SendGrid or a selection on the message sent via Slack.
                Task <bool> approvalResponse = context.WaitForExternalEvent <bool>("ReceiveApprovalResponse");

                Task winner = await Task.WhenAny(approvalResponse, timeoutTask);

                ApprovalResponseMetadata approvalResponseMetadata = new ApprovalResponseMetadata()
                {
                    ReferenceUrl = approvalRequestMetadata.ReferenceUrl
                };

                if (winner == approvalResponse)
                {
                    if (!context.IsReplaying)
                    {
                        log.LogInformation("An approval response was received");
                    }

                    if (approvalResponse.Result)
                    {
                        approvalResponseMetadata.Status = "approved";
                        context.SetCustomStatus("Approved. Congrats, You are super special!");
                    }
                    else
                    {
                        approvalResponseMetadata.Status = "rejected";
                        context.SetCustomStatus("Rejected. Sorry! Only the best can join us!");
                    }
                }
                else
                {
                    if (!context.IsReplaying)
                    {
                        log.LogInformation("The waiting time has exceeded!");
                    }

                    approvalResponseMetadata.Status = "rejected";
                    context.SetCustomStatus("Rejected. Sorry! We are extremely busy. Try again in your next life!");
                }

                if (!timeoutTask.IsCompleted)
                {
                    // All pending timers must be completed or cancelled before the function exits.
                    timeoutCts.Cancel();
                }

                // Once the approval process has been finished, the Blob is to be moved to the corresponding container.

                if (!context.IsReplaying)
                {
                    log.LogInformation("Moving the blob to the corresponding container");
                }

                await context.CallActivityAsync <string>("MoveBlob", approvalResponseMetadata);

                if (!context.IsReplaying)
                {
                    log.LogInformation("Orchestration has finished");
                }

                return(isApproved);
            }
        }
        public static async Task <bool> Run([OrchestrationTrigger] DurableOrchestrationContext context)
        {
            var    isApproved      = false;
            string meansOfApproval = Environment.GetEnvironmentVariable("Workflow:MeansOfApproval");
            ApprovalRequestMetadata approvalRequestMetadata = context.GetInput <ApprovalRequestMetadata>();

            approvalRequestMetadata.InstanceId = context.InstanceId;

            // Check whether the approval request is to be sent via Email or Slack based on App Settings
            if (meansOfApproval.Equals("email", StringComparison.OrdinalIgnoreCase))
            {
                await context.CallActivityAsync("SendApprovalRequestViaEmail", approvalRequestMetadata);
            }
            else
            {
                await context.CallActivityAsync("SendApprovalRequestViaSlack", approvalRequestMetadata);
            }

            // Wait for Response as an external event or a time out.
            // The approver has a limit to approve otherwise the request will be rejected.
            using (var timeoutCts = new CancellationTokenSource())
            {
                int timeout;
                if (!int.TryParse(Environment.GetEnvironmentVariable("Workflow:Timeout"), out timeout))
                {
                    timeout = 5;
                }
                DateTime expiration  = context.CurrentUtcDateTime.AddMinutes(timeout);
                Task     timeoutTask = context.CreateTimer(expiration, timeoutCts.Token);

                // This event can come from a click on the Email sent via SendGrid or a selection on the message sent via Slack.
                Task <bool> approvalResponse = context.WaitForExternalEvent <bool>("ReceiveApprovalResponse");
                Task        winner           = await Task.WhenAny(approvalResponse, timeoutTask);

                ApprovalResponseMetadata approvalResponseMetadata = new ApprovalResponseMetadata()
                {
                    ReferenceUrl = approvalRequestMetadata.ReferenceUrl
                };

                if (winner == approvalResponse)
                {
                    if (approvalResponse.Result)
                    {
                        approvalResponseMetadata.DestinationContainer = "approved";
                    }
                    else
                    {
                        approvalResponseMetadata.DestinationContainer = "rejected";
                    }
                }
                else
                {
                    approvalResponseMetadata.DestinationContainer = "rejected";
                }

                if (!timeoutTask.IsCompleted)
                {
                    // All pending timers must be completed or cancelled before the function exits.
                    timeoutCts.Cancel();
                }

                // Once the approval process has been finished, the Blob is to be moved to the corresponding container.
                await context.CallActivityAsync <string>("MoveBlob", approvalResponseMetadata);

                return(isApproved);
            }
        }