private void Execute(object a) { if (servicePaused || !serviceStarted) { return; } // Constrain the number of worker threads (Omitted here.) var threadInfo = a as ThreadExecuteInfo; if (threadInfo == null) { return; } if (!threads[threadInfo.ClassName].IsRunning) { threads[threadInfo.ClassName].IsRunning = true; try { log.Error("Thread start " + threadInfo.Description + " " + threadInfo.JobId); threads[threadInfo.ClassName].Description = threadInfo.Description; threads[threadInfo.ClassName].Execute(); DbCron.TouchLastFinished(threadInfo.JobId); } catch (Exception ex) { log.Error("Job threw an exception", ex); DbCron.TouchLastErrorMessage(threadInfo.JobId, ex.Message); } finally { threads[threadInfo.ClassName].IsRunning = false; } } else { log.Info("Skipped " + threadInfo.JobId + " becuase job with engine " + threadInfo.ClassName + " is already running."); } }
private void InitCron() { servicePaused = true; log.Debug("Clearing existing cron entries and threads"); cronEntries = null; var safeForReload = threads.Values.Count == 0; var waitingForReloadCount = 0; while (!safeForReload) { waitingForReloadCount++; safeForReload = true; foreach (var t in threads.Values) { if (t.IsRunning) { safeForReload = false; } } if (!safeForReload) { if (waitingForReloadCount < MAX_RELOAD_COUNT) { log.Info("Waiting 100 seconds for threads to exit in order to reload crontab."); Thread.Sleep(RELOAD_DELAY); } else { log.Fatal("Too many retries while waiting for threads to exit (" + ((MAX_RELOAD_COUNT * RELOAD_DELAY) / 3600000) + "hrs). Stoping service."); Stop(); return; } } } threads = null; log.Info("Now starting load of new cron entries"); try { cronEntries = DbCron.List(); } catch (Exception ex) { log.Error("InitCron()", ex); loadCrontabErrors++; } if (cronEntries != null && loadCrontabErrors > 0) { log.Info("Crontab error count cleared."); loadCrontabErrors = 0; } if (cronEntries != null) { threads = new Dictionary <string, IJob>(); foreach (var cron in cronEntries) { if (!threads.ContainsKey(cron.ClassName)) { var job = JobFactory.CreateJob(cron.ClassName); if (job != null) { threads[cron.ClassName] = job; } else { log.Error("Class " + cron.ClassName + " not loaded."); } } } log.Info("Found " + threads.Count + " cron plugins"); } servicePaused = false; }