public PackageCacheStatusInfo GetCacheStatus(string mode, bool setAsReported = false) { PackageCacheStatusInfo cacheStatus; using (lockObject.Lock()) { cacheStatus = new PackageCacheStatusInfo(this.cacheStatusType, this.currentActionVerb, this.installStatusType, sweepIndex, sweepCount, lastSweepStart, lastCacheStatusRequest, pathsProcessed, memoryStatus); if (mode != "Agent") { cacheStatus = new PackageCacheStatusInfo(cacheStatus.CacheStatus, cacheStatus.StatusText); } cacheStatus.NoCaching = NO_CACHING; cacheStatus.NoInstallFromCache = NO_INSTALL_FROM_CACHE; if (setAsReported) { lastCacheStatusRequest = DateTime.Now; lastCacheStatusMode = mode; cacheStatus.SetLastStatusReported(pathsProcessed, lastCacheStatusRequest); } } return(cacheStatus); }
private void Process() { List <string> paths = null; Dictionary <string, PackageWorkingInstallFromCache> installs = null; var foldersCopied = 0; var foldersWithErrors = 0; var foldersSkipped = 0; var skipped = false; var stopwatch = new Stopwatch(); using (lockObject.Lock()) { paths = pathsToProcess.ToList(); installs = installsFromCacheToProcess.ToDictionary(); if (cacheStatusType < CacheStatusType.ServiceStarted) { cacheStatusType = CacheStatusType.ServiceStarted; } } if (installs != null) { if (installs.Count > 0) { using (lockObject.Lock()) { if (cacheStatusType < CacheStatusType.Installing) { cacheStatusType = CacheStatusType.Installing; installsFromCacheStatus.StatusSummary = "Installing from cache"; } } foreach (var install in installs.Values) { if (this.InstallFromCache(install)) { using (lockObject.Lock()) { installsFromCacheToProcess.Remove(install.Install); } } Thread.Sleep(1); } } } if (paths != null) { if (paths.Count > 0) { using (lockObject.Lock()) { if (cacheStatusType < CacheStatusType.Processing) { cacheStatusType = CacheStatusType.Processing; } } using (this.PushSetStatus(CacheStatusType.SweepingFiles)) { stopwatch.Start(); using (lockObject.Lock()) { sweepIndex = 0; sweepCount = paths.Count; lastSweepStart = DateTime.Now; if (paths.Count == 1) { this.WriteSweepLine("Sweeping {0} directory {1}", paths.Count, "*".Repeat(100)); } else { this.WriteSweepLine("Sweeping {0} directories {1}", paths.Count, "*".Repeat(100)); } } foreach (var path in paths) { try { var packagePath = Path.Combine(nodeModulesPath, path); var cachePath = Path.Combine(packageCachePath, path); var cacheDirectory = new DirectoryInfo(cachePath); var packageDirectory = new DirectoryInfo(packagePath); this.WriteSweepLine("Processing '{0}'", path); using (lockObject.Lock()) { sweepIndex++; } if (!Directory.Exists(cachePath)) { cacheDirectory.Create(); this.WriteSweepLine("Syncing directories for '{0}'", path); SyncDirectories(path, cacheDirectory, packageDirectory, ref skipped); if (skipped) { foldersSkipped++; } else { foldersCopied++; } } else { TimeSpan timeSpan; if (cacheDirectory.CompareTo(packageDirectory, out timeSpan)) { AddPathStatus(path, CacheStatusType.PathProcessed); using (lockObject.Lock()) { this.WriteSweepLine("Cache directory for '{0}' up to date. Comparison took {1} seconds", path, Math.Round(stopwatch.Elapsed.ToDecimalSeconds())); pathsToProcess.Remove(path); foldersSkipped++; } } else { if (CheckVersions(cachePath, packagePath)) { this.WriteSweepLine("Syncing directories for '{0}'. Comparison took {1} seconds", path, Math.Round(stopwatch.Elapsed.ToDecimalSeconds())); SyncDirectories(path, cacheDirectory, packageDirectory, ref skipped); if (skipped) { foldersSkipped++; } else { foldersCopied++; } } else { foldersSkipped++; } } } } catch (Exception ex) { AddPathStatus(path, ex); this.WriteSweepLine("Error for '{0}'. Exception: \r\n{1}", path, ex.Message); if (PackageCacheStatusInfo.GetPathErrorCount(path, pathsProcessed) > PATH_ERROR_LIMIT) { using (lockObject.Lock()) { this.WriteSweepLine("'{0}' reached error limit of {1}. Removing from list", path, PATH_ERROR_LIMIT); foldersWithErrors++; pathsToProcess.Remove(path); } } else { foldersSkipped++; } } Thread.Sleep(1); } stopwatch.Stop(); this.WriteSweepLine("End of sweep. Copied: {0}, Errored: {1}, Skipped: {2}, Sweep time elapsed: {3} seconds", foldersCopied, foldersWithErrors, foldersSkipped, Math.Round(stopwatch.Elapsed.ToDecimalSeconds())); } using (lockObject.Lock()) { cacheStatusType = CacheStatusType.EndOfProcessing; } } } }