Skip to content

Commit 16e9822

Browse files
committed
Add ConcurrentQueue for writing output
1 parent 5cc9d8d commit 16e9822

6 files changed

Lines changed: 280 additions & 120 deletions

File tree

src/code/FindHelper.cs

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,11 @@ internal IEnumerable<PSResourceInfo> FindDependencyPackages(ServerApiCall curren
11721172
// Method 2
11731173
internal void FindDependencyPackagesHelper(ServerApiCall currentServer, ResponseUtil currentResponseUtil, PSResourceInfo currentPkg, PSRepositoryInfo repository)
11741174
{
1175-
ConcurrentBag<ErrorRecord> errors = new ConcurrentBag<ErrorRecord>();
1175+
ConcurrentQueue<ErrorRecord> errorMsgs = new ConcurrentQueue<ErrorRecord>();
1176+
ConcurrentQueue<string> verboseMsgs = new ConcurrentQueue<string>();
1177+
ConcurrentQueue<string> debugMsgs = new ConcurrentQueue<string>();
1178+
ConcurrentQueue<string> warningMsgs = new ConcurrentQueue<string>();
1179+
11761180
if (currentPkg.Dependencies.Length > 0)
11771181
{
11781182
// If finding more than 5 packages, do so concurrently
@@ -1183,26 +1187,54 @@ internal void FindDependencyPackagesHelper(ServerApiCall currentServer, Response
11831187
{
11841188
Parallel.ForEach(currentPkg.Dependencies, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }, dep =>
11851189
{
1186-
FindDependencyPackageVersion(dep, currentServer, currentResponseUtil, currentPkg, repository, errors);
1190+
debugMsgs.Enqueue($"Finding dependency '{dep.Name}' version range '{dep.VersionRange}'");
1191+
FindDependencyPackageVersion(dep, currentServer, currentResponseUtil, currentPkg, repository, errorMsgs, verboseMsgs, warningMsgs);
11871192
});
11881193
// TODO: what is perf if parallel.ForEach is always run?
11891194
}
11901195
else
11911196
{
11921197
foreach (var dep in currentPkg.Dependencies)
11931198
{
1194-
FindDependencyPackageVersion(dep, currentServer, currentResponseUtil, currentPkg, repository, errors);
1199+
debugMsgs.Enqueue($"Finding dependency '{dep.Name}' version range '{dep.VersionRange}'");
1200+
FindDependencyPackageVersion(dep, currentServer, currentResponseUtil, currentPkg, repository, errorMsgs, verboseMsgs, warningMsgs);
11951201
}
11961202
}
1197-
foreach (ErrorRecord error in errors)
1203+
1204+
// Write out all log messages collected from Parallel.ForEach
1205+
while (debugMsgs.TryDequeue(out string logMsg))
1206+
{
1207+
_cmdletPassedIn.WriteDebug(logMsg);
1208+
}
1209+
1210+
while (verboseMsgs.TryDequeue(out string verboseMsg))
1211+
{
1212+
_cmdletPassedIn.WriteVerbose(verboseMsg);
1213+
}
1214+
1215+
while (warningMsgs.TryDequeue(out string warningMsg))
1216+
{
1217+
_cmdletPassedIn.WriteWarning(warningMsg);
1218+
}
1219+
1220+
// Write out all errors collected from Parallel.ForEach
1221+
while (errorMsgs.TryDequeue(out ErrorRecord error))
11981222
{
11991223
_cmdletPassedIn.WriteError(error);
12001224
}
12011225
}
12021226
}
12031227

12041228
// Method 3
1205-
private void FindDependencyPackageVersion(Dependency dep, ServerApiCall currentServer, ResponseUtil currentResponseUtil, PSResourceInfo currentPkg, PSRepositoryInfo repository, ConcurrentBag<ErrorRecord> errors)
1229+
private void FindDependencyPackageVersion(
1230+
Dependency dep,
1231+
ServerApiCall currentServer,
1232+
ResponseUtil currentResponseUtil,
1233+
PSResourceInfo currentPkg,
1234+
PSRepositoryInfo repository,
1235+
ConcurrentQueue<ErrorRecord> errorMsgs,
1236+
ConcurrentQueue<string> verboseMsgs,
1237+
ConcurrentQueue<string> warningMsgs)
12061238
{
12071239
PSResourceInfo depPkg = null;
12081240

@@ -1212,12 +1244,13 @@ private void FindDependencyPackageVersion(Dependency dep, ServerApiCall currentS
12121244
// Check if the latest version is cached
12131245
if (_knownLatestPkgVersion.TryGetValue(dep.Name, out PSResourceInfo cachedDepPkg))
12141246
{
1247+
verboseMsgs.Enqueue($"Dependency '{dep.Name}', with no upper bound version, was found in cache");
12151248
depPkg = cachedDepPkg;
12161249
}
12171250
else
12181251
{
12191252
// Find this version from the server
1220-
depPkg = FindDependencyWithLowerBound(dep, currentServer, currentResponseUtil, currentPkg, repository, errors);
1253+
depPkg = FindDependencyWithLowerBound(dep, currentServer, currentResponseUtil, currentPkg, repository, errorMsgs, debugMsgs);
12211254
}
12221255
}
12231256
else if (dep.VersionRange.HasLowerBound && dep.VersionRange.MinVersion.Equals(dep.VersionRange.MaxVersion))
@@ -1229,11 +1262,12 @@ private void FindDependencyPackageVersion(Dependency dep, ServerApiCall currentS
12291262
NuGetVersion.TryParse(cachedRangePkg.Version?.ToString(), out NuGetVersion cachedPkgVersion) &&
12301263
dep.VersionRange.Satisfies(cachedPkgVersion))
12311264
{
1265+
verboseMsgs.Enqueue($"Dependency '{dep.Name}', with exact version, was found in cache");
12321266
depPkg = cachedRangePkg;
12331267
}
12341268
else
12351269
{
1236-
depPkg = FindDependencyWithSpecificVersion(dep, currentServer, currentResponseUtil, currentPkg, repository, errors);
1270+
depPkg = FindDependencyWithSpecificVersion(dep, currentServer, currentResponseUtil, currentPkg, repository, errorMsgs, debugMsgs);
12371271
}
12381272
}
12391273
else
@@ -1244,17 +1278,26 @@ private void FindDependencyPackageVersion(Dependency dep, ServerApiCall currentS
12441278
NuGetVersion.TryParse(cachedRangePkg.Version?.ToString(), out NuGetVersion cachedPkgVersion) &&
12451279
dep.VersionRange.Satisfies(cachedPkgVersion))
12461280
{
1281+
verboseMsgs.Enqueue($"Dependency '{dep.Name}', with upper bound version, was found in cache");
12471282
depPkg = cachedRangePkg;
12481283
}
12491284
else
12501285
{
1251-
depPkg = FindDependencyWithUpperBound(dep, currentServer, currentResponseUtil, currentPkg, repository, errors);
1286+
depPkg = FindDependencyWithUpperBound(dep, currentServer, currentResponseUtil, currentPkg, repository, errorMsgs, debugMsgs);
12521287
}
12531288
}
12541289
}
12551290

