public async Task TakeFor_NewJobs_ReturnsAvailableJobAndAcknowledges() { var exampleQueueId = QueueId.Parse("ExampleQueue"); var exampleOptions = new TakeNextOptions { QueueId = exampleQueueId, Acknowledgment = new JobAcknowledgment { { "RunnerId", Guid.NewGuid().ToString("N") } } }; var newlyCreatedJobId = JobId.Generate(); var existingJobs = new[] { new Job { Id = JobId.Generate(), QueueId = exampleQueueId, Result = new JobResult { { "Result", "Success" } }, Acknowledgment = new JobAcknowledgment { { "RunnerId", Guid.NewGuid().ToString("N") } } }, new Job { Id = JobId.Generate(), QueueId = exampleQueueId, Acknowledgment = new JobAcknowledgment { { "RunnerId", Guid.NewGuid().ToString("N") } } }, new Job { Id = newlyCreatedJobId, QueueId = exampleQueueId } }; await RunInMongoLand(async database => { var jobs = database.GetJobCollection(); await jobs.InsertManyAsync(existingJobs).ConfigureAwait(false); var sut = new MongoJobTakeNextService(database); var result = await sut.TakeFor(exampleOptions).ConfigureAwait(false); Assert.That(result, Is.Not.Null); Assert.That(result?.Id, Is.EqualTo(newlyCreatedJobId)); Assert.That(result?.Acknowledgment, Is.Not.Null); Assert.That(result?.Acknowledgment["RunnerId"], Is.EqualTo(exampleOptions.Acknowledgment["RunnerId"])); }).ConfigureAwait(false); }
public async Task TakeFor_NewJobs_OnlyModifiesOneJob() { var exampleQueueId = QueueId.Parse("ExampleQueue"); var exampleOptions = new TakeNextOptions { QueueId = exampleQueueId, Acknowledgment = new JobAcknowledgment { { "RunnerId", Guid.NewGuid().ToString("N") } } }; var newlyCreatedJobId1 = JobId.Generate(); var newlyCreatedJobId2 = JobId.Generate(); var existingJobs = new[] { new Job { Id = JobId.Generate(), QueueId = exampleQueueId, Result = new JobResult { { "Result", "Success" } }, Acknowledgment = new JobAcknowledgment { { "RunnerId", Guid.NewGuid().ToString("N") } } }, new Job { Id = JobId.Generate(), QueueId = exampleQueueId, Acknowledgment = new JobAcknowledgment { { "RunnerId", Guid.NewGuid().ToString("N") } } }, new Job { Id = newlyCreatedJobId1, QueueId = exampleQueueId }, new Job { Id = newlyCreatedJobId2, QueueId = exampleQueueId } }; await RunInMongoLand(async database => { var jobs = database.GetJobCollection(); await jobs.InsertManyAsync(existingJobs).ConfigureAwait(false); var sut = new MongoJobTakeNextService(database); var result = await sut.TakeFor(exampleOptions).ConfigureAwait(false); Assert.That(result, Is.Not.Null); Assert.That(result?.Id, Is.EqualTo(newlyCreatedJobId1)); var jobInDb1 = await jobs.Find(Builders <Job> .Filter.Eq(x => x.Id, newlyCreatedJobId1)).SingleAsync().ConfigureAwait(false); Assert.That(jobInDb1.Acknowledgment, Is.Not.Null); Assert.That(jobInDb1.Acknowledgment["RunnerId"], Is.EqualTo(exampleOptions.Acknowledgment["RunnerId"])); var jobInDb2 = await jobs.Find(Builders <Job> .Filter.Eq(x => x.Id, newlyCreatedJobId2)).SingleAsync().ConfigureAwait(false); Assert.That(jobInDb2.Acknowledgment, Is.Null); }).ConfigureAwait(false); }
protected static async Task <Job> TryProcessOneJobUsingTakeNext(IMongoDatabase database, QueueId exampleQueueName, JobAttributes attributesThatShouldWork) { // Take Next available job var takeNextService = new MongoJobTakeNextService(database); var standardAck = new JobAcknowledgment { { "RunnerId", Guid.NewGuid().ToString("N") } }; var peekQuery = new TakeNextOptions { QueueId = exampleQueueName, HasAttributes = attributesThatShouldWork, Acknowledgment = standardAck }; var nextJob = await takeNextService.TakeFor(peekQuery).ConfigureAwait(false); if (nextJob == null) { return(null); } var exampleReportMessage1 = "FooBar"; var exampleReportMessage2 = "WizBang"; var exampleReportMessage3 = "PowPop"; // Send Reports var reportService = new MongoJobReportService(database); await reportService.AddReport(exampleQueueName, nextJob.Id, new JobReport { { "Timestamp", DateTime.UtcNow.ToString("O") }, { "Message", exampleReportMessage1 } }).ConfigureAwait(false); await reportService.AddReport(exampleQueueName, nextJob.Id, new JobReport { { "Timestamp", DateTime.UtcNow.ToString("O") }, { "Message", exampleReportMessage2 } }).ConfigureAwait(false); await reportService.AddReport(exampleQueueName, nextJob.Id, new JobReport { { "Timestamp", DateTime.UtcNow.ToString("O") }, { "Message", exampleReportMessage3 } }).ConfigureAwait(false); // Send Result var completionService = new MongoJobCompletionService(database); await completionService.Complete(exampleQueueName, nextJob.Id, new JobResult { { "Result", "Success" } }).ConfigureAwait(false); // Finish Job var finishedJobFromDb = await database.GetJobCollection().Find(Builders <Job> .Filter.Eq(x => x.Id, nextJob.Id)).FirstAsync().ConfigureAwait(false); Assert.That(finishedJobFromDb, Is.Not.Null); Assert.That(finishedJobFromDb.Acknowledgment, Is.Not.Null); Assert.That(finishedJobFromDb.Reports, Has.Length.EqualTo(3)); var valuesOfReports = finishedJobFromDb.Reports.SelectMany(x => x.Values).ToList(); Assert.That(valuesOfReports, Contains.Item(exampleReportMessage1)); Assert.That(valuesOfReports, Contains.Item(exampleReportMessage2)); Assert.That(valuesOfReports, Contains.Item(exampleReportMessage3)); Assert.That(finishedJobFromDb.Result, Is.Not.Null); return(finishedJobFromDb); }
public async Task RollingDeploy_ModelSubJobsOrchestratedByParent() { var exampleQueueName = QueueId.Parse("Wind"); await RunInMongoLand(async database => { var cancellationSource = new CancellationTokenSource(); cancellationSource.CancelAfter(MaxTestTime); var cancellationToken = cancellationSource.Token; var finishedJobs = new ConcurrentBag <Job>(); await SetupTakeNextWorkerWithSubscription(database, exampleQueueName, finishedJobs, new JobAttributes { { "Name", "DeployServer" }, }, cancellationToken).ConfigureAwait(false); // Create a rolling deploy relevant for my datacenter var creationService = new MongoJobCreationService(database); await creationService.Create(exampleQueueName, new JobAttributes { { "Name", "RollingDeploy" }, { "Environment", "Prod" } }).ConfigureAwait(false); // Take Next available job var takeNextSubscriber = new TaskBasedTakeNextSubscriber(new MongoJobTakeNextService(database)); var standardAck = new JobAcknowledgment { { "RunnerId", Guid.NewGuid().ToString("N") } }; var peekQuery = new TakeNextOptions { QueueId = exampleQueueName, HasAttributes = new JobAttributes { { "Environment", "Prod" } }, Acknowledgment = standardAck }; await takeNextSubscriber.Subscribe(async rollingDeployJob => { // Send Reports var reportService = new MongoJobReportService(database); await reportService.AddReport(exampleQueueName, rollingDeployJob.Id, new JobReport { { "Timestamp", DateTime.UtcNow.ToString("O") }, { "Message", "Starting Rolling Deploy" } }).ConfigureAwait(false); var queryService = new MongoJobQueryService(database); IEnumerable servers = new[] { "PROD2", "PROD1" }; foreach (var server in servers) { var deployServer = new JobAttributes { { "Name", "DeployServer" }, { "Server", server } }; await reportService.AddReport(exampleQueueName, rollingDeployJob.Id, new JobReport { { "Timestamp", DateTime.UtcNow.ToString("O") }, { "Message", $"Requesting Deploy on server {server}" } }).ConfigureAwait(false); var deployServerJobId = await creationService.Create(exampleQueueName, deployServer).ConfigureAwait(false); Job hasResult; do { // replace with detail service hasResult = (await queryService.QueryFor(new JobQuery { QueueId = exampleQueueName, JobIds = new[] { deployServerJobId }, HasResult = true }).ConfigureAwait(false)).FirstOrDefault(); Thread.Sleep(500); } while (hasResult == null); await reportService.AddReport(exampleQueueName, rollingDeployJob.Id, new JobReport { { "Timestamp", DateTime.UtcNow.ToString("O") }, { "Message", $"Deploy on server {server} Completed, {JsonConvert.SerializeObject(hasResult.Result)}" } }).ConfigureAwait(false); // inspect result } // Send Result var completionService = new MongoJobCompletionService(database); await completionService.Complete(exampleQueueName, rollingDeployJob.Id, new JobResult { { "Result", "Success" } }).ConfigureAwait(false); var finalizedRollingDeployJob = await queryService.QueryFor(new JobQuery { QueueId = exampleQueueName, JobIds = new[] { rollingDeployJob.Id } }).ConfigureAwait(false); cancellationSource.Cancel(); Console.WriteLine($"Finalized Rolling Deploy Job: {JsonConvert.SerializeObject(finalizedRollingDeployJob)}"); Assert.That(finalizedRollingDeployJob.First().Result["Result"], Is.EqualTo("Success")); }, new TakeNextSubscriptionOptions { TakeNextOptions = peekQuery, Token = cancellationToken, }).ConfigureAwait(false); }).ConfigureAwait(false); }