Odd behavior when I try to execute multiple batches

I have the following code:

BatchJob.Attach(jobId, batch =>
                    {
                        foreach (var rule in ruleStatus.RulesExecuted.Select(x => x.RuleExecuted).OrderBy(x => x.Order))
                        {
                            batch.StartNew(x =>
                            {
                                foreach (var action in rule.Actions.Where(action => action.IsEnable)
                                             .OrderBy(y => y.ExecutionOrder))
                                {
                                    var ruleOperationDtos = action.ActionOperationList()?.Operation;
                                    if (ruleOperationDtos == null)
                                    {
                                        continue;
                                    }

                                    foreach (var operation in ruleOperationDtos)
                                    {
                                        operation.RuleName = rule.Name;
                                        operation.RuleDescription = rule.Description;
                                        operation.RuleId = rule.Oid;
                                        operation.ActionType = action.ActionName;
                                        operation.BatchJobId = x.BatchId;

                                        x.Enqueue(
                                            () => ExecuteRuleActionOperation(
                                                operation,
                                                ruleEngineSignalDTO,
                                                null));
                                    }
                                }

                            }, $"{rule.Name}");
                        }
                    });

The code works perfectly if it is executed the first two times, or even the third time as well. Then, if I try to run this code, the jobs are executed in parallel which creates undesired behavior.

Thoughts?

That is how Hangfire is designed to work. You queue jobs and it executes jobs on the worker threads (in parallel). If you want to only run one at a time you could try only having one worker. I’m not sure which parallel operation you have issue with.

You could specify that each batch continue with the next batch. This will require batch one to complete before batch 2 starts, batch 2 to complete before batch 3 starts, etc. Having them in a batch doesn’t prevent parallelism.