// Define the event handlers. /// <summary> /// The directory created callback /// </summary> /// <param name="source"></param> /// <param name="e"></param> public void OnCreated(object source, FileSystemEventArgs e) { string jobDirectory = e.FullPath; string job = jobDirectory.Replace(StaticClass.IniData.InputDir, "").Remove(0, 1); StaticClass.Log(string.Format("\nInput Directory Watcher checking new Job {0} for Input Job list at {1:HH:mm:ss.fff}", job, DateTime.Now)); if (StaticClass.ShutDownPauseCheck("Directory Watcher OnCreated") == false) { Thread.Sleep(StaticClass.WAIT_FOR_FILES_TO_COMPLETE); // Check directory contents complete if (StaticClass.CheckDirectoryReady(jobDirectory) == true) { StaticClass.AddJobToList(job); if ((StaticClass.IniData.DebugMode & (byte)DebugModeState.JOB_LIST) != 0) { StaticClass.DisplayJobList(); } } else { StaticClass.Logger.LogError("DirectoryWatcherThread Job {0} directory check failed at {1:HH:mm:ss.fff}", job, DateTime.Now); } } }
/// <summary> /// Method to scan for unfinished jobs in the Input Buffer /// </summary> public void CheckForUnfinishedInputJobs() { // Start the Directory Watcher class to scan for new Input Buffer jobs DirectoryWatcherThread dirWatch = new DirectoryWatcherThread(); if (dirWatch == null) { StaticClass.Logger.LogError("InputJobsScanThread dirWatch failed to instantiate"); } dirWatch.ThreadProc(); // Register with the Processing Buffer Jobs check completion event and start its thread ProcessingJobsScanThread unfinishedProcessingJobs = new ProcessingJobsScanThread(); if (unfinishedProcessingJobs == null) { StaticClass.Logger.LogError("InputJobsScanThread currentProcessingJobs failed to instantiate"); } unfinishedProcessingJobs.ThreadProc(); // Wait while scanning for unfinished Processing jobs do { if (StaticClass.ShutDownPauseCheck("CheckForUnfinishedInputJobs") == true) { return; } Thread.Yield(); }while (StaticClass.UnfinishedProcessingJobsScanComplete == false); StaticClass.Log("\nChecking for unfinished Input Jobs..."); DirectoryInfo InputDirectoryInfo = new DirectoryInfo(StaticClass.IniData.InputDir); if (InputDirectoryInfo == null) { StaticClass.Logger.LogError("InputJobsScanThread InputDirectoryInfo failed to instantiate"); } // Get the current list of directories from the Input Buffer List <DirectoryInfo> inputDirectoryInfoList = InputDirectoryInfo.EnumerateDirectories().ToList(); if (inputDirectoryInfoList == null) { StaticClass.Logger.LogError("InputJobsScanThread inputDirectoryInfoList failed to instantiate"); } if (inputDirectoryInfoList.Count > 0) { StaticClass.Log("\nUnfinished Input Jobs waiting..."); } else { StaticClass.Log("\nNo unfinished Input Jobs found..."); } // Start the jobs in the directory list found for the Input Buffer foreach (DirectoryInfo dirInfo in inputDirectoryInfoList.Reverse <DirectoryInfo>()) { if (StaticClass.NumberOfJobsExecuting < StaticClass.IniData.ExecutionLimit) { string jobDirectory = dirInfo.ToString(); string job = jobDirectory.ToString().Replace(StaticClass.IniData.InputDir, "").Remove(0, 1); // Check if the directory has a full set of Job files if (StaticClass.CheckIfJobFilesComplete(jobDirectory) == true) { StaticClass.Log(string.Format("Starting Input Job {0} at {1:HH:mm:ss.fff}", jobDirectory, DateTime.Now)); // Remove job run from Input Job directory list lock (RemoveLock) { if (inputDirectoryInfoList.Remove(dirInfo) == false) { StaticClass.Logger.LogError("InputJobsScanThread failed to remove Job {0} from Input Job list", job); } } StaticClass.Log(string.Format("Input Directory Watcher removed Job {0} from Input Directory Jobs list at {1:HH:mm:ss.fff}", job, DateTime.Now)); // Reset Input file scan flag StaticClass.InputFileScanComplete[job] = false; // Start an Input Buffer Job StartInputJob(jobDirectory); // Throttle the Job startups Thread.Sleep(StaticClass.ScanWaitTime); } } } if (inputDirectoryInfoList.Count > 0) { StaticClass.Log("\nMore unfinished Input Jobs then Execution Slots available..."); // Sort the Input Buffer directory list by older dates first inputDirectoryInfoList.Sort((x, y) => - x.LastAccessTime.CompareTo(y.LastAccessTime)); // Add the jobs in the directory list to the Input Buffer Jobs to run list foreach (DirectoryInfo dirInfo in inputDirectoryInfoList) { string directory = dirInfo.ToString(); string job = directory.Replace(StaticClass.IniData.InputDir, "").Remove(0, 1); StaticClass.AddJobToList(job); } // Clear the Directory Info List after done with it inputDirectoryInfoList.Clear(); // Display new job list StaticClass.DisplayJobList(); } StaticClass.Log("\nStarted Watching for new Input Jobs..."); // Run new Input Job watch loop here forever do { // Check if the shutdown flag is set, exit method if (StaticClass.ShutDownPauseCheck("CheckForNewInputJobs") == true) { return; } // Run any unfinished input jobs RunInputJobsFound(); // Throttle calling new Jobs Found handler Thread.Sleep(StaticClass.IniData.ScanWaitTime); }while (true); }