/// <summary> /// Activate the monitoring (usually this has to be run by exploiting the BackgroundWorker pattern). /// No effects are produced if no JobExecuter instance is associated to the JobMonitor. /// </summary> public void Start() { this.mMonitorStarted = true; while (mMonitorStarted == true) { if (mJobExecuter != null) { List <string> zFileNameList = new List <string>(); List <int> zCountList = new List <int>(); List <int> zLocalCountList = new List <int>(); IMonitoredJob zJob = (IMonitoredJob)mJobExecuter.Job; string zLine; DateTime zT0, zT1; TimeSpan zElapsedTime; //int zProcessedFiles = 0; //int zProcessedLines = 0; bool zFinished = false; // Number of files to process (in order to return the percentage of job completion): int zFilesToProcess; if (mNrOfLinesToMonitor == 0) { zFilesToProcess = zJob.To - zJob.From + 1; } else { zFilesToProcess = mNrOfLinesToMonitor; } StringBuilder aStringBuilder; // // Raise up the the event for the start up of the monitoring: // if (JobStarted != null) { JobStarted(this, new JobEventArgs(Environment.NewLine + "Job for " + zJob.ToString() + " submitted...", 0.0)); } try { // Get a first timestamp: zT0 = DateTime.Now; // Create a list of the log files to monitor: for (int i = 0; i < mJobExecuter.Processes; i++) { aStringBuilder = new StringBuilder(zJob.LogFile); aStringBuilder.Remove(zJob.LogFile.Length - 6, 2); aStringBuilder.Insert(zJob.LogFile.Length - 6, i.ToString("00", CultureInfo.InvariantCulture)); zFileNameList.Add(aStringBuilder.ToString()); zCountList.Add(0); zLocalCountList.Add(0); } // Wait until the files exist: TimeSpan zTimeOut = TimeSpan.FromSeconds(Properties.Settings.Default.TimeOutWaitForLogInSeconds); Stopwatch zStopWatch = Stopwatch.StartNew(); bool zDone = false; while (zStopWatch.Elapsed < zTimeOut && !zDone) { // If all the work is completed set zDone to True: zDone = true; for (int i = 0; i < mJobExecuter.Processes; i++) { string zFileName = (zFileNameList[i]); if (!File.Exists(zFileName)) { zDone = false; } } Thread.Sleep(500); } if (zDone == false) { if (JobError != null) { // Add the time for the execution of the whole job: JobError(this, new JobEventArgs(Environment.NewLine + "Job error: A time out occurred while looking for log files.", 1.0)); } } // Start a loop 'till the last file has been processed. Set also a timeout. //while (zProcessedFiles < zFilesToProcess) while (!zFinished) { int zProcessedFiles = 0; // For each file to log: for (int i = 0; i < mJobExecuter.Processes; i++) { string zFileName = zFileNameList[i]; zLocalCountList[i] = 0; // Get first the number of lines into this log file: using (FileStream zFileStream = new FileStream(zFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (StreamReader zStreamReader = new StreamReader(zFileStream)) { while ((zLine = zStreamReader.ReadLine()) != null) { zLocalCountList[i]++; string zTestString = "\t" + this.mStrToMonitor; if (zLine.StartsWith(zTestString)) { zProcessedFiles++; } } } } // Check if the number of lines in this log file is greater than the number of lines read // at the previous step: int zNumberOfLines = zLocalCountList[i]; zLocalCountList[i] = 0; if (zNumberOfLines > zCountList[i]) { using (FileStream zFileStream = new FileStream(zFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (StreamReader zStreamReader = new StreamReader(zFileStream)) { while ((zLine = zStreamReader.ReadLine()) != null) { // If something to log has been found raise up the event: if (zLocalCountList[i] >= zCountList[i]) { // The number of processed files is determined by reading the log line: /*string zTestString = "\t" + this.mStrToMonitor; * if (zLine.StartsWith(zTestString)) * { * // Get processed counter: * string zString = zLine.Substring(zTestString.Length + 1, 4); * int zInt = Convert.ToInt32(zString); * zProcessedFiles = Convert.ToInt32(zLine.Substring(zTestString.Length + 1, 4)) - mJobExecuter.Job.From + 1; * //zProcessedLines++; * // Ending condition: * //if (zLine.StartsWith(zTestString + "_" + mJobExecuter.Job.To.ToString("D4"))) * //if ( zProcessedLines >= mNrOfLinesToMonitor ) * // zFinished = true; * }*/ // Raise up the event: if (JobStep != null) { JobStep(this, new JobEventArgs(Environment.NewLine + zLine, zProcessedFiles / ((double)(zFilesToProcess)))); //JobStep(this, new JobEventArgs(Environment.NewLine + zLine, zProcessedLines / ((double)(mNrOfLinesToMonitor)))); } } // Increment counter: zLocalCountList[i]++; } } } } zCountList[i] = zNumberOfLines; } // Check ending condition: if (zProcessedFiles >= zFilesToProcess) { zFinished = true; } // Sleep the thread: Thread.Sleep(500); } // // Raise up the the event for completion of the monitoring: // // Get the last timestamp: zT1 = DateTime.Now; zElapsedTime = zT1 - zT0; // Delete logs: for (int i = 0; i < mJobExecuter.Processes; i++) { string zFileName = (zFileNameList[i]); if (File.Exists(zFileName)) { File.Delete(zFileName); } } if (JobCompleted != null) { // Add the time for the execution of the whole job: JobCompleted(this, new JobEventArgs(Environment.NewLine + "Job for " + zJob.ToString() + " completed successfully in " + zElapsedTime.ToString(@"hh\:mm\:ss") + ".", 1.0)); } mJobExecuter = null; } catch (Exception e) { if (JobError != null) { // Add the time for the execution of the whole job: JobError(this, new JobEventArgs(Environment.NewLine + "Job error: " + e.Message + ".", 1.0)); } mJobExecuter = null; } } // Sleep: Thread.Sleep(500); } }
/// <summary> /// Activate the monitoring (usually this has to be run by exploiting the BackgroundWorker pattern). /// No effects are produced if no JobExecuter instance is associated to the RemoteJobMonitor. /// </summary> public void Start() { this.mMonitorStarted = true; while (mMonitorStarted == true) { if (mJobExecuter != null) { /* List<string> zFileNameList = new List<string>(); * List<int> zCountList = new List<int>(); * List<int> zLocalCountList = new List<int>();*/ int zCount = 0; int zLocalCount = 0; IMonitoredJob zJob = (IMonitoredJob)mJobExecuter.Job; string zLine; DateTime zT0, zT1; TimeSpan zElapsedTime; //int zProcessedFiles = 0; //int zProcessedLines = 0; bool zFinished = false; // Number of files to process (in order to return the percentage of job completion): int zFilesToProcess; if (mNrOfLinesToMonitor == 0) { zFilesToProcess = zJob.To - zJob.From + 1; } else { zFilesToProcess = mNrOfLinesToMonitor; } // // Raise up the the event for the start up of the monitoring: // if (JobStarted != null) { JobStarted(this, new JobEventArgs(Environment.NewLine + "Job for " + zJob.ToString() + " submitted...", 0.0)); } try { // Get a first timestamp: zT0 = DateTime.Now; // Wait until the files exist: TimeSpan zTimeOut = TimeSpan.FromSeconds(Properties.Settings.Default.TimeOutWaitForLogInSeconds); Stopwatch zStopWatch = Stopwatch.StartNew(); bool zDone = false; while ((zStopWatch.Elapsed < zTimeOut) && (zDone == false)) { string zresult = SYRMEP_HPC.Execute("ls " + zJob.LogFile); if (zresult.StartsWith(zJob.LogFile)) { zDone = true; } // Sleep the thread: if (zJob is IRemoteMonitoredJob) { Thread.Sleep(Properties.Settings.Default.RemoteLogRefreshRateMilliseconds); } else { Thread.Sleep(Properties.Settings.Default.LocalLogRefreshRateMilliseconds); } } if (zDone == false) { if (JobError != null) { // Add the time for the execution of the whole job: JobError(this, new JobEventArgs(Environment.NewLine + "Job error: A time out occurred while looking for log files.", 1.0)); } } // Start a loop 'till the last file has been processed. Set also a timeout. while (!zFinished) { int zProcessedFiles = 0; // For each file to log: zLocalCount = 0; string zResult = SYRMEP_HPC.Execute("cat " + zJob.LogFile); string[] zLines = zResult.Split('\n'); zLocalCount = zLines.Length - 1; zProcessedFiles = countFreq("\t" + this.mStrToMonitor, zResult); // Check if the number of lines in the log file is greater than the number of lines read // at the previous step. If true there is something to log and we raise up the event: if (zLocalCount >= zCount) { int diff = zLocalCount - zCount; for (int k = 0; k < diff; k++) { zLine = zLines[zCount + k]; // Raise up the event: if (JobStep != null) { JobStep(this, new JobEventArgs(Environment.NewLine + zLine, zProcessedFiles / ((double)(zFilesToProcess)))); } } } zCount = zLocalCount; // Check ending condition: if (zProcessedFiles >= zFilesToProcess) { zFinished = true; } // Sleep the thread: if (zJob is IRemoteMonitoredJob) { Thread.Sleep(Properties.Settings.Default.RemoteLogRefreshRateMilliseconds); } else { Thread.Sleep(Properties.Settings.Default.LocalLogRefreshRateMilliseconds); } } // // Raise up the the event for completion of the monitoring: // // Get the last timestamp: zT1 = DateTime.Now; zElapsedTime = zT1 - zT0; // Delete log: if (File.Exists(zJob.LogFile)) { string zResult = SYRMEP_HPC.Execute("rm " + zJob.LogFile); } // Raise the event: if (JobCompleted != null) { // Add the time for the execution of the whole job: JobCompleted(this, new JobEventArgs(Environment.NewLine + "Job for " + zJob.ToString() + " completed successfully in " + zElapsedTime.ToString(@"hh\:mm\:ss") + ".", 1.0)); } mJobExecuter = null; } catch (Exception e) { if (JobError != null) { // Add the time for the execution of the whole job: JobError(this, new JobEventArgs(Environment.NewLine + "Job error: " + e.Message + ".", 1.0)); } mJobExecuter = null; } } // Sleep: Thread.Sleep(Properties.Settings.Default.LocalLogRefreshRateMilliseconds); } }