/// <summary> /// Sets the VHO dictionary by getting the VHO details from the xml /// </summary> /// <param name="config">XML config file</param> /// <param name="ns">Namespace of the config file</param> /// <returns></returns> private Task setVHO(XDocument config, XNamespace ns) { Trace.WriteLine("setVHO called"); var vhoElements = config.Root.Descendants(ns + "VHO"); var exceptions = new ConcurrentQueue <Exception>(); var vhoDict = new ConcurrentDictionary <string, NGVodVHO>(); Parallel.ForEach(vhoElements, (el) => { try { var vho = new NGVodVHO(); var sqlDb = new SqlDb(); vho.Name = el.Attribute("Name").Value; vho.WebServerName = el.Element(ns + "PrimaryWebServer").Value; sqlDb.DatabaseName = el.Element(ns + "IMGDb").Attribute("Name").Value; vho.IMGDb = sqlDb; vho.IMGDs = new Datasource(el.Element(ns + "IMGDb").Attribute("InstanceName").Value); vho.IMGDs.Databases.Add(sqlDb); vho.IMGConnectString = vho.IMGDs.CreateConnectionString(true, vho.IMGDb); #if DEBUG Trace.WriteLine(string.Format("Name: {0} | WS: {1} | DBName: {2} | DBSource: {3}", vho.Name, vho.WebServerName, vho.IMGDb.DatabaseName, vho.IMGDs.Name)); #endif try { vhoDict.TryAdd(vho.Name, vho); } catch (Exception ex) { Trace.TraceError("Error in setVHO while adding to dictionary. Key: {0}, Value: {1}, Error: {2}", vho.Name, vho, ex.Message); } } catch (NullReferenceException nre) { Trace.TraceError("Error in setVHO. {0}", nre.Message); exceptions.Enqueue(nre); } catch (Exception ex) { Trace.TraceError("Error in setVHO. {0}", ex.Message); exceptions.Enqueue(ex); } }); this._vhos = vhoDict; if (exceptions.Count > 0) { throw new AggregateException(exceptions); } return(Task.FromResult(0)); }
/// <summary> /// Gets all VOD Asset Id to Poster Maps for all VHO's /// </summary> /// <param name="config">Configuration that contains all VHO data</param> /// <returns>Enumerable with a tuple that contains the vod asset id and poster file (full path)</returns> public static IEnumerable <Tuple <int, string> > GetAllPosterSourceMaps(NGVodPosterConfig config, CancellationToken CancelToken) { StringBuilder sbCmd = new StringBuilder(); sbCmd.AppendFormat("SELECT * FROM {0} WHERE {0}.strPosterFile IS NOT NULL", _posterSourceMapTbl); foreach (var vho in config.Vhos.Keys) { CancelToken.ThrowIfCancellationRequested(); NGVodVHO vodvho = config.Vhos[vho]; foreach (var dr in DBFactory.SQL_ExecuteReader(vodvho.IMGDs.CreateConnectionString(vodvho.IMGDb), sbCmd.ToString(), CommandType.Text, null)) { CancelToken.ThrowIfCancellationRequested(); yield return(new Tuple <int, string>(int.Parse(dr.GetString(0)), dr.IsDBNull(1) ? string.Empty : Path.Combine(config.SourceDir, dr.GetString(1)))); } } }
/// <summary> /// Begins processing of NGVodPoster image files for the provided VHO /// </summary> /// <param name="vho">Name of the VHO</param> /// <param name="maxImages">Maximum numbere of images to process</param> /// <param name="config">NGVodPoster configuration</param> /// <param name="token">Cancellation Token</param> /// <returns>Task result returns with all assets that do not have posters assigned</returns> internal async Task BeginProcess(string vho, int?maxImages, NGVodPosterConfig config) { if (this.token.IsCancellationRequested) { this.token.ThrowIfCancellationRequested(); } Console.WriteLine("\n\n----Beginning {0}----\n", vho); Trace.TraceInformation(vho); //Get the VHO values from the configuration NGVodVHO ngVho = null; if (!config.Vhos.TryGetValue(vho, out ngVho)) { throw new Exception("Unable to get VHO from config"); } //Get the T-SQL connection string for the IMG front end database string connectionStr = ngVho.IMGConnectString; #if DEBUG Trace.TraceInformation("Connection string for {0} --> {1}", vho, connectionStr); #endif this.token.ThrowIfCancellationRequested(); if (ngProgress.Total != 0) { this.ngProgress.Reset(); } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); bool ranToCompletion = false; if (string.IsNullOrEmpty(this.customAction) || this.customAction.ToUpper().EndsWith("M")) { //Start run task to ensure it ran to completion before attempting cleanup var mainTsk = Task.Factory.StartNew(() => ProcessAsset(config, ngVho.Name, ngVho.PosterDir, connectionStr, this.token)); try { //Allow threads to catch up before changing the console color await Task.Delay(20); Console.ForegroundColor = ConsoleColor.Cyan; this.ngProgress.StopProgress = false; //Wait to finish await mainTsk; } catch (AggregateException aex) { foreach (var ex in aex.InnerExceptions) { Trace.TraceError("Error during processing method. {0}", ex.Message); } } ranToCompletion = mainTsk.Status == TaskStatus.RanToCompletion; if (ngProgress.CompleteCount > config.Vhos.Count && ngProgress.CompleteCount % config.Vhos.Count == 0 && !token.IsCancellationRequested) { await Task.Delay(15000); this.ngProgress.StopProgress = true; this.ngProgress.IsComplete = !this.ngProgress.IsCanceled; } } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); await cleanupDatabase(connectionStr, vho, this.token); //Clean up poster directory based on the vho's active assets if the process had no errors, no max values were specified, //and cancellation was not requested. if ((ranToCompletion || (!string.IsNullOrEmpty(this.customAction) && this.customAction.ToUpper().EndsWith("D"))) && !maxImages.HasValue && !token.IsCancellationRequested) { try { await Task.Factory.StartNew(() => cleanupDestination(GetVODAssets(connectionStr, maxImages, vho, config.SourceDir, ngVho.PosterDir, this.token).Select(x => x.PosterDest).ToList(), ngVho.PosterDir, config.MaxThreads)); } catch (Exception ex) { Trace.TraceError("Error while cleaning up destination directory. {0}", ex.Message); } } }