Example #1
0
        public static async Task <object> ProcessVideo(
            [OrchestrationTrigger] IDurableOrchestrationContext ctx,
            ILogger log)
        {
            var videoLocation = ctx.GetInput <string>();

            try
            {
                var transcodedLocations = await
                                          ctx.CallSubOrchestratorAsync <string[]>(OrchestratorNames.Transcode,
                                                                                  videoLocation);

                var transcodedLocation = transcodedLocations.First(x => x.Contains(".mp4")); // these are SAS tokens

                var thumbnailLocation = await ctx.CallActivityWithRetryAsync <string>(ActivityNames.ExtractThumbnail,
                                                                                      new RetryOptions(TimeSpan.FromSeconds(5), 4), // {Handle = ex => ex.InnerException is InvalidOperationException}, - currently not possible #84
                                                                                      transcodedLocation);

                var withIntroLocation = await ctx.CallActivityAsync <string>
                                            (ActivityNames.PrependIntro, transcodedLocation);

                // we need to give our suborchestrator its own id so we can send it events
                // could be a new guid, but by basing it on the parent instance id we make it predictable
                var approvalInfo =
                    new ApprovalInfo {
                    OrchestrationId = "XYZ" + ctx.InstanceId, VideoLocation = withIntroLocation
                };
                var approvalResult = await ctx.CallSubOrchestratorAsync <string>(OrchestratorNames.GetApprovalResult, approvalInfo.OrchestrationId, approvalInfo);

                if (approvalResult == "Approved")
                {
                    await ctx.CallActivityAsync(ActivityNames.PublishVideo,
                                                new [] { transcodedLocation, thumbnailLocation, withIntroLocation });

                    return("Approved and published");
                }
                await ctx.CallActivityAsync(ActivityNames.RejectVideo,
                                            new [] { transcodedLocation, thumbnailLocation, withIntroLocation });

                return($"Not published because {approvalResult}");
            }
            catch (Exception e)
            {
                if (!ctx.IsReplaying)
                {
                    log.LogError("Failed to process video with error " + e.Message);
                }
                await ctx.CallActivityAsync(ActivityNames.Cleanup, videoLocation);

                return(new { Error = "Failed to process video", e.Message });
            }
        }
        public static int SendApprovalRequestEmail(
            [ActivityTrigger] ApprovalInfo approvalInfo,
            [SendGrid(ApiKey = "SendGridKey")] out SendGridMessage message,
            [Table("Approvals", "AzureWebJobsStorage")] out Approval approval,
            ILogger log)
        {
            var approvalCode = Guid.NewGuid().ToString("N");

            approval = new Approval
            {
                PartitionKey    = "Approval",
                RowKey          = approvalCode,
                OrchestrationId = approvalInfo.OrchestrationId
            };
            var approverEmail = new EmailAddress(Environment.GetEnvironmentVariable("ApproverEmail"));
            var senderEmail   = new EmailAddress(Environment.GetEnvironmentVariable("SenderEmail"));
            var subject       = "A video is awaiting approval";

            log.LogInformation($"Sending approval request for {approvalInfo.VideoLocation}");
            var host            = Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME") ?? "localhost:7071";
            var functionAddress = $"http://{host}/api/SubmitVideoApproval/{approvalCode}";
            var approvedLink    = functionAddress + "?result=Approved";
            var rejectedLink    = functionAddress + "?result=Rejected";
            var body            = $"Please review {approvalInfo.VideoLocation}<br>"
                                  + $"<a href=\"{approvedLink}\">Approve</a><br>"
                                  + $"<a href=\"{rejectedLink}\">Reject</a>";

            message         = new SendGridMessage();
            message.Subject = subject;
            message.From    = senderEmail;
            message.AddTo(approverEmail);
            message.HtmlContent = body;

            log.LogWarning(body);
            var approvalTimeoutSeconds = Environment.GetEnvironmentVariable("ApprovalTimeoutSeconds");

            if (string.IsNullOrEmpty(approvalTimeoutSeconds))
            {
                return(30);
            }
            return(Int32.Parse(approvalTimeoutSeconds));
        }
        public static int SendApprovalRequestEmail(
            [ActivityTrigger] ApprovalInfo approvalInfo,
            [SendGrid(ApiKey = "SendGridKey")] out Mail message,
            [Table("Approvals", "AzureWebJobsStorage")] out Approval approval,
            TraceWriter log)
        {
            var approvalCode = Guid.NewGuid().ToString("N");

            approval = new Approval
            {
                PartitionKey    = "Approval",
                RowKey          = approvalCode,
                OrchestrationId = approvalInfo.OrchestrationId
            };
            var approverEmail = new Email(ConfigurationManager.AppSettings["ApproverEmail"]);
            var senderEmail   = new Email(ConfigurationManager.AppSettings["SenderEmail"]);
            var subject       = "A video is awaiting approval";

            log.Info($"Sending approval request for {approvalInfo.VideoLocation}");
            var host            = Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME") ?? "localhost:7071";
            var functionAddress = $"http://{host}/api/SubmitVideoApproval/{approvalCode}";
            var approvedLink    = functionAddress + "?result=Approved";
            var rejectedLink    = functionAddress + "?result=Rejected";
            var body            = $"Please review {approvalInfo.VideoLocation}<br>"
                                  + $"<a href=\"{approvedLink}\">Approve</a><br>"
                                  + $"<a href=\"{rejectedLink}\">Reject</a>";
            var content = new Content("text/html", body);

            message = new Mail(senderEmail, subject, approverEmail, content);

            log.Warning(body);
            var approvalTimeoutSeconds = ConfigurationManager.AppSettings["ApprovalTimeoutSeconds"];

            if (string.IsNullOrEmpty(approvalTimeoutSeconds))
            {
                return(30);
            }
            return(Int32.Parse(approvalTimeoutSeconds));
        }