public override string CreateExpiredJob(Job job, IDictionary <string, string> parameters, DateTime createdAt, TimeSpan expireIn) { if (job == null) { throw new ArgumentNullException(nameof(job)); } if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } lock (_lock) { var invocationData = InvocationData.Serialize(job); var jobDto = new LiteJob { InvocationData = SerializationHelper.Serialize(invocationData, SerializationOption.User), Arguments = invocationData.Arguments, Parameters = parameters.ToDictionary(kv => kv.Key, kv => kv.Value), CreatedAt = createdAt, ExpireAt = createdAt.Add(expireIn) }; Database.Job.Insert(jobDto); var jobId = jobDto.Id; return(jobId.ToString()); } }
private static JobQueue CreateJobQueueDto(HangfireDbContext connection, string queue, bool isFetched) { var job = new LiteJob { CreatedAt = DateTime.UtcNow, StateHistory = new List <LiteState>() }; connection.Job.Insert(job); var jobQueue = new JobQueue { Queue = queue, JobId = job.Id }; if (isFetched) { jobQueue.FetchedAt = DateTime.UtcNow.AddDays(-1); } connection.JobQueue.Insert(jobQueue); return(jobQueue); }
public void Dequeue_ShouldFetchJobs_FromMultipleQueuesBasedOnQueuePriority() { UseConnection(connection => { var criticalJob = new LiteJob { InvocationData = "", Arguments = "", CreatedAt = DateTime.UtcNow }; connection.Job.Insert(criticalJob); var defaultJob = new LiteJob { InvocationData = "", Arguments = "", CreatedAt = DateTime.UtcNow }; connection.Job.Insert(defaultJob); connection.JobQueue.Insert(new JobQueue { JobId = defaultJob.Id, Queue = "default" }); connection.JobQueue.Insert(new JobQueue { JobId = criticalJob.Id, Queue = "critical" }); var queue = CreateJobQueue(connection); var critical = (LiteDbFetchedJob)queue.Dequeue( new[] { "critical", "default" }, CreateTimingOutCancellationToken()); Assert.NotNull(critical.JobId); Assert.Equal("critical", critical.Queue); var @default = (LiteDbFetchedJob)queue.Dequeue( new[] { "critical", "default" }, CreateTimingOutCancellationToken()); Assert.NotNull(@default.JobId); Assert.Equal("default", @default.Queue); }); }
public void SetJobState_AppendsAStateAndSetItToTheJob() { UseConnection(database => { LiteJob job = new LiteJob { InvocationData = "", Arguments = "", CreatedAt = DateTime.Now }; database.Job.Insert(job); LiteJob anotherJob = new LiteJob { InvocationData = "", Arguments = "", CreatedAt = DateTime.Now }; database.Job.Insert(anotherJob); var serializedData = new Dictionary <string, string> { { "Name", "Value" } }; var state = new Mock <IState>(); state.Setup(x => x.Name).Returns("State"); state.Setup(x => x.Reason).Returns("Reason"); state.Setup(x => x.SerializeData()).Returns(serializedData); Commit(database, x => x.SetJobState(job.IdString, state.Object)); var testJob = GetTestJob(database, job.Id); Assert.Equal("State", testJob.StateName); Assert.Equal(1, testJob.StateHistory.Count); var anotherTestJob = GetTestJob(database, anotherJob.Id); Assert.Null(anotherTestJob.StateName); Assert.Equal(0, anotherTestJob.StateHistory.Count); var jobWithStates = database.Job.FindAll().ToList().FirstOrDefault(); var jobState = jobWithStates.StateHistory.Single(); Assert.Equal("State", jobState.Name); Assert.Equal("Reason", jobState.Reason); Assert.NotNull(jobState.CreatedAt); Assert.Equal(serializedData, jobState.Data); }); }
public void Dequeue_ShouldSetFetchedAt_OnlyForTheFetchedJob() { // Arrange UseConnection(connection => { var job1 = new LiteJob { InvocationData = "", Arguments = "", CreatedAt = DateTime.UtcNow }; connection.Job.Insert(job1); var job2 = new LiteJob { InvocationData = "", Arguments = "", CreatedAt = DateTime.UtcNow }; connection.Job.Insert(job2); connection.JobQueue.Insert(new JobQueue { JobId = job1.Id, Queue = "default" }); connection.JobQueue.Insert(new JobQueue { JobId = job2.Id, Queue = "default" }); var queue = CreateJobQueue(connection); // Act var payload = queue.Dequeue(DefaultQueues, CreateTimingOutCancellationToken()); var payloadJobId = int.Parse(payload.JobId); // Assert var otherJobFetchedAt = connection.JobQueue.Find(_ => _.JobId != payloadJobId).FirstOrDefault().FetchedAt; Assert.Null(otherJobFetchedAt); }); }
public void Dequeue_ShouldFetchJobs_OnlyFromSpecifiedQueues() { UseConnection(connection => { var job1 = new LiteJob { InvocationData = "", Arguments = "", CreatedAt = DateTime.UtcNow }; connection.Job.Insert(job1); connection.JobQueue.Insert(new JobQueue { JobId = job1.Id, Queue = "critical" }); var queue = CreateJobQueue(connection); Assert.Throws <OperationCanceledException>(() => queue.Dequeue(DefaultQueues, CreateTimingOutCancellationToken())); }); }
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); }