Example #1
0
        public void TestProcessJobs()
        {
            Guid jobId       = Guid.NewGuid();
            var  testJobInfo = new ExtractJobInfo(
                jobId,
                DateTime.UtcNow,
                "123",
                "test/dir",
                "KeyTag",
                123,
                null,
                ExtractJobStatus.ReadyForChecks,
                isIdentifiableExtraction: true,
                isNoFilterExtraction: true
                );

            var opts = new CohortPackagerOptions {
                JobWatcherTimeoutInSeconds = 123
            };
            var mockJobStore = new Mock <IExtractJobStore>();
            var callbackUsed = false;
            var mockCallback = new Action <Exception>(_ => callbackUsed = true);
            var testNotifier = new TestJobCompleteNotifier();
            var testReporter = new TestJobReporter();

            var watcher = new ExtractJobWatcher(opts, mockJobStore.Object, mockCallback, testNotifier, testReporter);

            // Check that we can call ProcessJobs with no Guid to process all jobs
            mockJobStore.Setup(x => x.GetReadyJobs(default(Guid))).Returns(new List <ExtractJobInfo>());
            watcher.ProcessJobs();
            mockJobStore.Verify();

            // Check that we MarkJobFailed for known exceptions
            mockJobStore.Reset();
            mockJobStore.Setup(x => x.GetReadyJobs(It.IsAny <Guid>())).Returns(new List <ExtractJobInfo> {
                testJobInfo
            });
            mockJobStore.Setup(x => x.MarkJobCompleted(It.IsAny <Guid>())).Throws(new ApplicationException("aah"));
            watcher.ProcessJobs(jobId);
            mockJobStore.Verify(x => x.MarkJobFailed(jobId, It.IsAny <ApplicationException>()), Times.Once);

            // Check that we call the exception callback for unhandled exceptions
            mockJobStore.Reset();
            mockJobStore.Setup(x => x.GetReadyJobs(It.IsAny <Guid>())).Returns(new List <ExtractJobInfo> {
                testJobInfo
            });
            mockJobStore.Setup(x => x.MarkJobCompleted(It.IsAny <Guid>())).Throws(new Exception("aah"));
            watcher.ProcessJobs(jobId);
            Assert.True(callbackUsed);

            // Check happy path
            mockJobStore.Reset();
            mockJobStore.Setup(x => x.GetReadyJobs(It.IsAny <Guid>())).Returns(new List <ExtractJobInfo> {
                testJobInfo
            });
            testNotifier.Notified = false;
            watcher.ProcessJobs(jobId);
            Assert.True(testNotifier.Notified);
            Assert.True(testReporter.Reported);
        }
Example #2
0
        /// <summary>
        /// Default constructor for CohortPackagerHost
        /// </summary>
        /// <param name="globals"></param>
        /// <param name="jobStore"></param>
        /// <param name="fileSystem"></param>
        /// <param name="reporter">
        /// Pass to override the default IJobReporter that will be created from
        /// Globals.CohortPackagerOptions.ReportFormat. That value should not be set if a reporter is passed.
        /// </param>
        /// <param name="notifier"></param>
        /// <param name="rabbitMqAdapter"></param>
        /// <param name="dateTimeProvider"></param>
        public CohortPackagerHost(
            [NotNull] GlobalOptions globals,
            [CanBeNull] ExtractJobStore jobStore          = null,
            [CanBeNull] IFileSystem fileSystem            = null,
            [CanBeNull] IJobReporter reporter             = null,
            [CanBeNull] IJobCompleteNotifier notifier     = null,
            [CanBeNull] IRabbitMqAdapter rabbitMqAdapter  = null,
            [CanBeNull] DateTimeProvider dateTimeProvider = null
            )
            : base(globals, rabbitMqAdapter)
        {
            if (jobStore == null)
            {
                MongoDbOptions mongoDbOptions = Globals.MongoDatabases.ExtractionStoreOptions;
                jobStore = new MongoExtractJobStore(
                    MongoClientHelpers.GetMongoClient(mongoDbOptions, HostProcessName),
                    mongoDbOptions.DatabaseName,
                    dateTimeProvider
                    );
            }
            else if (dateTimeProvider != null)
            {
                throw new ArgumentException("jobStore and dateTimeProvider are mutually exclusive arguments");
            }

            // If not passed a reporter or notifier, try and construct one from the given options

            string reportFormatStr = Globals.CohortPackagerOptions.ReportFormat;

            if (reporter == null)
            {
                reporter = JobReporterFactory.GetReporter(
                    Globals.CohortPackagerOptions.ReporterType,
                    jobStore,
                    fileSystem ?? new FileSystem(),
                    Globals.FileSystemOptions.ExtractRoot,
                    reportFormatStr,
                    Globals.CohortPackagerOptions.ReportNewLine
                    );
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(reportFormatStr))
                {
                    throw new ArgumentException($"Passed an IJobReporter, but this conflicts with the ReportFormat of '{reportFormatStr}' in the given options");
                }
                if (fileSystem != null)
                {
                    throw new ArgumentException("Passed a fileSystem, but this will be unused as also passed an existing IJobReporter");
                }
            }

            notifier ??= JobCompleteNotifierFactory.GetNotifier(
                Globals.CohortPackagerOptions.NotifierType
                );

            _jobWatcher = new ExtractJobWatcher(
                globals.CohortPackagerOptions,
                jobStore,
                ExceptionCallback,
                notifier,
                reporter
                );

            AddControlHandler(new CohortPackagerControlMessageHandler(_jobWatcher));

            // Setup our consumers
            _requestInfoMessageConsumer      = new ExtractionRequestInfoMessageConsumer(jobStore);
            _fileCollectionMessageConsumer   = new ExtractFileCollectionMessageConsumer(jobStore);
            _anonFailedMessageConsumer       = new AnonFailedMessageConsumer(jobStore);
            _anonVerificationMessageConsumer = new AnonVerificationMessageConsumer(jobStore);
        }