public void Start(JobQueue queue, bool alwaysRunning) { var processor = new JobProcessor(config, currentDb, manager, indexUpdater, indexReader); var running = true; while (running) { var curSize = queue.Size(); while (curSize > 0) { var loop = 0; while (loop < curSize) { try { processor.Add(queue.Dequeue()); } catch (Exception ex) { log.ErrorException("Dequeue Exception", ex); } loop++; } curSize = queue.Size(); } processor.Flush(true); running = Wait(queue, alwaysRunning, processor.ProcessCount); } }
bool Wait(JobQueue queue, bool alwaysRunning, long processCount) { var ret = false; var exitMethod = false; var dtCountdown = DateTime.UtcNow + TimeSpan.FromSeconds(30); var sw = new SpinWait(); if (traceEnabled && Waiting != null) { Waiting(this, new WaitEventArgs(processCount, queue.ID)); } while (!exitMethod) { var localStopping = stopping; // Exit if Timeout and not always running // Or Signaled from Manager to stop job if ((!alwaysRunning && dtCountdown > DateTime.UtcNow) || (localStopping == 1)) { exitMethod = true; } else { // Something in queue to process if (queue.Size() > 0) { ret = true; exitMethod = true; } else { sw.SpinOnce(); } } } return ret; }