public int Run(IRDMPPlatformRepositoryServiceLocator repositoryLocator, IDataLoadEventListener listener, ICheckNotifier checkNotifier, GracefulCancellationToken token) { const string dataLoadTask = "caching"; CacheProgress cp = repositoryLocator.CatalogueRepository.GetObjectByID <CacheProgress>(_options.CacheProgress); var defaults = repositoryLocator.CatalogueRepository.GetServerDefaults(); var loggingServer = defaults.GetDefaultFor(PermissableDefaults.LiveLoggingServer_ID); if (loggingServer == null) { throw new NotSupportedException("No default logging server specified, you must specify one in "); } var logManager = new LogManager(loggingServer); logManager.CreateNewLoggingTaskIfNotExists(dataLoadTask); switch (_options.Command) { case CommandLineActivity.run: //Setup dual listeners for the Cache process, one ticks the lifeline one very message and one logs to the logging db var toLog = new ToLoggingDatabaseDataLoadEventListener(this, logManager, dataLoadTask, "Caching " + cp); var forkListener = new ForkDataLoadEventListener(toLog, listener); try { var cachingHost = new CachingHost(repositoryLocator.CatalogueRepository); cachingHost.RetryMode = _options.RetryMode; cachingHost.CacheProgressList = new ICacheProgress[] { cp }.ToList(); //run the cp //By default caching host will block cachingHost.TerminateIfOutsidePermissionWindow = true; cachingHost.Start(forkListener, token); } finally { //finish everything toLog.FinalizeTableLoadInfos(); } break; case CommandLineActivity.check: var checkable = new CachingPreExecutionChecker(cp); checkable.Check(checkNotifier); break; default: throw new ArgumentOutOfRangeException(); } return(0); }
public void FireItUpManually() { RepositoryLocator.CatalogueRepository.MEF.AddTypeToCatalogForTesting(typeof(TestDataWriter)); RepositoryLocator.CatalogueRepository.MEF.AddTypeToCatalogForTesting(typeof(TestDataInventor)); var cachingHost = new CachingHost(CatalogueRepository); cachingHost.CacheProgress = _cp; cachingHost.Start(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken()); // should be numDaysToCache days in cache Assert.AreEqual(NumDaysToCache, _LoadDirectory.Cache.GetFiles("*.csv").Count()); // make sure each file is named as expected var cacheFiles = _LoadDirectory.Cache.GetFiles().Select(fi => fi.Name).ToArray(); for (var i = -NumDaysToCache; i < 0; i++) { var filename = DateTime.Now.AddDays(i).ToString("yyyyMMdd") + ".csv"; Assert.IsTrue(cacheFiles.Contains(filename), filename + " not found"); } }
public void CacheHostOutwithPermissionWindow() { var rootDir = new DirectoryInfo(TestContext.CurrentContext.TestDirectory); var testDir = rootDir.CreateSubdirectory("C"); if (testDir.Exists) { Directory.Delete(testDir.FullName, true); } var loadDirectory = LoadDirectory.CreateDirectoryStructure(testDir, "Test"); var cp = WhenIHaveA <CacheProgress>(); var loadMetadata = cp.LoadProgress.LoadMetadata; loadMetadata.LocationOfFlatFiles = loadDirectory.RootPath.FullName; // This feels a bit nasty, but quick and much better than having the test wait for an arbitrary time period. var listener = new ExpectedNotificationListener("Download not permitted at this time, sleeping for 60 seconds"); cp.CacheFillProgress = DateTime.Now.AddDays(-1); cp.PermissionWindow_ID = 1; var permissionWindow = new PermissionWindow(Repository); permissionWindow.RequiresSynchronousAccess = true; permissionWindow.ID = 1; permissionWindow.Name = "Test Permission Window"; //Create a time period that we are outwith (1 hour ago to 30 minutes ago). TimeSpan start = DateTime.Now.TimeOfDay.Subtract(new TimeSpan(0, 1, 0, 0)); TimeSpan stop = DateTime.Now.TimeOfDay.Subtract(new TimeSpan(0, 0, 30, 0)); permissionWindow.SetPermissionWindowPeriods(new List <PermissionWindowPeriod>(new [] { new PermissionWindowPeriod( (int)DateTime.Now.DayOfWeek, start, stop) })); permissionWindow.SaveToDatabase(); cp.PermissionWindow_ID = permissionWindow.ID; cp.SaveToDatabase(); var dataFlowPipelineEngine = Mock.Of <IDataFlowPipelineEngine>(); // set up a factory stub to return our engine mock var cacheHost = new CachingHost(Repository) { CacheProgressList = new List <ICacheProgress> { cp } }; var stopTokenSource = new CancellationTokenSource(); var abortTokenSource = new CancellationTokenSource(); var cancellationToken = new GracefulCancellationToken(stopTokenSource.Token, abortTokenSource.Token); var task = Task.Run(() => cacheHost.Start(listener, cancellationToken), cancellationToken.CreateLinkedSource().Token); // Don't want to cancel before the DownloadUntilFinished loop starts and we receive the first "Download not permitted at this time, sleeping for 60 seconds" message listener.ReceivedMessage += abortTokenSource.Cancel; try { task.Wait(); } catch (AggregateException e) { Assert.AreEqual(1, e.InnerExceptions.Count); Assert.IsInstanceOf(typeof(TaskCanceledException), e.InnerExceptions[0], e.InnerExceptions[0].Message); } finally { testDir.Delete(true); } }