public static UserObject GetNextAlert(string processorId, string alertQueueFileName) { Mutex mutex = new Mutex(false, _mobiusAlertQueue); mutex.WaitOne(); // get exclusive access int alertId; try { StreamReader sr = new StreamReader(alertQueueFileName); string content = sr.ReadToEnd(); sr.Close(); if (Lex.IsNullOrEmpty(content)) // all done if nothing left in queue { //AlertCount = 0; return(null); } int i1 = content.IndexOf(","); if (i1 >= 0) { alertId = Int32.Parse(content.Substring(0, i1)); content = content.Substring(i1 + 1); } else { alertId = Int32.Parse(content); content = ""; } StreamWriter sw = new StreamWriter(alertQueueFileName); sw.Write(content); sw.Close(); //_remainingingAlertCount--; } catch (Exception ex) { AlertUtil.LogAlertMessage("Error accessing alert queue: " + ex.Message + processorId); return(null); } finally { mutex.ReleaseMutex(); } UserObject uo = UserObjectDao.Read(alertId); // read the alert if (uo == null) { AlertUtil.LogAlertMessage("Error reading alert " + alertId + processorId); } return(uo); }
/// <summary> /// Start new process to check queued alerts /// </summary> public void StartQueueProcessor( string args) { _numberOfCurrentProcessors++; string msg = "Starting queue processor " + NumberOfCurrentProcessors; AlertUtil.LogAlertMessage(msg); Progress.Show(msg); if (Lex.Contains(args, "singleProcessor")) // use single queue processor within this process (for debug) { CheckQueuedAlerts(args); } else // start separate process to check the queued alerts { try { CommandLine.StartForegroundSession("Check Queued Alerts " + args, null); } catch (Exception ex) { AlertUtil.LogAlertMessage("Couldn't start new queue processor:\r\n" + ex.Message); } } }
/// <summary> /// Check queued alerts. Several of these process may be running at the same time in parallel. /// </summary> /// <param name="args"></param> /// <returns></returns> public static string CheckQueuedAlerts( string args) { string msg; int i1; bool forceCheck = false; bool checkOnly = false; int firstToCheck = 0; bool hasMoreAlerts = true; int alertsWithNewData = 0; int alertsChecked = 0; string processorIdTxt = ", Pid: " + (int)DateTime.Now.TimeOfDay.TotalSeconds; // mark each message with id of processor (sec of day started) AlertUtil.LogAlertMessage("Queue processor started: " + args + processorIdTxt); if (args != null && args.Trim().Length > 0) { if (Lex.Contains(args, "forceCheck")) { forceCheck = true; } if (Lex.Contains(args, "checkOnly")) { checkOnly = true; } } string alertQueueFileName = GetAlertQueueFileName(); while (hasMoreAlerts) { UserObject uo = GetNextAlert(processorIdTxt, alertQueueFileName); if (uo == null) { break; } int queryId = 0; // get queryId from alert name i1 = uo.Name.IndexOf("_"); if (i1 > 0) { try { queryId = Int32.Parse(uo.Name.Substring(i1 + 1)); } catch (Exception) { } } msg = "Processing Alert: " + uo.Id + ", QueryId: " + queryId + processorIdTxt; // Thread.Sleep(10000); // debug AlertUtil.LogAlertMessage(msg); Progress.Show(msg); DateTime begin = DateTime.Now; try // check it { msg = AlertUtil.Check(uo, forceCheck, checkOnly, false); } catch (Exception ex) { msg = "Error executing Alert: " + uo.Id + ", QueryId: " + queryId + ", Processor: " + processorIdTxt; if (Lex.Contains(ex.Message, "ORA-01012") || Lex.Contains(ex.Message, "ORA-00028")) { msg += ", Error: " + ex.Message; // keep simple if forced logoff } else // include stack trace otherwise { msg += "Error: " + DebugLog.FormatExceptionMessage(ex, msg); } AlertUtil.LogAlertMessage(msg); AlertUtil.LogAlertErrorMessage(msg); alertsChecked++; continue; } if (Lex.Contains(msg, "Delayed Alert")) { AlertUtil.LogAlertMessage(msg); Alert alert = Alert.GetAlertFromUserObject(uo, true); AddAlertBack(alert); SleepUntilNextAlertStartTime(uo); continue; } string msg0 = // message prefix "Completed Alert: " + uo.Id + ", QueryId: " + queryId + processorIdTxt; //DateTime end = DateTime.Now; // (redundant) //TimeSpan elapsed = end.Subtract(begin); //string eTxt = elapsed.ToString(); //if ((i1 = eTxt.IndexOf(".")) > 0) eTxt = eTxt.Substring(0, i1); //msg0 += ", Time to process = " + eTxt; msg = msg0 + " " + msg; AlertUtil.LogAlertMessage(msg); if (Lex.Contains(msg, "Error executing alert")) // keep separate log of errors { AlertUtil.LogAlertErrorMessage(msg); } alertsChecked++; if (msg.Contains("Changed compounds")) { alertsWithNewData++; } } msg = "Queue processor complete, Alerts checked: " + alertsChecked + ", Alert E-mails: " + alertsWithNewData + processorIdTxt; AlertUtil.LogAlertMessage(""); AlertUtil.LogAlertMessage(msg); Progress.Hide(); return(msg); }