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?