Limiting batch processing to one at a time

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

Hi,

Since I managed to get passed this error, I answer my own question. The reason for that error is because of the BatchId property of the job.
That property does contain the batch Id but surrounded by quotes in the string. Removing the quotes in the string makes this code work and the new batches are added.

Franck