private async Task <StatusOutputModel> GetServiceStatusAsync(InputRequestModel input)
        {
            StatusOutputModel status = await StatusService.GetStatusAsync(input);

            LoggerService.Info("Got service status", LoggerService.Stop);
            return(status);
        }
        public async Task <StatusChangeOutputModel> StopServicesAsync(InputRequestModel input)
        {
            LoggerService.Info("Beginning the stop procedure", LoggerService.Stop);

            StatusOutputModel preRunServiceStatus = await GetServiceStatusAsync(input);

            await StopStreamingEndpointAsync(preRunServiceStatus, input.StreamingEndpoint);

            LoggerService.Info($"Stopping {input.LiveEvents.Count} live event(s)", LoggerService.Stop);

            foreach (string liveEventName in input.LiveEvents)
            {
                if (!IsLiveEventRunning(preRunServiceStatus, liveEventName))
                {
                    continue;
                }

                await StopLiveEventAsync(liveEventName);

                List <LiveOutput> liveOutputs = await GetAllLiveOutputsForLiveEventAsync(liveEventName);

                LoggerService.Info($"Stopping {liveOutputs.Count} live output(s)", LoggerService.Stop);

                foreach (LiveOutput liveOutput in liveOutputs)
                {
                    await DeleteAllStreamingLocatorsForAssetAsync(liveOutput.AssetName);
                    await DeleteAssetAsync(liveOutput.AssetName);
                    await DeleteLiveOutputAsync(liveEventName, liveOutput.Name);
                }
            }

            StatusOutputModel postRunServiceStatus = await GetServiceStatusAsync(input);

            return(GenerateStatusChange(preRunServiceStatus, postRunServiceStatus));
        }
        private static StatusChangeOutputModel GenerateStatusChange(StatusOutputModel preRunServiceStatus, StatusOutputModel postRunServiceStatus)
        {
            List <StatusChangeOutputModel.Diff.Resource> liveEventDiff = new List <StatusChangeOutputModel.Diff.Resource>();

            for (int i = 0; i < preRunServiceStatus.LiveEvents.Count; ++i)
            {
                liveEventDiff.Add(new StatusChangeOutputModel.Diff.Resource
                {
                    Name      = preRunServiceStatus.LiveEvents[i].Name,
                    NewStatus = postRunServiceStatus.LiveEvents[i].Status.Name,
                    OldStatus = preRunServiceStatus.LiveEvents[i].Status.Name
                });
            }

            return(new StatusChangeOutputModel
            {
                Changes = new StatusChangeOutputModel.Diff
                {
                    LiveEvents = liveEventDiff,
                    StreamingEndpoint = new StatusChangeOutputModel.Diff.Resource
                    {
                        Name = preRunServiceStatus.StreamingEndpoint.Name,
                        NewStatus = postRunServiceStatus.StreamingEndpoint.Status.Name,
                        OldStatus = preRunServiceStatus.StreamingEndpoint.Status.Name
                    }
                },
                Status = postRunServiceStatus
            });
        }
        public async Task <StatusChangeOutputModel> StartServicesAsync(InputRequestModel input)
        {
            LoggerService.Info("Beginning the start procedure", LoggerService.Start);

            StatusOutputModel preRunServiceStatus = await GetServiceStatusAsync(input);

            await StartStreamingEndpointAsync(preRunServiceStatus, input.StreamingEndpoint);

            LoggerService.Info($"Starting {input.LiveEvents.Count} live event(s)", LoggerService.Start);

            foreach (string liveEventName in input.LiveEvents)
            {
                if (!IsLiveEventStopped(preRunServiceStatus, liveEventName))
                {
                    continue;
                }

                ResourceNamesModel resources = GenerateResourceNames(liveEventName);
                Asset asset = await CreateAssetAsync(resources.AssetName);
                await CreateLiveOutputAsync(asset, liveEventName, resources.LiveOutputName, resources.ManifestName);
                await CreateStreamingLocatorAsync(asset, resources.StreamingLocatorName);
                await StartLiveEventAsync(liveEventName);
            }

            StatusOutputModel postRunServiceStatus = await GetServiceStatusAsync(input);

            return(GenerateStatusChange(preRunServiceStatus, postRunServiceStatus));
        }
Пример #5
0
        public async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "broadcaster")] HttpRequest req,
            ILogger log)
        {
            TelemetryClient.TrackEvent("Status");

            using (LoggerService.Init(log))
            {
                try
                {
                    ConfigurationModel       config = ConfigurationService.GetConfiguration();
                    AzureMediaServicesClient client = await AuthenticationService.GetClientAsync(config);

                    InputRequestService inputRequestService = new InputRequestService(client, config);
                    StatusController    statusController    = new StatusController(client, config);

                    InputRequestModel inputModel = await inputRequestService.GetInputRequestModelAsync(req);

                    StatusOutputModel outputModel = await statusController.GetStatusAsync(inputModel);

                    return(SuccessResponseService.CreateResponse(outputModel));
                }
                catch (AppException e)
                {
                    return(ReportError(e));
                }
                catch (Exception e)
                {
                    return(ReportError(e));
                }
            }
        }
        private async Task StopStreamingEndpointAsync(StatusOutputModel preRunServiceStatus, string endpointName)
        {
            if (preRunServiceStatus.StreamingEndpoint.Status.Name == ResourceStatusEnum.Running)
            {
                await Client.StreamingEndpoints.StopAsync(
                    resourceGroupName : Config.ResourceGroup,
                    accountName : Config.AccountName,
                    streamingEndpointName : endpointName
                    );

                LoggerService.Info("Stopped streaming endpoint", LoggerService.Stop);
            }
            else
            {
                LoggerService.Warn("Did not need to stop streaming endpoint", LoggerService.Stop);
            }
        }
        private static bool IsLiveEventRunning(StatusOutputModel preRunServiceStatus, string liveEventName)
        {
            StatusOutputModel.Resource liveEvent = preRunServiceStatus
                                                   .LiveEvents
                                                   .FindLast(currentEvent => currentEvent.Name == liveEventName);

            if (liveEvent.Status.Name != ResourceStatusEnum.Running)
            {
                LoggerService.Warn("Did not stop the live event", LoggerService.Stop);
                return(false);
            }
            else
            {
                LoggerService.Info("Stopping the live event", LoggerService.Stop);
                return(true);
            }
        }