public async Task PeekFor_NewJobs_ReturnsAvailableJobs()
        {
            var exampleQueueId = QueueId.Parse("ExampleQueue");
            var exampleQuery   = new PeekNextOptions
            {
                QueueId = exampleQueueId
            };

            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 MongoJobPeekNextService(new MongoJobQueryService(database));

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

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

                Assert.That(results?.First()?.Id, Is.EqualTo(newlyCreatedJobId));
            }).ConfigureAwait(false);
        }
Exemple #2
0
        protected static async Task <Job> TryProcessOneJobUsingPeekThanAck(IMongoDatabase database, QueueId exampleQueueName, JobAttributes attributesThatShouldWork)
        {
            // Query for next available a job for my datacenter
            var peekNextService = new MongoJobPeekNextService(new MongoJobQueryService(database));
            var peekQuery       = new PeekNextOptions
            {
                QueueId       = exampleQueueName,
                HasAttributes = attributesThatShouldWork,
                Limit         = 5
            };
            var jobs = (await peekNextService.PeekFor(peekQuery).ConfigureAwait(false)).ToList();

            if (!jobs.Any())
            {
                return(null);
            }

            foreach (var job in jobs)
            {
                // Acknowledge the job
                var acknowledgmentService = new MongoJobAcknowledgmentService(database);
                var standardAck           = new JobAcknowledgment
                {
                    { "RunnerId", Guid.NewGuid().ToString("N") }
                };
                var ackResult = await acknowledgmentService.Ack(exampleQueueName, job.Id, standardAck).ConfigureAwait(false);

                if (!ackResult.Success)
                {
                    continue;
                }

                var exampleReportMessage1 = "FooBar";
                var exampleReportMessage2 = "WizBang";
                var exampleReportMessage3 = "PowPop";

                // Send Reports
                var reportService = new MongoJobReportService(database);
                await
                reportService.AddReport(exampleQueueName, job.Id,
                                        new JobReport { { "Timestamp", DateTime.UtcNow.ToString("O") }, { "Message", exampleReportMessage1 } }).ConfigureAwait(false);

                await
                reportService.AddReport(exampleQueueName, job.Id,
                                        new JobReport { { "Timestamp", DateTime.UtcNow.ToString("O") }, { "Message", exampleReportMessage2 } }).ConfigureAwait(false);

                await
                reportService.AddReport(exampleQueueName, job.Id,
                                        new JobReport { { "Timestamp", DateTime.UtcNow.ToString("O") }, { "Message", exampleReportMessage3 } }).ConfigureAwait(false);

                // Send Result
                var completionService = new MongoJobCompletionService(database);
                await completionService.Complete(exampleQueueName, job.Id, new JobResult { { "Result", "Success" } }).ConfigureAwait(false);

                // Finish Job
                var finishedJobFromDb =
                    await database.GetJobCollection().Find(Builders <Job> .Filter.Eq(x => x.Id, job.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);
            }
            return(null);
        }