public static void RunNotifierService() { for (bool wait = true; ;) { if (wait) { System.Threading.Thread.Sleep(1000 * 30); } else { System.Threading.Thread.Sleep(1000 * 3); } wait = true; { if (!MySpace.DataMining.AELight.dfs.DfsConfigExists( MySpace.DataMining.AELight.dfs.DFSXMLNAME)) { // If not surrogate, wait a lot longer. // Need to keep this loop in case this machine becomes surrogate. System.Threading.Thread.Sleep(1000 * 30 * 9); continue; } } NotifyInfo ninfo = NotifyInfo.Load(); MySpace.DataMining.AELight.dfs dc = null; // Load once if needed. { DateTime starttime = DateTime.Now; if (null == ninfo.LastHistoryTime) { ninfo.LastHistoryTime = starttime.ToString(); ninfo.Save(); } else { bool AnyNotifyAll = false; for (int i = 0; i < ninfo.Notify.Count; i++) { NotifyInfo.NEntry ne = ninfo.Notify[i]; if (-1 == ne.WaitOnJID) { AnyNotifyAll = true; break; } } if (null == ninfo.NewHistory) { if (AnyNotifyAll) { ninfo.NewHistory = new List <NotifyInfo.ConfigNewHistory>(); } } else { if (!AnyNotifyAll) { ninfo.NewHistory = null; ninfo.LastHistoryTime = starttime.ToString(); ninfo.Save(); } } if (AnyNotifyAll) { DateTime lasthistory = DateTime.Parse(ninfo.LastHistoryTime); try { using (System.IO.FileStream stmhist = new System.IO.FileStream("execlog.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite)) { long stmhistLength = stmhist.Length; bool skipfirst = false; if (stmhistLength > 0x400 * 8) { stmhist.Position = stmhistLength - (0x400 * 8); skipfirst = true; } System.IO.StreamReader srhist = new System.IO.StreamReader(stmhist); if (skipfirst) { srhist.ReadLine(); // Probably partial line, ignore it. } string ln; while (null != (ln = srhist.ReadLine())) { if (-1 != ln.IndexOf(" exec ", StringComparison.OrdinalIgnoreCase)) { string user = null, stime = null, cmd = null, snhjid = null; if (ParseExecLogLine(ln, ref user, ref stime, ref cmd, ref snhjid)) { DateTime time = DateTime.Parse(stime); if (time > lasthistory) { string secondarg = ""; { int secondargstart = 0; if (cmd.StartsWith("\"")) { secondargstart = cmd.IndexOf('"', 1); secondargstart++; } secondargstart = cmd.IndexOf(' ', secondargstart); secondargstart++; int secondargend = cmd.IndexOf(' ', secondargstart); if (-1 != secondargend) { secondarg = cmd.Substring(secondargstart, secondargend - secondargstart).Trim('"'); } } if (0 == string.Compare("exec", secondarg, StringComparison.OrdinalIgnoreCase)) { NotifyInfo.ConfigNewHistory cnh = new NotifyInfo.ConfigNewHistory(); cnh.JID = long.Parse(snhjid); //cnh.User = user; //cnh.Time = stime; //cnh.Command = cmd; cnh.History = ln; ninfo.NewHistory.Add(cnh); } } } } } } } catch (System.IO.FileNotFoundException) { } { ninfo.LastHistoryTime = starttime.ToString(); ninfo.Save(); } { // Handle the 'notifyfinish <JobInfo>' cases now. for (int inh = 0; inh < ninfo.NewHistory.Count; inh++) { NotifyInfo.ConfigNewHistory cnh = ninfo.NewHistory[inh]; if (!System.IO.File.Exists(cnh.JID.ToString() + ".jid")) { ninfo.NewHistory.RemoveAt(inh); inh--; ninfo.Save(); for (int i = 0; i < ninfo.Notify.Count; i++) { NotifyInfo.NEntry ne = ninfo.Notify[i]; if (-1 == ne.WaitOnJID) { if (System.Text.RegularExpressions.Regex.IsMatch(cnh.History, ne.WaitOnHistoryRegex, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) { try { string clustername = "N/A"; string SMTP; { if (dc == null) { dc = MySpace.DataMining.AELight.dfs.ReadDfsConfig_unlocked( MySpace.DataMining.AELight.dfs.DFSXMLNAME); } if (dc.ClusterName != null) { clustername = dc.ClusterName; } SMTP = dc.SMTP; } if (null == SMTP) { throw new Exception("SMTP server is null"); } string subject = "Qizmt Notification"; string body; { StringBuilder sbbody = new StringBuilder(); sbbody.AppendLine(ne.GetFinishedUserMessage()); sbbody.AppendFormat("{1} - Cluster Name{0}", Environment.NewLine, clustername); //sbbody.AppendFormat("{1} - Command{0}", Environment.NewLine, cnh.Command); // Insecure. sbbody.AppendFormat("{1} - Job Identifier{0}", Environment.NewLine, cnh.JID); sbbody.AppendFormat("{1} - Notify Identifier{0}", Environment.NewLine, ne.ID); sbbody.AppendFormat("{1} - Completion Time{0}", Environment.NewLine, DateTime.Now); sbbody.AppendFormat("{1} - User Added Notify{0}", Environment.NewLine, ne.GetPlainUserAdded()); body = sbbody.ToString(); } SendQNEmail(SMTP, ne.Email, subject, body); } catch (Exception e) { throw new Exception("Problem sending email to " + ne.Email + " after JID " + ne.WaitOnJID + " finished (notifyfinish <JobInfo>)", e); } System.Threading.Thread.Sleep(1000 * 3); } } } wait = false; break; } } } } } for (int i = 0; i < ninfo.Notify.Count; i++) { NotifyInfo.NEntry ne = ninfo.Notify[i]; if (-1 == ne.WaitOnJID) { } else if (!System.IO.File.Exists(ne.WaitOnJID.ToString() + ".jid")) { ninfo.Notify.RemoveAt(i); i--; ninfo.Save(); try { string clustername = "N/A"; string SMTP; { if (dc == null) { dc = MySpace.DataMining.AELight.dfs.ReadDfsConfig_unlocked( MySpace.DataMining.AELight.dfs.DFSXMLNAME); } if (dc.ClusterName != null) { clustername = dc.ClusterName; } SMTP = dc.SMTP; } if (null == SMTP) { throw new Exception("SMTP server is null"); } string subject = "Qizmt Notification"; string body; { StringBuilder sbbody = new StringBuilder(); sbbody.AppendLine(ne.GetFinishedUserMessage()); sbbody.AppendFormat("{1} - Cluster Name{0}", Environment.NewLine, clustername); sbbody.AppendFormat("{1} - Job Identifier{0}", Environment.NewLine, ne.WaitOnJID); sbbody.AppendFormat("{1} - Notify Identifier{0}", Environment.NewLine, ne.ID); sbbody.AppendFormat("{1} - Completion Time{0}", Environment.NewLine, DateTime.Now); sbbody.AppendFormat("{1} - User Added Notify{0}", Environment.NewLine, ne.GetPlainUserAdded()); body = sbbody.ToString(); } SendQNEmail(SMTP, ne.Email, subject, body); } catch (Exception e) { throw new Exception("Problem sending email to " + ne.Email + " after JID " + ne.WaitOnJID + " finished (notifyfinish <JobID>)", e); } wait = false; break; } } } } }
public static IList <NotifyInfo.NEntry> GetNotifySnapshot() { return(NotifyInfo.Load().Notify); }