/// <summary> /// Populate this container with AcLock objects on \e streams. /// </summary> /// <param name="streams">Limit the list of locks to those on \e streams only. Stream names in \e streams /// must match their respective AccuRev stream name exactly.</param> /// <returns>\e true if initialization succeeded, \e false otherwise.</returns> /// <exception cref="AcUtilsException">caught and [logged](@ref AcUtils#AcDebug#initAcLogging) /// in <tt>\%LOCALAPPDATA\%\\AcTools\\Logs\\<prog_name\>-YYYY-MM-DD.log</tt> on \c show command failure.</exception> /// <exception cref="Exception">caught and logged in same on failure to handle a range of exceptions.</exception> /*! \sa [AcLocks constructor](@ref AcUtils#AcLocks#AcLocks), initAsync(AcDepot), initAsync(DepotsCollection) */ /*! \show_ <tt>show -fx locks</tt> */ public async Task <bool> initAsync(StreamsCollection streams) { bool ret = false; // assume failure try { AcResult r = await AcCommand.runAsync("show -fx locks").ConfigureAwait(false); if (r != null && r.RetVal == 0) { XElement xml = XElement.Parse(r.CmdResult); IEnumerable <XElement> query = from e in xml.Elements("Element") where streams.OfType <StreamElement>().Any(se => se.Stream == (string)e.Attribute("Name")) select e; ret = initList(query); } } catch (AcUtilsException ecx) { AcDebug.Log($"AcUtilsException caught and logged in AcLocks.initAsync(StreamsCollection){Environment.NewLine}{ecx.Message}"); } catch (Exception ecx) { AcDebug.Log($"Exception caught and logged in AcLocks.initAsync(StreamsCollection){Environment.NewLine}{ecx.Message}"); } return(ret); }
/// <summary> /// Populate this container with AcRule objects for all streams in \e streamsCol /// as per [constructor parameter](@ref AcUtils#AcRules#AcRules) \e explicitOnly. /// </summary> /// <param name="streamsCol">The list of streams to query for rules.</param> /// <param name="progress">Optionally report progress back to the caller.</param> /// <returns>\e true if no failure occurred and list was initialized successfully, \e false otherwise.</returns> /// <exception cref="Exception">caught and [logged](@ref AcUtils#AcDebug#initAcLogging) in /// <tt>\%LOCALAPPDATA\%\\AcTools\\Logs\\<prog_name\>-YYYY-MM-DD.log</tt> on failure to handle a range of exceptions.</exception> public async Task <bool> initAsync(StreamsCollection streamsCol, IProgress <int> progress = null) { bool ret = false; // assume failure try { AcDepots depots = new AcDepots(); if (!(await depots.initAsync(null, progress).ConfigureAwait(false))) { return(false); } int num = 0; // get number of streams for tasks list foreach (AcDepot depot in depots) { IEnumerable <AcStream> filter = depot.Streams.Where(s => streamsCol.OfType <StreamElement>().Any(se => s.Name == se.Stream)); num += filter.Count(); } List <Task <bool> > tasks = new List <Task <bool> >(num); Func <Task <bool>, bool> cf = t => { bool res = t.Result; if (res && progress != null) { progress.Report(Interlocked.Increment(ref _counter)); } return(res); }; foreach (AcDepot depot in depots) { IEnumerable <AcStream> filter = depot.Streams.Where(s => streamsCol.OfType <StreamElement>().Any(se => s.Name == se.Stream)); foreach (AcStream stream in filter) { Task <bool> t = initAsync(stream).ContinueWith(cf); tasks.Add(t); } } bool[] arr = await Task.WhenAll(tasks).ConfigureAwait(false); ret = (arr != null && arr.All(n => n == true)); // true if all succeeded } catch (Exception ecx) { AcDebug.Log($"Exception caught and logged in AcRules.initAsync(StreamsCollection){Environment.NewLine}{ecx.Message}"); } return(ret); }