Hi,
While looking for a solution to limit batch processing to one at a time (without limiting the job processing within that batch), I got an “Operation is not valid due to the current state of the object” exception.
Here is the code I’m running.
if (previousBatch == null)
{
IMonitoringApi monitor = JobStorage.Current.GetMonitoringApi();
long processingJobCount2 = monitor.GetStatistics().Processing;
long processingJobCount3 = monitor.ProcessingCount();
JobList<ProcessingJobDto> processingJobs = monitor.ProcessingJobs(0, 100);
if (processingJobs != null && processingJobs.Count() > 0)
{
JobDetailsDto jobDetail = monitor.JobDetails(processingJobs.First().Key);
if (jobDetail.Properties.ContainsKey("BatchId"))
{
previousBatch = jobDetail.Properties["BatchId"];
}
}
}
if (previousBatch == null)
{
batchId = BatchJob.StartNew(x =>
{
ProcessJobListInBatch(x, engine, jobs);
}, description
);
}
else
{
batchId = BatchJob.ContinueWith(previousBatch, x =>
{
ProcessJobListInBatch(x, engine, jobs);
}, description
);
}
The problem appears in the ContinueWith function.
Here is the relevant stacktrace.
at Hangfire.Batches.BatchContinuationsSupportAttribute.AddContinuation(ElectBatchStateContext context, BatchAwaitingState awaitingState)
at Hangfire.Batches.BatchContinuationsSupportAttribute.OnStateElection(ElectBatchStateContext context)
at Hangfire.Batches.States.BatchStateMachine.ApplyState(ApplyBatchStateContext initialContext)
at Hangfire.Batches.Client.BatchFactory.Create(BatchCreateContext context)
at Hangfire.BatchJobClient.Create(Action`1 createAction, IBatchState state, String description)
I think that’s a bug in Hangfire.
Meanwhile, is there any way to restrict the processing of batches to only one (even better if it can be based on a condition linked to the job) ?
Thanks,
Franck