Esempio n. 1
0
        public override ResponseEncodeStatusBaseDTO ToGridwichEncodeData()
        {
            var encodeStatusProcessing = new ResponseEncodeProcessingDTO(CustomEventTypes.ResponseEncodeFlipProcessing)
            {
                CurrentStatus    = "Processing", // TODO: There seems to be no other changing status from Telestream.
                PercentComplete  = Progress,
                OperationContext = GetOperationContext()
            };

            return(encodeStatusProcessing);
        }
        /// <summary>
        /// Handles the CloudPort encode complete EventGrid message, converting it and
        /// sending it to Requestor.
        /// </summary>
        /// <param name="eventData">EventGrid object.</param>
        /// <param name="eventType">The event type.</param>
        /// <returns>True if successfully handled, false if not.</returns>
        protected override async Task <ResponseBaseDTO> DoWorkAsync(CloudPortStatusData eventData, string eventType)
        {
            _ = eventData ?? throw new ArgumentNullException(nameof(eventData));
            _ = eventType ?? throw new ArgumentNullException(nameof(eventType));

            var flipPayload = eventData.Payload;

            _ = flipPayload ?? throw new ArgumentException("FlipPayload cannot be null");

            // If the EncodeData.EventType is a Gridwich failure, then we need to convert
            // it into a generic Gridwich failure and push it up the stack.
            switch (eventType.ToLowerInvariant())
            {
            case ExternalEventTypes.CloudPortWorkflowJobError:
                var workflowJobInfo = await _cloudPortService.GetWorkflowJobInfo(eventData.WorkflowId, eventData.Id).ConfigureAwait(false);

                this.Log.LogEventObject(out var uriLocator, LogEventIds.EncodeCompleteFailure, new { eventData, workflowJobInfo });
                return(GetGridwichFailureDTO(workflowJobInfo.ErrorClass + ": " + workflowJobInfo.ErrorMessage, flipPayload.OperationContext, LogEventIds.EncodeCompleteFailure, uriLocator));

            case ExternalEventTypes.CloudPortWorkflowJobProgress:
                var responseEncodeProcessingDTO = new ResponseEncodeProcessingDTO(CustomEventTypes.ResponseEncodeCloudPortProcessing)
                {
                    PercentComplete  = eventData.Progress,
                    OperationContext = flipPayload.OperationContext,
                    WorkflowJobName  = eventData.Name
                };
                this.Log.LogEventObject(LogEventIds.CloudPortProgress, responseEncodeProcessingDTO);
                return(responseEncodeProcessingDTO);

            case ExternalEventTypes.CloudPortWorkflowJobSuccess:
                workflowJobInfo = await _cloudPortService.GetWorkflowJobInfo(eventData.WorkflowId, eventData.Id).ConfigureAwait(false);

                // Rifle thru all the possible output files from CloudPort and build a list.
                List <string> listOfOutputFiles;
                try
                {
                    listOfOutputFiles = GetOutputFiles(workflowJobInfo);
                }
                catch (Exception e)
                {
                    throw new GridwichCloudPortOutputListingException(string.Empty, flipPayload.OperationContext, e);
                }

                var responseEncodeSuccessDTO = new ResponseEncodeSuccessDTO(CustomEventTypes.ResponseEncodeCloudportSuccess)
                {
                    OperationContext = flipPayload.OperationContext,
                    Outputs          = listOfOutputFiles.ConvertAll(s => new Output()
                    {
                        BlobUri = _storageService.CreateBlobUrl(flipPayload.OutputContainer, s)
                    }).ToArray(),
                    WorkflowJobName = eventData.Name,

                    // TODO: Uncomment this line when Cloudflare firewall rules have been addressed.
                    EncoderContext = JObject.FromObject(workflowJobInfo)
                };

                this.Log.LogEventObject(LogEventIds.CloudPortSuccess, responseEncodeSuccessDTO);
                return(responseEncodeSuccessDTO);

            case ExternalEventTypes.CloudPortWorkflowJobCreated:
                return(new ResponseEncodeScheduledDTO(CustomEventTypes.ResponseEncodeCloudPortScheduled)
                {
                    OperationContext = flipPayload.OperationContext,
                    WorkflowJobName = eventData.Name
                });

            // In theory it is not possible to reach this code.  But...
            default:
                throw new GridwichCloudPortWorkflowJobException("Invalid event type received from CloudPort.", flipPayload.OperationContext);
            }
        }
        /// <inheritdoc/>
        protected override async Task <ResponseBaseDTO> DoWorkAsync(object eventData, string eventType)
        {
            _ = eventData ?? throw new ArgumentNullException(nameof(eventData));

            switch (eventType)
            {
            case EventTypes.MediaJobCanceledEvent:
            {
                var canceledEventData = (MediaJobCanceledEventData)eventData;
                this.Log.LogEventObject(out Uri uriLocator, LogEventIds.MediaServicesV3JobCanceledReceived, canceledEventData);
                return(GetGridwichFailureDTO(LogEventIds.MediaServicesV3JobCanceledReceived.Name,
                                             GetOperationContext(canceledEventData), LogEventIds.MediaServicesV3JobCanceledReceived.Id, uriLocator));
            }

            case EventTypes.MediaJobErroredEvent:
            {
                var errorData    = (MediaJobErroredEventData)eventData;
                var errorMessage = BuildErrorMessage(errorData);

                // - Raise Event "Job Errored"
                this.Log.LogEventObject(out Uri uriLocator, LogEventIds.MediaServicesV3JobErroredReceived, errorData);
                return(GetGridwichFailureDTO(
                           $"{LogEventIds.MediaServicesV3JobErroredReceived.Name}: {errorMessage}",
                           GetOperationContext(errorData),
                           LogEventIds.MediaServicesV3JobErroredReceived.Id,
                           uriLocator));
            }

            case EventTypes.MediaJobFinishedEvent:
            {
                var finishedData     = (MediaJobFinishedEventData)eventData;
                var operationContext = GetOperationContext(finishedData);
                var encodeStatus     = new ResponseEncodeSuccessDTO(CustomEventTypes.ResponseEncodeMediaServicesV3Success)
                {
                    OperationContext = operationContext
                };
                // Get the Blob Uri from Azure Media Services in the correlationData
                finishedData.CorrelationData.TryGetValue("outputAssetContainer", out var uri);
                encodeStatus.Outputs = await GetOutputBlobs(uri, operationContext).ConfigureAwait(false);

                return(encodeStatus);
            }

            case EventTypes.MediaJobOutputProgressEvent:
            {
                var progressData = (MediaJobOutputProgressEventData)eventData;

                var encodeStatus = new ResponseEncodeProcessingDTO(CustomEventTypes.ResponseEncodeMediaServicesV3Processing);
                if (progressData.Progress != null)
                {
                    encodeStatus.PercentComplete = (int)progressData.Progress;
                }

                // CurrentStatus = "Processing",....
                encodeStatus.CurrentStatus    = ProcessingStatus;
                encodeStatus.OperationContext = GetOperationContext(progressData);
                return(encodeStatus);
            }

            case EventTypes.MediaJobScheduledEvent:
            {
                var scheduledData = (MediaJobScheduledEventData)eventData;

                var encodeStatus = new ResponseEncodeSuccessDTO(CustomEventTypes.ResponseEncodeMediaServicesV3Scheduled)
                {
                    OperationContext = GetOperationContext(scheduledData)
                };
                return(encodeStatus);
            }

            default:
                throw new GridwichMediaServicesV3NotHandledException($"The provided event type {eventType} cannot be handled by this handler. ", null);
            }
        }