12561291
// Method 4
1257-
private PSResourceInfo FindDependencyWithSpecificVersion(Dependency dep, ServerApiCall currentServer, ResponseUtil currentResponseUtil, PSResourceInfo currentPkg, PSRepositoryInfo repository, ConcurrentBag<ErrorRecord> errors)
1292+
private PSResourceInfo FindDependencyWithSpecificVersion(
1293+
Dependency dep,
1294+
ServerApiCall currentServer,
1295+
ResponseUtil currentResponseUtil,
1296+
PSResourceInfo currentPkg,
1297+
PSRepositoryInfo repository,
1298+
ConcurrentQueue<ErrorRecord> errorMsgs,
1299+
ConcurrentQueue<string> verboseMsgs,
1300+
ConcurrentQueue<string> warningMsgs)
12581301
{
12591302
PSResourceInfo depPkg = null;
12601303
ErrorRecord errRecord = null;
@@ -1265,6 +1308,7 @@ private PSResourceInfo FindDependencyWithSpecificVersion(Dependency dep, ServerA
12651308
{
12661309
// See if the network call we're making is already cached, if not, call FindNameAsync() and cache results
12671310
string key = $"{dep.Name}|{dep.VersionRange.MaxVersion.ToString()}|{_type}";
1311+
verboseMsgs.Enqueue("Checking if network call is cached.");
12681312
response = _cachedNetworkCalls.GetOrAdd(key, _ => currentServer.FindVersionAsync(dep.Name, dep.VersionRange.MaxVersion.ToString(), _type));
12691313

12701314
responses = response.GetAwaiter().GetResult();
@@ -1278,7 +1322,7 @@ private PSResourceInfo FindDependencyWithSpecificVersion(Dependency dep, ServerA
12781322
// Error handling and Convert to PSResource object
12791323
if (errRecord != null)
12801324
{
1281-
errors.Add(new ErrorRecord(
1325+
errorMsgs.Enqueue(new ErrorRecord(
12821326
new ResourceNotFoundException($"Dependency package could not be found: '{errRecord.Exception.Message}'"),
12831327
"DependencyPackageNotFound",
12841328
ErrorCategory.ObjectNotFound,
@@ -1289,7 +1333,7 @@ private PSResourceInfo FindDependencyWithSpecificVersion(Dependency dep, ServerA
12891333
PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).FirstOrDefault();
12901334
if (currentResult == null || currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
12911335
{
1292-
errors.Add(new ErrorRecord(
1336+
errorMsgs.Enqueue(new ErrorRecord(
12931337
new ResourceNotFoundException($"Dependency package with name '{dep.Name}' and version range '{dep.VersionRange}' could not be found in repository '{repository.Name}'", currentResult?.exception ?? new ItemNotFoundException()),
12941338
"DependencyPackageNotFound",
12951339
ErrorCategory.ObjectNotFound,
@@ -1301,12 +1345,14 @@ private PSResourceInfo FindDependencyWithSpecificVersion(Dependency dep, ServerA
13011345
TryAddToKnownLatestPkgVersion(depPkg);
13021346

13031347
string pkgVersion = FormatPkgVersionString(depPkg);
1348+
verboseMsgs.Enqueue($"Found dependency '{depPkg.Name}' version '{pkgVersion}'");
13041349
string key = $"{depPkg.Name}{pkgVersion}";
13051350
if (!depPkgsFound.ContainsKey(key))
13061351
{
13071352
// Add pkg to collection of packages found then find dependencies
13081353
// depPkgsFound creates a new instance of depPkgsFound each time FindDependencyPackages() is called.
13091354
// This will eventually return the PSResourceInfo object to the main cmdlet class.
1355+
verboseMsgs.Enqueue($"Adding'{key}' to list of dependency packages found");
13101356
depPkgsFound.TryAdd(key, depPkg);
13111357
FindDependencyPackagesHelper(currentServer, currentResponseUtil, depPkg, repository);
13121358
}
@@ -1317,7 +1363,14 @@ private PSResourceInfo FindDependencyWithSpecificVersion(Dependency dep, ServerA
13171363
}
13181364

13191365
// Method 5
1320-
private PSResourceInfo FindDependencyWithLowerBound(Dependency dep, ServerApiCall currentServer, ResponseUtil currentResponseUtil, PSResourceInfo currentPkg, PSRepositoryInfo repository, ConcurrentBag<ErrorRecord> errors)
1366+
private PSResourceInfo FindDependencyWithLowerBound(
1367+
Dependency dep,
1368+
ServerApiCall currentServer,
1369+
ResponseUtil currentResponseUtil,
1370+
PSResourceInfo currentPkg,
1371+
PSRepositoryInfo repository,
1372+
ConcurrentQueue<ErrorRecord> errorMsgs,
1373+
ConcurrentQueue<string> verboseMsgs)
13211374
{
13221375
PSResourceInfo depPkg = null;
13231376
FindResults responses = null;
@@ -1328,6 +1381,7 @@ private PSResourceInfo FindDependencyWithLowerBound(Dependency dep, ServerApiCal
13281381
{
13291382
// See if the network call we're making is already cached, if not, call FindNameAsync() and cache results
13301383
string key = $"{dep.Name}|*|{_type}";
1384+
verboseMsgs.Enqueue("Checking if network call is cached.");
13311385
response = _cachedNetworkCalls.GetOrAdd(key, _ => currentServer.FindNameAsync(dep.Name, includePrerelease: true, _type));
13321386

13331387
responses = response.GetAwaiter().GetResult();
@@ -1340,7 +1394,7 @@ private PSResourceInfo FindDependencyWithLowerBound(Dependency dep, ServerApiCal
13401394
// Error handling and Convert to PSResource object
13411395
if (errRecord != null)
13421396
{
1343-
errors.Add(new ErrorRecord(
1397+
errorMsgs.Enqueue(new ErrorRecord(
13441398
new ResourceNotFoundException($"Dependency package could not be found: '{errRecord.Exception.Message}'"),
13451399
"DependencyPackageNotFound",
13461400
ErrorCategory.ObjectNotFound,
@@ -1351,7 +1405,7 @@ private PSResourceInfo FindDependencyWithLowerBound(Dependency dep, ServerApiCal
13511405
PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).FirstOrDefault();
13521406
if (currentResult == null || currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
13531407
{
1354-
errors.Add(new ErrorRecord(
1408+
errorMsgs.Enqueue(new ErrorRecord(
13551409
new ResourceNotFoundException($"Dependency package with name '{dep.Name}' and version range '{dep.VersionRange}' could not be found in repository '{repository.Name}'", currentResult?.exception ?? new ItemNotFoundException()),
13561410
"DependencyPackageNotFound",
13571411
ErrorCategory.ObjectNotFound,
@@ -1363,12 +1417,14 @@ private PSResourceInfo FindDependencyWithLowerBound(Dependency dep, ServerApiCal
13631417
TryAddToKnownLatestPkgVersion(depPkg);
13641418

13651419
string pkgVersion = FormatPkgVersionString(depPkg);
1420+
verboseMsgs.Enqueue($"Found dependency '{depPkg.Name}' version '{pkgVersion}'");
13661421
string key = $"{depPkg.Name}{pkgVersion}";
13671422
if (!depPkgsFound.ContainsKey(key))
13681423
{
13691424
// Add pkg to collection of packages found then find dependencies
13701425
// depPkgsFound creates a new instance of depPkgsFound each time FindDependencyPackages() is called.
13711426
// This will eventually return the PSResourceInfo object to the main cmdlet class.
1427+
verboseMsgs.Enqueue($"Adding'{key}' to list of dependency packages found");
13721428
depPkgsFound.TryAdd(key, depPkg);
13731429
FindDependencyPackagesHelper(currentServer, currentResponseUtil, depPkg, repository);
13741430
}
@@ -1379,7 +1435,14 @@ private PSResourceInfo FindDependencyWithLowerBound(Dependency dep, ServerApiCal
13791435
}
13801436

13811437
// Method 6
1382-
private PSResourceInfo FindDependencyWithUpperBound(Dependency dep, ServerApiCall currentServer, ResponseUtil currentResponseUtil, PSResourceInfo currentPkg, PSRepositoryInfo repository, ConcurrentBag<ErrorRecord> errors)
1438+
private PSResourceInfo FindDependencyWithUpperBound(
1439+
Dependency dep,
1440+
ServerApiCall currentServer,
1441+
ResponseUtil currentResponseUtil,
1442+
PSResourceInfo currentPkg,
1443+
PSRepositoryInfo repository,
1444+
ConcurrentQueue<ErrorRecord> errorMsgs,
1445+
ConcurrentQueue<string> verboseMsgs)
13831446
{
13841447
PSResourceInfo depPkg = null;
13851448
ErrorRecord errRecord = null;
@@ -1392,6 +1455,7 @@ private PSResourceInfo FindDependencyWithUpperBound(Dependency dep, ServerApiCal
13921455
{
13931456
// See if the network call we're making is already caced, if not, call FindNameAsync() and cache results
13941457
string key = $"{dep.Name}|{dep.VersionRange.MaxVersion.ToString()}|{_type}";
1458+
verboseMsgs.Enqueue("Checking if network call is cached.");
13951459
response = cachedNetworkCalls.GetOrAdd(key, _ => currentServer.FindVersionGlobbingAsync(dep.Name, dep.VersionRange, includePrerelease: true, ResourceType.None, getOnlyLatest: true));
13961460

13971461
responses = response.GetAwaiter().GetResult();
@@ -1405,7 +1469,7 @@ private PSResourceInfo FindDependencyWithUpperBound(Dependency dep, ServerApiCal
14051469
// Error handling and Convert to PSResource object
14061470
if (errRecord != null)
14071471
{
1408-
errors.Add(new ErrorRecord(
1472+
errorMsgs.Enqueue(new ErrorRecord(
14091473
new ResourceNotFoundException($"Dependency package could not be found: '{errRecord.Exception.Message}'"),
14101474
"DependencyPackageNotFound",
14111475
ErrorCategory.ObjectNotFound,
@@ -1416,7 +1480,7 @@ private PSResourceInfo FindDependencyWithUpperBound(Dependency dep, ServerApiCal
14161480
PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).FirstOrDefault();
14171481
if (currentResult == null || currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
14181482
{
1419-
errors.Add(new ErrorRecord(
1483+
errorMsgs.Enqueue(new ErrorRecord(
14201484
new ResourceNotFoundException($"Dependency package with name '{dep.Name}' and version range '{dep.VersionRange}' could not be found in repository '{repository.Name}'", currentResult?.exception ?? new ItemNotFoundException()),
14211485
"DependencyPackageNotFound",
14221486
ErrorCategory.ObjectNotFound,
@@ -1429,12 +1493,14 @@ private PSResourceInfo FindDependencyWithUpperBound(Dependency dep, ServerApiCal
14291493
TryAddToKnownLatestPkgVersion(depPkg);
14301494

14311495
string pkgVersion = FormatPkgVersionString(depPkg);
1496+
verboseMsgs.Enqueue($"Found dependency '{depPkg.Name}' version '{pkgVersion}'");
14321497
string key = $"{depPkg.Name}{pkgVersion}";
14331498
if (!depPkgsFound.ContainsKey(key))
14341499
{
14351500
// Add pkg to collection of packages found then find dependencies
14361501
// depPkgsFound creates a new instance of depPkgsFound each time FindDependencyPackages() is called.
14371502
// This will eventually return the PSResourceInfo object to the main cmdlet class.
1503+
verboseMsgs.Enqueue($"Adding'{key}' to list of dependency packages found");
14381504
depPkgsFound.TryAdd(key, depPkg);
14391505
FindDependencyPackagesHelper(currentServer, currentResponseUtil, depPkg, repository);
14401506
}

0 commit comments

Comments
 (0)