public void ProcessingJobs_ReturnsProcessingJobsOnly_WhenMultipleJobsExistsInProcessingSucceededAndEnqueuedState() { UseMonitoringApi((database, monitoringApi) => { var processingJob = CreateJobInState(database, 1, ProcessingState.StateName); var succeededJob = CreateJobInState(database, 2, SucceededState.StateName, liteJob => { var processingState = new LiteState() { Name = ProcessingState.StateName, Reason = null, CreatedAt = DateTime.UtcNow, Data = new Dictionary <string, string> { ["ServerId"] = Guid.NewGuid().ToString(), ["StartedAt"] = JobHelper.SerializeDateTime(DateTime.UtcNow.Subtract(TimeSpan.FromMilliseconds(500))) } }; var succeededState = liteJob.StateHistory[0]; liteJob.StateHistory = new List <LiteState> { processingState, succeededState }; return(liteJob); }); var enqueuedJob = CreateJobInState(database, 3, EnqueuedState.StateName); var jobIds = new List <int> { processingJob.Id, succeededJob.Id, enqueuedJob.Id }; _persistentJobQueueMonitoringApi.Setup(x => x .GetFetchedJobIds(DefaultQueue, From, PerPage)) .Returns(jobIds); var resultList = monitoringApi.ProcessingJobs(From, PerPage); Assert.Single(resultList); }); }
private LiteJob CreateJobInState(HangfireDbContext database, int jobId, string stateName, Func <LiteJob, LiteJob> visitor = null) { var job = Job.FromExpression(() => SampleMethod("wrong")); Dictionary <string, string> stateData; if (stateName == EnqueuedState.StateName) { stateData = new Dictionary <string, string> { ["EnqueuedAt"] = $"{DateTime.UtcNow:o}" }; } else if (stateName == ProcessingState.StateName) { stateData = new Dictionary <string, string> { ["ServerId"] = Guid.NewGuid().ToString(), ["StartedAt"] = JobHelper.SerializeDateTime(DateTime.UtcNow.Subtract(TimeSpan.FromMilliseconds(500))) }; } else { stateData = new Dictionary <string, string>(); } var jobState = new LiteState() { JobId = jobId, Name = stateName, Reason = null, CreatedAt = DateTime.UtcNow, Data = stateData }; var liteJob = new LiteJob { Id = jobId, InvocationData = SerializationHelper.Serialize(InvocationData.SerializeJob(job)), Arguments = "[\"\\\"Arguments\\\"\"]", StateName = stateName, CreatedAt = DateTime.UtcNow, StateHistory = new List <LiteState> { jobState } }; if (visitor != null) { liteJob = visitor(liteJob); } database.Job.Insert(liteJob); var jobQueueDto = new JobQueue { FetchedAt = null, JobId = jobId, Queue = DefaultQueue }; if (stateName == FetchedStateName) { jobQueueDto.FetchedAt = DateTime.UtcNow; } database.JobQueue.Insert(jobQueueDto); return(liteJob); }