public static void Run([BlobTrigger(Constant.Storage.BlobContainer.MediaServices + "/{blobName}", Connection = "Storage")] Stream blobStream, string blobName, ILogger logger) { try { StringComparison stringComparison = StringComparison.OrdinalIgnoreCase; if (!blobName.EndsWith(Constant.Media.IngestManifest.FileExtensionLog, stringComparison)) { logger.LogInformation("Media File: {0}", blobName); if (blobName.StartsWith(Constant.Media.IngestManifest.TriggerPrefix, stringComparison)) { ProcessManifest(blobStream, blobName, logger); } else { string collectionId = Constant.Database.Collection.MediaIngestManifest; MediaIngestManifest[] ingestManifests = _databaseClient.GetDocuments <MediaIngestManifest>(collectionId); foreach (MediaIngestManifest ingestManifest in ingestManifests) { List <string> missingFiles = new List <string>(); foreach (string missingFile in ingestManifest.MissingFiles) { if (!string.Equals(missingFile, blobName, stringComparison)) { missingFiles.Add(blobName); } } if (missingFiles.Count == 0) { _databaseClient.DeleteDocument(collectionId, ingestManifest.Name); string containerName = Constant.Storage.BlobContainer.MediaServices; CloudBlockBlob manifestBlob = _blobClient.GetBlockBlob(containerName, null, ingestManifest.Name); blobStream = manifestBlob.OpenReadAsync().Result; ProcessManifest(blobStream, ingestManifest.Name, logger); } else if (missingFiles.Count != ingestManifest.MissingFiles.Length) { ingestManifest.MissingFiles = missingFiles.ToArray(); _databaseClient.UpsertDocument(collectionId, ingestManifest); } } } } } catch (ApiErrorException ex) { string logData = ex.Response.Content; WriteLog(blobName, logData, null, true); logger.LogError(logData); throw; } catch (Exception ex) { string logData = ex.ToString(); WriteLog(blobName, logData, null, true); logger.LogError(logData); throw; } }
private static void SyncMediaJobs() { DatabaseClient databaseClient = new DatabaseClient(); string collectionId = Constant.Database.Collection.MediaJobAccount; MediaJobAccount[] jobAccounts = databaseClient.GetDocuments <MediaJobAccount>(collectionId); foreach (MediaJobAccount jobAccount in jobAccounts) { using (MediaClient mediaClient = new MediaClient(null, jobAccount.MediaAccount)) { Job job = mediaClient.GetEntity <Job>(MediaEntity.TransformJob, jobAccount.JobName); if (job == null) { databaseClient.DeleteDocument(collectionId, jobAccount.JobName); } } } }
private static void SyncMediaAssets(DatabaseClient databaseClient, ILogger logger) { string collectionId = Constant.Database.Collection.MediaAssets; MediaAssetLink[] assetLinks = databaseClient.GetDocuments <MediaAssetLink>(collectionId); foreach (MediaAssetLink assetLink in assetLinks) { using (MediaClient mediaClient = new MediaClient(assetLink.MediaAccount, assetLink.UserAccount)) { Asset parentAsset = mediaClient.GetEntity <Asset>(MediaEntity.Asset, assetLink.AssetName); if (parentAsset == null) { foreach (KeyValuePair <MediaTransformPreset, string> jobOutput in assetLink.JobOutputs) { if (jobOutput.Key == MediaTransformPreset.VideoIndexer || jobOutput.Key == MediaTransformPreset.AudioIndexer) { string insightId = jobOutput.Value; mediaClient.IndexerDeleteVideo(insightId); } else { string assetName = jobOutput.Value; mediaClient.DeleteEntity(MediaEntity.Asset, assetName); } } string documentId = assetLink.AssetName; databaseClient.DeleteDocument(collectionId, documentId); logger.LogInformation("Asset Link Deleted: {0}", documentId); } else { List <MediaTransformPreset> missingJobOutputs = new List <MediaTransformPreset>(); foreach (KeyValuePair <MediaTransformPreset, string> jobOutput in assetLink.JobOutputs) { if (jobOutput.Key == MediaTransformPreset.VideoIndexer || jobOutput.Key == MediaTransformPreset.AudioIndexer) { string insightId = jobOutput.Value; bool insightExists = mediaClient.IndexerInsightExists(insightId, out JObject insight); if (!insightExists) { missingJobOutputs.Add(jobOutput.Key); logger.LogInformation("Missing Indexer Insight: {0}", insightId); } } else { string assetName = jobOutput.Value; Asset childAsset = mediaClient.GetEntity <Asset>(MediaEntity.Asset, assetName); if (childAsset == null) { missingJobOutputs.Add(jobOutput.Key); logger.LogInformation("Missing Output Asset: {0}", assetName); } } } if (missingJobOutputs.Count > 0) { foreach (MediaTransformPreset missingJobOutput in missingJobOutputs) { assetLink.JobOutputs.Remove(missingJobOutput); } databaseClient.UpsertDocument(collectionId, assetLink); logger.LogInformation("Asset Link Upserted: {0}", assetLink); } } } } }