Example #1
0
        public async Task QueryFor_AJArrayOfStringsAndWithInvalidAdhocQuery_ThrowsException()
        {
            var exampleQueueId = QueueId.Parse("ExampleQueue");
            var exampleQuery   = new JobQuery
            {
                QueueId       = exampleQueueId,
                HasAttributes = new JobAttributes
                {
                    { "name", new JArray {
                          "DeployApi", "DeployWebsite"
                      } }
                },
                AdhocQuery = "SampeInvalidMongoQuery"
            };

            await RunInMongoLand(database =>
            {
                var sut = new MongoJobQueryService(database);

                var ex = Assert.ThrowsAsync <InvalidOperationException>(async() =>
                {
                    await sut.QueryFor(exampleQuery).ConfigureAwait(false);
                });

                Assert.That(ex.Message, Does.Contain("Invalid adhocQuery"));
                return(Task.FromResult(true));
            }).ConfigureAwait(false);
        }
Example #2
0
        public async Task QueryFor_HasResult_YieldsOnlyFinishedJobs()
        {
            var exampleQueueId = QueueId.Parse("ExampleQueue");
            var exampleQuery   = new JobQuery
            {
                QueueId   = exampleQueueId,
                HasResult = true
            };

            var matchingJob1  = JobId.Generate();
            var matchingJob2  = JobId.Generate();
            var unmatchedJob1 = JobId.Generate();

            var existingJobs = new[] { new Job
                                       {
                                           Id      = matchingJob1,
                                           QueueId = exampleQueueId,
                                           Result  = new JobResult
                                           {
                                               { "Result", "Failed" }
                                           }
                                       }, new Job
                                       {
                                           Id      = matchingJob2,
                                           QueueId = exampleQueueId,
                                           Result  = new JobResult
                                           {
                                               { "Result", "Success" }
                                           }
                                       }, new Job
                                       {
                                           Id      = unmatchedJob1,
                                           QueueId = exampleQueueId,
                                       } };

            await RunInMongoLand(async database =>
            {
                var jobs = database.GetJobCollection();

                await jobs.InsertManyAsync(existingJobs).ConfigureAwait(false);

                var sut = new MongoJobQueryService(database);

                var results = (await sut.QueryFor(exampleQuery).ConfigureAwait(false))?.ToList();

                Assert.That(results, Is.Not.Null);
                Assert.That(results, Has.Count.EqualTo(2));

                // ReSharper disable once AssignNullToNotNullAttribute
                var foundIds = results.Select(x => x.Id).ToList();

                Assert.That(foundIds, Contains.Item(matchingJob1));
                Assert.That(foundIds, Contains.Item(matchingJob2));
            }).ConfigureAwait(false);
        }
Example #3
0
        public async Task QueryFor_Limit_YieldsOnlyToTheLimit()
        {
            var exampleQueueId = QueueId.Parse("ExampleQueue");
            var exampleQuery   = new JobQuery
            {
                QueueId = exampleQueueId,
                Limit   = 2,
            };

            var existingJobs = new[] { new Job
                                       {
                                           Id      = JobId.Generate(),
                                           QueueId = exampleQueueId
                                       }, new Job
                                       {
                                           Id      = JobId.Generate(),
                                           QueueId = exampleQueueId
                                       }, new Job
                                       {
                                           Id      = JobId.Generate(),
                                           QueueId = exampleQueueId
                                       } };

            await RunInMongoLand(async database =>
            {
                var jobs = database.GetJobCollection();

                await jobs.InsertManyAsync(existingJobs).ConfigureAwait(false);

                var sut = new MongoJobQueryService(database);

                var results = (await sut.QueryFor(exampleQuery).ConfigureAwait(false))?.ToList();

                Assert.That(results, Is.Not.Null);
                Assert.That(results, Has.Count.EqualTo(2));
            }).ConfigureAwait(false);
        }
Example #4
0
        public async Task QueryFor_AJArrayOfStringsAndWithOrStyleAdhocQuery_ReturnsAllJobsMatching()
        {
            var matchingJob1  = JobId.Generate();
            var matchingJob2  = JobId.Generate();
            var matchingJob3  = JobId.Generate();
            var unmatchedJob1 = JobId.Generate();

            var exampleAcknowledgedDateTime1 = new DateTime(2010, 1, 22, 22, 00, 00, DateTimeKind.Utc);
            var exampleAcknowledgedDateTime2 = new DateTime(2010, 1, 22, 23, 00, 00, DateTimeKind.Utc);
            var exampleQueueId = QueueId.Parse("ExampleQueue");
            var exampleQuery   = new JobQuery
            {
                QueueId       = exampleQueueId,
                HasAttributes = new JobAttributes
                {
                    { "name", new JArray {
                          "DeployApi", "DeployWebsite"
                      } }
                },
                AdhocQuery = "{\"$or\" : [{ \"Acknowledgment.acknowledgedDateTime\": ISODate(\"2010-01-22T22:00:00.000Z\")},{ \"Acknowledgment.acknowledgedDateTime\": ISODate(\"2010-01-22T23:00:00.000Z\")}]}"
            };

            var existingJobs = new[] { new Job
                                       {
                                           Id         = matchingJob1,
                                           QueueId    = exampleQueueId,
                                           Attributes = new JobAttributes
                                           {
                                               { "name", "DeployApi" }
                                           }
                                       }, new Job
                                       {
                                           Id         = matchingJob2,
                                           QueueId    = exampleQueueId,
                                           Attributes = new JobAttributes
                                           {
                                               { "name", "DeployWebsite" }
                                           },
                                           Acknowledgment = new JobAcknowledgment
                                           {
                                               { "acknowledgedDateTime", exampleAcknowledgedDateTime1 }
                                           }
                                       }, new Job
                                       {
                                           Id         = unmatchedJob1,
                                           QueueId    = exampleQueueId,
                                           Attributes = new JobAttributes
                                           {
                                               { "name", "DeploySchema" }
                                           }
                                       }, new Job()
                                       {
                                           Id         = matchingJob3,
                                           QueueId    = exampleQueueId,
                                           Attributes = new JobAttributes
                                           {
                                               { "name", "DeployApi" }
                                           },
                                           Acknowledgment = new JobAcknowledgment
                                           {
                                               { "acknowledgedDateTime", exampleAcknowledgedDateTime2 }
                                           }
                                       } };

            await RunInMongoLand(async database =>
            {
                var jobs = database.GetJobCollection();

                await jobs.InsertManyAsync(existingJobs).ConfigureAwait(false);

                var sut = new MongoJobQueryService(database);

                var results = (await sut.QueryFor(exampleQuery).ConfigureAwait(false))?.ToList();

                Assert.That(results, Is.Not.Null);
                Assert.That(results, Has.Count.EqualTo(2));

                // ReSharper disable once AssignNullToNotNullAttribute
                var foundIds = results.Select(x => x.Id).ToList();

                Assert.That(foundIds, Contains.Item(matchingJob2));
                Assert.That(foundIds, Contains.Item(matchingJob3));
            }).ConfigureAwait(false);
        }
