I’m running the following test code and what I would expect to see is that if iterations > 5 none of my enqueued jobs are ever run since I throw and exception in the middle of the loop. However what I’m seeing is that jobs 0-4 are in fact logging out to the console. I’ve tried this with InMemory, SQLServier, and Redis providers. Am I doing something wrong?
static void Main(string[] args)
{
GlobalConfiguration.Configuration.UseMemoryStorage();
GlobalConfiguration.Configuration.UseBatches();
using (var server = new BackgroundJobServer())
{
Console.WriteLine("Hangfire Server started.");
Console.WriteLine("How many jobs?");
var iterations = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enqueing...");
EnqueueJobs(iterations);
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
static void EnqueueJobs(int iterations)
{
var sw = new Stopwatch();
sw.Start();
var rnd = new Random();
try
{
BatchJob.StartNew(x =>
{
for (var i = 0; i < iterations; i++)
{
if (i == 5) throw new Exception("Test");
BackgroundJob.Enqueue(() => Console.Write("\rFire-and-forget {0} - {1} ", i, sw.Elapsed));
}
});
}
catch (Exception e){ }
sw.Stop();
Console.WriteLine("Enqueued in "+ sw.Elapsed);
}