private JobDto CreateJobInState(HangfireDbContext database, int jobId, string stateName)
        {
            var job = Job.FromExpression(() => SampleMethod("wrong"));

            var jobState = new StateDto
            {
                CreatedAt = database.GetServerTimeUtc(),
                Data = stateName == EnqueuedState.StateName
                           ? string.Format(" {{ 'EnqueuedAt': '{0}' }}", database.GetServerTimeUtc().ToString("o"))
                           : "{}",
                JobId = jobId
            };
            database.State.InsertOne(jobState);

            var jobDto = new JobDto
            {
                Id = jobId,
                InvocationData = JobHelper.ToJson(InvocationData.Serialize(job)),
                Arguments = "['Arguments']",
                StateName = stateName,
                CreatedAt = database.GetServerTimeUtc(),
                StateId = jobState.Id
            };
            database.Job.InsertOne(jobDto);

            var jobQueueDto = new JobQueueDto
            {
                FetchedAt = null,
                Id = jobId * 10,
                JobId = jobId,
                Queue = DefaultQueue
            };

            if (stateName == FetchedStateName)
            {
                jobQueueDto.FetchedAt = database.GetServerTimeUtc();
            }

            database.JobQueue.InsertOne(jobQueueDto);

            return jobDto;
        }
Esempio n. 2
0
        public void Dequeue_ShouldFetchAJob_FromTheSpecifiedQueue()
        {
            // Arrange
            UseConnection(connection =>
            {
                var jobQueue = new JobQueueDto
                {
                    JobId = 1,
                    Queue = "default"
                };

                connection.JobQueue.InsertOne(jobQueue);

                var id = jobQueue.Id;
                var queue = CreateJobQueue(connection);

                // Act
                MongoFetchedJob payload = (MongoFetchedJob)queue.Dequeue(DefaultQueues, CreateTimingOutCancellationToken());

                // Assert
                Assert.Equal(id, payload.Id);
                Assert.Equal("1", payload.JobId);
                Assert.Equal("default", payload.Queue);
            });
        }
Esempio n. 3
0
        public void Dequeue_ShouldFetchATimedOutJobs_FromTheSpecifiedQueue()
        {
            // Arrange
            UseConnection(connection =>
            {
                var job = new JobDto
                {
                    InvocationData = "",
                    Arguments = "",
                    CreatedAt = connection.GetServerTimeUtc()
                };
                connection.Job.InsertOne(job);

                var jobQueue = new JobQueueDto
                {
                    JobId = job.Id,
                    Queue = "default",
                    FetchedAt = connection.GetServerTimeUtc().AddDays(-1)
                };
                connection.JobQueue.InsertOne(jobQueue);

                var queue = CreateJobQueue(connection);

                // Act
                var payload = queue.Dequeue(DefaultQueues, CreateTimingOutCancellationToken());

                // Assert
                Assert.NotEmpty(payload.JobId);
            });
        }
Esempio n. 4
0
        public void Dequeue_ShouldLeaveJobInTheQueue_ButSetItsFetchedAtValue()
        {
            // Arrange
            UseConnection(connection =>
            {
                var job = new JobDto
                {
                    InvocationData = "",
                    Arguments = "",
                    CreatedAt = connection.GetServerTimeUtc()
                };
                connection.Job.InsertOne(job);

                var jobQueue = new JobQueueDto
                {
                    JobId = job.Id,
                    Queue = "default"
                };
                connection.JobQueue.InsertOne(jobQueue);

                var queue = CreateJobQueue(connection);

                // Act
                var payload = queue.Dequeue(DefaultQueues, CreateTimingOutCancellationToken());

                // Assert
                Assert.NotNull(payload);

                var fetchedAt = connection.JobQueue.Find(Builders<JobQueueDto>.Filter.Eq(_ => _.JobId, int.Parse(payload.JobId))).FirstOrDefault().FetchedAt;

                Assert.NotNull(fetchedAt);
                Assert.True(fetchedAt > DateTime.UtcNow.AddMinutes(-1));
            });
        }
        private static JobQueueDto CreateJobQueueDto(HangfireDbContext connection, string queue, bool isFetched)
        {
            var state = new StateDto();
            AsyncHelper.RunSync(() => connection.State.InsertOneAsync(state));

            var job = new JobDto
            {
                CreatedAt = connection.GetServerTimeUtc(),
                StateId = state.Id
            };

            AsyncHelper.RunSync(() => connection.Job.InsertOneAsync(job));

            var jobQueue = new JobQueueDto
            {
                Queue = queue,
                JobId = job.Id
            };

            if (isFetched)
            {
                jobQueue.FetchedAt = connection.GetServerTimeUtc().AddDays(-1);
            }

            AsyncHelper.RunSync(() => connection.JobQueue.InsertOneAsync(jobQueue));

            return jobQueue;
        }
		private static int CreateJobQueueRecord(HangfireDbContext connection, string jobId, string queue)
		{
			var jobQueue = new JobQueueDto
			{
				JobId = int.Parse(jobId),
				Queue = queue,
				FetchedAt = connection.GetServerTimeUtc()
			};

			connection.JobQueue.Insert(jobQueue);

			return jobQueue.Id;
		}