Example #5
0
        public async Task QueryFor_AJArrayOfIntegers_ReturnsAllJobsMatching()
        {
            var matchingJob1  = JobId.Generate();
            var matchingJob2  = JobId.Generate();
            var unmatchedJob1 = JobId.Generate();

            var exampleQueueId = QueueId.Parse("ExampleQueue");
            var exampleQuery   = new JobQuery
            {
                QueueId       = exampleQueueId,
                HasAttributes = new JobAttributes
                {
                    { "cpu", new JArray {
                          1, 7
                      } }
                }
            };

            var existingJobs = new[] { new Job
                                       {
                                           Id         = matchingJob1,
                                           QueueId    = exampleQueueId,
                                           Attributes = new JobAttributes
                                           {
                                               { "cpu", 1 }
                                           }
                                       }, new Job
                                       {
                                           Id         = matchingJob2,
                                           QueueId    = exampleQueueId,
                                           Attributes = new JobAttributes
                                           {
                                               { "cpu", 7 }
                                           }
                                       }, new Job
                                       {
                                           Id         = unmatchedJob1,
                                           QueueId    = exampleQueueId,
                                           Attributes = new JobAttributes
                                           {
                                               { "cpu", 8 }
                                           }
                                       } };

            await RunInMongoLand(async database =>
            {
                var jobs = database.GetJobCollection();

                await jobs.InsertManyAsync(existingJobs).ConfigureAwait(false);

                var sut = new MongoJobQueryService(database);

                var results = (await sut.QueryFor(exampleQuery).ConfigureAwait(false))?.ToList();

                Assert.That(results, Is.Not.Null);
                Assert.That(results, Has.Count.EqualTo(2));

                // ReSharper disable once AssignNullToNotNullAttribute
                var foundIds = results.Select(x => x.Id).ToList();

                Assert.That(foundIds, Contains.Item(matchingJob1));
                Assert.That(foundIds, Contains.Item(matchingJob2));
            }).ConfigureAwait(false);
        }
Example #6
0
        public async Task QueryFor_QueryByMultiAttributes_YieldMatchingResults()
        {
            var exampleQueueId = QueueId.Parse("ExampleQueue");
            var exampleQuery   = new JobQuery
            {
                QueueId       = exampleQueueId,
                HasAttributes = new JobAttributes
                {
                    { "DataCenter", "CAL01" },
                    { "Geo", "USA" }
                }
            };

            var matchingJob1  = JobId.Generate();
            var unmatchedJob1 = JobId.Generate();
            var unmatchedJob2 = JobId.Generate();

            var existingJobs = new[] { new Job
                                       {
                                           Id         = unmatchedJob1,
                                           QueueId    = exampleQueueId,
                                           Attributes = new JobAttributes
                                           {
                                               { "DataCenter", "CAL01" },
                                           }
                                       }, new Job
                                       {
                                           Id         = matchingJob1,
                                           QueueId    = exampleQueueId,
                                           Attributes = new JobAttributes
                                           {
                                               { "DataCenter", "CAL01" },
                                               { "Geo", "USA" }
                                           }
                                       }, new Job
                                       {
                                           Id         = unmatchedJob2,
                                           QueueId    = exampleQueueId,
                                           Attributes = new JobAttributes
                                           {
                                               { "Geo", "USA" },
                                           }
                                       } };

            await RunInMongoLand(async database =>
            {
                var jobs = database.GetJobCollection();

                await jobs.InsertManyAsync(existingJobs).ConfigureAwait(false);

                var sut = new MongoJobQueryService(database);

                var results = (await sut.QueryFor(exampleQuery).ConfigureAwait(false))?.ToList();

                Assert.That(results, Is.Not.Null);
                Assert.That(results, Has.Count.EqualTo(1));

                // ReSharper disable once AssignNullToNotNullAttribute
                var foundIds = results.Select(x => x.Id).ToList();

                Assert.That(foundIds, Contains.Item(matchingJob1));
            }).ConfigureAwait(false);
        }
        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);
        }