public TaskManager() { try { if (!File.Exists(_SchedulerSettingsFile)) { throw new FileNotFoundException(String.Format("Settings file {0} not found.", _SchedulerSettingsFile)); } LocalLog.AddLine(String.Format("Loading settings file {0}", _SchedulerSettingsFile)); _Settings = XmlUtilities.LoadXmlFile(_SchedulerSettingsFile); LocalLog.AddLine("Settings file successfully loaded"); LocalLog.AddLine("Reading system tokens"); _tokens = new TokenManager(_Settings); LocalLog.AddLine("Tokens ready"); InitializeSystem(); LocalLog.AddLine("ScanManager is ready."); } catch (Exception ex) { LocalLog.AddLine("ScanManager Instantiation Error: " + ex.Message); _AppLog.WriteEntry(ex.StackTrace, System.Diagnostics.EventLogEntryType.Error); throw; } }
protected void ValidateRunningJobs() { Dictionary <String, Object> sqlParameters = new Dictionary <String, Object>(); sqlParameters.Add(":machinename", Environment.MachineName); // Pull the jobs running on this machine. DataTable dt = SqlUtilities.GetData(_ConnectionString, _SqlJobsInProgress, sqlParameters); if ((dt != null) && (dt.Rows != null) && (dt.Rows.Count > 0)) { sqlParameters.Add(":inprogress", false); sqlParameters.Add(":scanrequested", false); sqlParameters.Add(":jobstatus", "Error"); sqlParameters.Add(":statusdescription", "See activity log for details."); sqlParameters.Add(":pkey", -1); for (int i = 0; i < dt.Rows.Count; i++) { try { if (!MiscUtilities.isAlive(dt.Rows[i]["definition_name"].ToString())) { sqlParameters[":pkey"] = MiscUtilities.ObjectToInteger(dt.Rows[i]["pkey"], "No primary key defined."); SqlUtilities.ExcecuteNonQuery(_ConnectionString, _SqlUpdateInProgressStatus, sqlParameters); } } catch (Exception ex) { LocalLog.AddLine("Error while updating in progress job status. Error message was: " + ex.Message); _AppLog.WriteEntry(ex.StackTrace, System.Diagnostics.EventLogEntryType.Error); } } } }
public ScanRequestManager(String SettingsFile, String JarPath) { try { if (!File.Exists(SettingsFile)) { _errorMessage = String.Format("Settings file {0} not found.", SettingsFile); throw new FileNotFoundException(_errorMessage); } else { LocalLog.AddLine("Using " + SettingsFile); } if (!File.Exists(JarPath)) { _errorMessage = String.Format("JAR file {0} not found.", JarPath); throw new FileNotFoundException(_errorMessage); } else { LocalLog.AddLine("Using " + JarPath); } _SettingsFile = SettingsFile; _JarPath = JarPath; _Settings = XmlUtilities.LoadXmlFile(_SettingsFile); ReadQueries(); LocalLog.AddLine("ScanManager is ready."); } catch (Exception ex) { LocalLog.AddLine("ScanManager Instantiation Error: " + ex.Message); _AppLog.WriteEntry(ex.StackTrace, System.Diagnostics.EventLogEntryType.Error); throw; } }
protected void RunJava(int JobKey, string JobFileName, string RawJobFileName) { String args = String.Format("-s {0} -d {1} JobKey={2} DefinitionFile={3}", MiscUtilities.WrapSpaces(_SettingsFile), MiscUtilities.WrapSpaces(JobFileName), JobKey, RawJobFileName); using (Process clientProcess = new Process()) { clientProcess.StartInfo.UseShellExecute = false; clientProcess.StartInfo.RedirectStandardOutput = true; clientProcess.StartInfo.RedirectStandardError = true; clientProcess.StartInfo.CreateNoWindow = true; clientProcess.StartInfo.WorkingDirectory = MiscUtilities.AppParentPath(); clientProcess.StartInfo.FileName = MiscUtilities.WrapSpaces(_JavaHome + "java.exe"); clientProcess.StartInfo.Arguments = @"-jar " + MiscUtilities.WrapSpaces(_JarPath) + " " + args; lock (_lock) { LocalLog.AddLine(String.Format("Command line: {0} {1}", MiscUtilities.WrapSpaces(_JavaHome + "java.exe"), @"-jar " + MiscUtilities.WrapSpaces(_JarPath) + " " + args)); } clientProcess.Start(); string output = clientProcess.StandardOutput.ReadToEnd(); string errorOutput = clientProcess.StandardError.ReadToEnd(); clientProcess.WaitForExit(); int exitcode = clientProcess.ExitCode; LocalLog.AddLine("Console Output: " + System.Environment.NewLine + output); if (exitcode > 0) { LocalLog.AddLine("ERROR OUTPUT: " + System.Environment.NewLine + errorOutput); throw new Exception(String.Format("Run JAVA error ExitCode {0} running {1} {2}", exitcode, clientProcess.StartInfo.FileName, clientProcess.StartInfo.Arguments)); } } }
//protected override void OnStart(string[] args) //{ // LocalLog.AddLine("Starting ScanManager service."); // logEnvironment(); // String path = MiscUtilities.AppParentPath() + Path.DirectorySeparatorChar; // _srm = new ScanManager.ScanRequestManager(path + "_Settings.xml", path + "pie.jar"); // try // { // LocalLog.AddLine("Starting Timer: Interval is 30 seconds"); // _timer = new Timer(30000); // every 30 seconds // _timer.Elapsed += new System.Timers.ElapsedEventHandler(pieJobCheck); // _timer.Start(); // LocalLog.AddLine("Service is running."); // } // catch (Exception ex) // { // LocalLog.AddLine("Error while starting timer. " + ex.Message); // } //} protected override void OnStop() { if (_timer != null) { _timer.Stop(); _timer = null; } LocalLog.AddLine("Service stopped."); }
protected Boolean BackupCastDatabase(int JobKey, String JobFileName, String RawJobFileName) { Dictionary <String, Object> sqlParameters = new Dictionary <string, object>(); sqlParameters.Add(":inprogress", true); sqlParameters.Add(":scanrequested", true); sqlParameters.Add(":jobstatus", "Ready"); sqlParameters.Add(":statusdescription", String.Format("Backup Started: {0:MMMM d, yyyy HH:mm:ss}", DateTime.Now)); sqlParameters.Add(":machinename", Environment.MachineName); sqlParameters.Add(":jobkey", JobKey); JobFileName = _tokens.Resolve("Scheduler", "BackupDatabase", "Missing the Scheduler/BackupDatabase attribute with the name of the backup definition. SQL query. Please add the name of the backup definition to the settings file under SchedulerSettings/Tokens/Scheduler BackupDatabase"); // Look for backup database request DataTable dt = SqlUtilities.GetData(_ConnectionString, _SqlNextDbBackup); if ((dt == null) || (dt.Rows.Count == 0)) { return(false); } for (int i = 0; i < dt.Rows.Count; i++) { try { JobKey = MiscUtilities.ObjectToInteger(dt.Rows[0]["pkey"], "No primary key is definied"); sqlParameters[":jobkey"] = JobKey; RunJava(JobKey, JobFileName, RawJobFileName); // Job finished update record. sqlParameters[":jobstatus"] = "Backup Completed"; sqlParameters[":statusdescription"] = String.Format("Backup Completed: {0:MMMM d, yyyy HH:mm:ss}", DateTime.Now); } catch (Exception ex) { LocalLog.AddLine("Database Backup Error: " + ex.Message); sqlParameters[":jobstatus"] = "Error"; String message = String.Format("Recorded: {0:MMMM d, yyyy HH:mm:ss}, Message: {1} ", DateTime.Now, ex.Message); if (message.Length > 99) { message = message.Substring(0, 99); } sqlParameters[":statusdescription"] = message; } SqlUtilities.ExcecuteNonQuery(_ConnectionString, _SqlUpdateInProgressStatus, sqlParameters); } return(false); }
public static void logEnvironment() { try { LocalLog.AddLine(String.Format("Process account name {0}", Environment.UserDomainName + "/" + Environment.UserName)); LocalLog.AddLine(String.Format("Machine Name {0}", Environment.MachineName)); LocalLog.AddLine(String.Format("Framework Version {0}", "Microsoft NET v" + Environment.Version.ToString())); LocalLog.AddLine(String.Format("Operating system name {0}", Environment.OSVersion)); LocalLog.AddLine(String.Format("Is 64bit {0}", Environment.Is64BitOperatingSystem ? "Yes" : "No")); LocalLog.AddLine(String.Format("User working directory {0}", Environment.CurrentDirectory)); } catch (Exception ex) { LocalLog.AddLine("WARNING: Could not read all environment settings."); LocalLog.AddLine(String.Format("WARNING: Reason: {0}", ex.Message)); } }
protected Boolean RunDefinition(int JobKey, String JobFileName, String RawJobFileName, String processingMessage) { Dictionary <String, Object> sqlParameters = new Dictionary <string, object>(); sqlParameters.Add(":inprogress", true); sqlParameters.Add(":scanrequested", true); sqlParameters.Add(":jobstatus", processingMessage); sqlParameters.Add(":statusdescription", String.Format("Started: {0:MMMM d, yyyy HH:mm:ss}", DateTime.Now)); sqlParameters.Add(":machinename", Environment.MachineName); sqlParameters.Add(":jobkey", JobKey); try { SqlUtilities.ExcecuteNonQuery(_ConnectionString, _SqlUpdateInProgressStatus, sqlParameters); // Shell to the JAVA program and run it. RunJava(JobKey, JobFileName, RawJobFileName); // Job finished update record. sqlParameters[":inprogress"] = false; sqlParameters[":scanrequested"] = false; sqlParameters[":jobstatus"] = "Completed"; sqlParameters[":statusdescription"] = String.Format("Completed: {0:MMMM d, yyyy HH:mm:ss}", DateTime.Now); sqlParameters[":machinename"] = null; // Error out this queue item and rest for this application. } catch (Exception ex) { LocalLog.AddLine("Code Scan Error: " + ex.Message); LocalLog.AddLine(ex); String message = String.Format("Recorded: {0:MMMM d, yyyy HH:mm:ss}, Message: {1} ", DateTime.Now, ex.Message); if (message.Length > 99) { message = message.Substring(0, 99); } sqlParameters[":statusdescription"] = message; sqlParameters[":inprogress"] = false; sqlParameters[":scanrequested"] = false; sqlParameters[":jobstatus"] = "Error"; sqlParameters[":machinename"] = null; } SqlUtilities.ExcecuteNonQuery(_ConnectionString, _SqlUpdateInProgressStatus, sqlParameters); return(false); }
protected Boolean BackupCastDatabase() { Dictionary <String, Object> sqlParameters = new Dictionary <string, object>(); sqlParameters.Add(":jobkey", _JobKey); sqlParameters.Add(":jobstatus", "Ready"); sqlParameters.Add(":statusdescription", "Ready"); try { // Look for the next rescan request. DataTable requests = SqlUtilities.GetData(_ConnectionString, "SELECT pkey, definition_name, scan_status, dbprefix FROM fnma_measure8.scan_manager WHERE in_progress and scan_status='Backup Database'"); if ((requests == null) || (requests.Rows.Count == 0)) { return(false); } _JobKey = (int)requests.Rows[0]["pkey"]; _JobFileName = (String)requests.Rows[0]["definition_name"]; sqlParameters[":jobkey"] = _JobKey; _JobFileName = "DatabaseBackup.xml"; // Shell to the JAVA program and run it. RunJava(); // Job finished update record. sqlParameters[":jobstatus"] = "Backup Completed"; sqlParameters[":statusdescription"] = String.Format("Backup Completed: {0:MMMM d, yyyy HH:mm:ss}", DateTime.Now); } catch (Exception ex) { LocalLog.AddLine("Database Backup Error: " + ex.Message); sqlParameters[":jobstatus"] = "Error"; String message = String.Format("Recorded: {0:MMMM d, yyyy HH:mm:ss}, Message: {1} ", DateTime.Now, ex.Message); if (message.Length > 99) { message = message.Substring(0, 99); } sqlParameters[":statusdescription"] = message; } SqlUtilities.ExcecuteNonQuery(_ConnectionString, _SqlUpdateStatus, sqlParameters); return(false); }
protected void CheckScheduledJobs() { DateTime currentDateTime = DateTime.Now; // Check = SELECT pkey, definition_name FROM fnma_measure8.scan_manager WHERE scheduled_date is not null and scheduled_date < current_timestamp and definition_name is not null // Queue = UPDATE fnma_measure8.scan_manager SET scan_requested=true, request_date=current_timestamp, scan_status='Queued', machine_name=null, scheduled_date=null WHERE pkey=:key and scan_requested=false and machine_name is null and in_progress=false and scan_status='Completed' // Put here to lighten the load on the database server, system checks schedules every 2 minutes. // With 4 machines running this code, the offset between them should ensure pretty quick schedule pickups. if (_nextScheduleCheck > currentDateTime) { return; } DataTable dt = SqlUtilities.GetData(_ConnectionString, _sqlScheduleCheck); if ((dt != null) && (dt.Rows != null) && (dt.Rows.Count > 0)) { Dictionary <String, Object> sqlParameters = new Dictionary <String, Object>(); sqlParameters.Add(":key", -1); sqlParameters.Add(":requestdate", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); for (int i = 0; i < dt.Rows.Count; i++) { string job_name = ""; try { if ((DateTime)dt.Rows[i]["scheduled_date"] < DateTime.Now) { job_name = MiscUtilities.ObjectToStringDefault(dt.Rows[i]["definition_name"], "Definition name is blank."); sqlParameters[":key"] = MiscUtilities.ObjectToInteger(dt.Rows[i]["pkey"], "No primary key defined."); SqlUtilities.ExcecuteNonQuery(_ConnectionString, _sqlQueueScheduledJob, sqlParameters); } } catch (Exception ex) { LocalLog.AddLine("Error while queuing scheduled job. Error message was: " + ex.Message); _AppLog.WriteEntry(ex.StackTrace, System.Diagnostics.EventLogEntryType.Error); } } } _nextScheduleCheck = DateTime.Now.AddMinutes(2); }
protected DataRow GetNextJob() { try { Dictionary <String, Object> sqlParameters = new Dictionary <String, Object>(); sqlParameters.Add(":machinename", Environment.MachineName); DataTable dt = SqlUtilities.GetData(_ConnectionString, _SqlNextJob, sqlParameters); if ((dt != null) && (dt.Rows != null) && (dt.Rows.Count > 0)) { return(dt.Rows[0]); } return(null); } catch (Exception ex) { LocalLog.AddLine("Error while getting next job from queue. Error message was: " + ex.Message); _AppLog.WriteEntry(ex.StackTrace, System.Diagnostics.EventLogEntryType.Error); } return(null); }
protected Boolean RunDefinition(String processingMessage) { Dictionary <String, Object> sqlParameters = new Dictionary <string, object>(); sqlParameters.Add(":jobkey", _JobKey); sqlParameters.Add(":jobstatus", "Ready"); sqlParameters.Add(":statusdescription", "Ready"); try { sqlParameters[":jobkey"] = _JobKey; // Run the definition and wait till it finishes. sqlParameters[":jobstatus"] = processingMessage; sqlParameters[":statusdescription"] = String.Format("Started: {0:MMMM d, yyyy HH:mm:ss}", DateTime.Now); SqlUtilities.ExcecuteNonQuery(_ConnectionString, _SqlUpdateStatus, sqlParameters); // Shell to the JAVA program and run it. RunJava(); // Job finished update record. sqlParameters[":jobstatus"] = "Completed"; sqlParameters[":statusdescription"] = String.Format("Completed: {0:MMMM d, yyyy HH:mm:ss}", DateTime.Now); // Error out this queue item and rest for this application. } catch (Exception ex) { LocalLog.AddLine("Code Scan Error: " + ex.Message); sqlParameters[":jobstatus"] = "Error"; String message = String.Format("Recorded: {0:MMMM d, yyyy HH:mm:ss}, Message: {1} ", DateTime.Now, ex.Message); if (message.Length > 99) { message = message.Substring(0, 99); } sqlParameters[":statusdescription"] = message; SqlUtilities.ExcecuteNonQuery(_ConnectionString, String.Format("UPDATE fnma_measure8.automated_queue SET ready=false, status='ERROR' WHERE ready and scan_manager_pkey={0}", _JobKey)); } SqlUtilities.ExcecuteNonQuery(_ConnectionString, _SqlJobFinished, sqlParameters); return(false); }
protected override void OnStart(string[] args) { LocalLog.AddLine("Starting the mutliprocessor scheduler service."); logEnvironment(); String path = MiscUtilities.AppParentPath() + Path.DirectorySeparatorChar; _taskManager = new ScanManager.TaskManager(); _CleanupLogs = _taskManager.CleanupLogs; _threadPool = new ScanManager.BackgroundProcessing[_taskManager.ThreadPoolSize]; try { LocalLog.AddLine("Starting Timer: Interval is 30 seconds"); _timer = new System.Timers.Timer(30000); // every 30 seconds _timer.Elapsed += new System.Timers.ElapsedEventHandler(pieJobCheck); _timer.Start(); LocalLog.AddLine("Service is running."); } catch (Exception ex) { LocalLog.AddLine("Error while starting timer. " + ex.Message); } }
//protected Boolean RunScan() //{ // Dictionary<String, Object> aParams = new Dictionary<string, object>(); // aParams.Add(":jobkey", _JobKey); // aParams.Add(":jobstatus", "Ready"); // aParams.Add(":statusdescription", "Ready"); // aParams.Add(":machinename", Environment.MachineName); // try // { // // Set any definitions left in_progress from previous run to error. // SqlUtilities.ExcecuteNonQuery(_ConnectionString, _SqlErrorOutInProgress, aParams); // //// Look for the next rescan request. // SqlUtilities.ExcecuteNonQuery(_ConnectionString, _SqlTakeNextRequest, aParams); // DataTable requests = SqlUtilities.GetData(_ConnectionString, _SqlNextJob); // //if ((requests == null) || (requests.Rows.Count == 0)) return false; // //_JobKey = (int)requests.Rows[0]["pkey"]; // //_JobFileName = (String)requests.Rows[0]["definition_name"]; // aParams[":jobkey"] = _JobKey; // // Run the definition and wait till it finishes. // aParams[":jobstatus"] = "Processing"; // aParams[":statusdescription"] = String.Format("Started: {0:MMMM d, yyyy HH:mm:ss}", DateTime.Now); // SqlUtilities.ExcecuteNonQuery(_ConnectionString, _SqlUpdateStatus, aParams); // // Shell to the JAVA program and run it. // RunJava(); // // Job finished update record. // aParams[":jobstatus"] = "Completed"; // aParams[":statusdescription"] = String.Format("Completed: {0:MMMM d, yyyy HH:mm:ss}", DateTime.Now); // } // catch (Exception ex) // { // LocalLog.AddLine("Code Scan Error: " + ex.Message); // aParams[":jobstatus"] = "Error"; // aParams[":statusdescription"] = String.Format("Recorded: {0:MMMM d, yyyy HH:mm:ss}, Message: {1} ", DateTime.Now, ex.Message).Substring(0, 99); // // Error out this queue item and rest for this application. // SqlUtilities.ExcecuteNonQuery(_ConnectionString, String.Format("UPDATE fnma_measure8.automated_queue SET ready=false, status='ERROR' WHERE ready and scan_manager_pkey={0}", _JobKey)); // } // SqlUtilities.ExcecuteNonQuery(_ConnectionString, _SqlJobFinished, aParams); // return false; //} protected void RunJava() { String args = String.Format("-s {0} -d {1} JobKey={2} DefinitionFile={3}", WrapSpaces(_SettingsFile), WrapSpaces(_JobFileName), _JobKey, _RawJobFileName); using (Process clientProcess = new Process()) { clientProcess.StartInfo.UseShellExecute = false; clientProcess.StartInfo.RedirectStandardOutput = true; clientProcess.StartInfo.RedirectStandardError = true; clientProcess.StartInfo.CreateNoWindow = true; clientProcess.StartInfo.WorkingDirectory = MiscUtilities.AppParentPath(); clientProcess.StartInfo.FileName = WrapSpaces(_JavaHome + "java.exe"); clientProcess.StartInfo.Arguments = @"-jar " + WrapSpaces(_JarPath) + " " + args; LocalLog.AddLine(String.Format("Command line: {0} {1}", WrapSpaces(_JavaHome + "java.exe"), @"-jar " + WrapSpaces(_JarPath) + " " + args)); clientProcess.Start(); string output = clientProcess.StandardOutput.ReadToEnd(); clientProcess.WaitForExit(); int exitcode = clientProcess.ExitCode; if (exitcode > 0) { throw new Exception(String.Format("Run JAVA error ExitCode {0} running {1} {2}", exitcode, clientProcess.StartInfo.FileName, clientProcess.StartInfo.Arguments)); } } }
protected void ReadQueries() { LocalLog.AddLine("Confirming LogPath.."); String logPath = OptionalAttribute("//Configuration", "LogPath"); if (!String.IsNullOrEmpty(logPath)) { // Check to see if it Exists if (!Directory.Exists(logPath)) { throw new Exception(String.Format("Configured LogPath directory {0} does not exist.", logPath)); } // Check to see if you can write to it if (!logPath.EndsWith(Path.DirectorySeparatorChar.ToString())) { logPath += Path.DirectorySeparatorChar; } String filename = logPath + "test.tmp"; try { using (FileStream fstream = new FileStream(filename, FileMode.Create)) { using (TextWriter writer = new StreamWriter(fstream)) { writer.WriteLine("Verify rights."); writer.Close(); } fstream.Close(); } File.Delete(filename); } catch (Exception ex) { throw new Exception(String.Format("Do not have write to create/write log files in {0}. " + ex.Message, logPath), ex); } LocalLog.AddLine(String.Format("LogPath set to {0}", logPath)); } LocalLog.AddLine("Reading system tokens.."); SetupTokens(); LocalLog.AddLine("Reading system SQL queries.."); String Message = "Missing the {0}.{1} SQL query. Please add the query into the settings file under Settings/Tokens/{0}.{1}"; _SqlNextJob = ReadTokenValue("SelfServiceScan", "NextJob", Message); _SqlErrorOutInProgress = ReadTokenValue("SelfServiceScan", "NetErrorOldInProgress", Message); _SqlUpdateStatus = ReadTokenValue("SelfServiceScan", "NetUpdateStatus", Message); _SqlJobFinished = ReadTokenValue("SelfServiceScan", "NetFinishedProcessing", Message); _SqlJobStarted = ReadTokenValue("SelfServiceScan", "NetStartProcessing", Message); _SqlTakeNextJob = ReadTokenValue("SelfServiceScan", "NetTakeNextJob", Message); //_SqlNextJob = ReadTokenValue("SelfServiceScan", "NetNextJob", Message); LocalLog.AddLine("Reading connection information.."); _ConnectionString = ReadAttribute("//Connections/Connection[@ID='NetScanManager']", "ConnectionString", "No ScanManager connection information found."); LocalLog.AddLine("Reading location of JAVA.."); _JavaHome = ReadTokenValue("JavaHome", "Path", "Missing path to JAVA bin directory. Please add the path to Settings/Tokens/JavaHome.Path"); if (!_JavaHome.EndsWith("\\")) { _JavaHome += "\\"; } LocalLog.AddLine(String.Format("JAVA location is {0}", _JavaHome)); LocalLog.AddLine("Reading service mode.."); String BackupMachine = ReadOptionalTokenValue("SelfServiceScan", "DatabaseBackupServer", "False"); if (!String.IsNullOrEmpty(BackupMachine) && "True".Equals(BackupMachine, StringComparison.CurrentCultureIgnoreCase)) { _IsDbBackup = true; LocalLog.AddLine("*** Running as database backup machine: True"); } else { LocalLog.AddLine("Running as database backup machine: false"); } }
public void ProcessQueue() { int JobKey = -1; try { // Validate the processing job and update those that are no long running. ValidateRunningJobs(); // Check for any scheduled work CheckScheduledJobs(); // Check for any queued work DataRow dr = GetNextJob(); if (dr == null) { return; } JobKey = MiscUtilities.ObjectToInteger(dr["pkey"], "No primary key is definied"); String JobFileName = MiscUtilities.ObjectToString(dr["definition_name"], "No definition file name is defined."); String RawJobFileName = JobFileName; String lockFilename = MiscUtilities.LockFilename(RawJobFileName); String action_requested = MiscUtilities.ObjectToStringDefault(dr["action_requested"], "rescan"); using (FileStream fs = new FileStream(lockFilename, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine("While file is locked open, processing thread is running. Seeing this message means the thread may not be running, please delete this file."); if (_IsDbBackup) { BackupCastDatabase(JobKey, JobFileName, RawJobFileName); } else { String message = "Processing"; switch (action_requested.ToLower()) { case "publish": JobFileName = _tokens.Resolve("Scheduler", "PublishDefinition", "No publish definition defined in the settings file. Please add a Tokens/Scheduler PublishDefinition"); message = "Publishing"; break; case "empty": case "rescan": message = "Processing"; break; case "onboard": JobFileName = _tokens.Resolve("Scheduler", "OnBoardDefinition", "No onboard definition defined in the settings file. Please add a Tokens/Scheduler OnBoardDefinition"); message = "OnBoarding"; break; default: throw new Exception(String.Format("{0} is not a valid action request.", action_requested)); } // Kick off the definition RunDefinition(JobKey, JobFileName, RawJobFileName, message); } } fs.Close(); } File.Delete(lockFilename); } catch (Exception ex) { lock (_lock) { LocalLog.AddLine("Queue Error: " + ex.Message); } _AppLog.WriteEntry(ex.StackTrace, System.Diagnostics.EventLogEntryType.Error); if (JobKey > 0) { Dictionary <String, Object> sqlParameters = new Dictionary <string, object>(); sqlParameters.Add(":jobkey", JobKey); sqlParameters.Add(":jobstatus", "Error"); sqlParameters.Add(":inprogress", false); sqlParameters.Add(":scanrequested", false); String message = String.Format("Recorded: {0:MMMM d, yyyy HH:mm:ss}, Message: {1} ", DateTime.Now, ex.Message); if (message.Length > 99) { message = message.Substring(0, 99); } sqlParameters.Add(":statusdescription", message); SqlUtilities.ExcecuteNonQuery(_ConnectionString, _SqlUpdateInProgressStatus, sqlParameters); } } }
protected void InitializeSystem() { LocalLog.AddLine("Reading system SQL queries.."); String Message = "Missing the {0} {1} SQL query. Please add the query into the settings file under SchedulerSettings/Tokens/{0} {1}"; _SqlNextJob = _tokens.Resolve("Scheduler", "NextJob", Message); _SqlJobsInProgress = _tokens.Resolve("Scheduler", "JobsInProgress", Message); _SqlUpdateInProgressStatus = _tokens.Resolve("Scheduler", "UpdateInProgress", Message); _SqlNextDbBackup = _tokens.Resolve("Scheduler", "NextDbBackup", Message); _sqlScheduleCheck = _tokens.Resolve("Scheduler", "ScheduledJobCheck", Message); _sqlQueueScheduledJob = _tokens.Resolve("Scheduler", "QueueScheduledJob", Message); String connID = _tokens.Resolve("Scheduler", "ConnectionID", "Missing the connection ID for the database server. Please add the ConnectionID to the settings file under SchedulerSettings/Tokens/{0} {1}"); LocalLog.AddLine("Reading connection information.."); _ConnectionString = XmlUtilities.RequiredAttribute(_Settings, "//Connections/Connection[@ID='" + connID + "']", "ConnectionString", "No " + connID + " connection information found."); LocalLog.AddLine("Reading location of JAVA.."); _JavaHome = _tokens.Resolve("JavaHome", "Path", "Missing path to JAVA bin directory. Please add the path to SchedulerSettings/Tokens/JavaHome Path"); if (!_JavaHome.EndsWith("\\")) { _JavaHome += "\\"; } LocalLog.AddLine(String.Format("JAVA location is {0}", _JavaHome)); _JarPath = _tokens.Resolve("PieJar", "Path", "Missing path to PIE JAR. Please add the path to SchedulerSettings/Tokens/PieJar Path"); _SettingsFile = _tokens.Resolve("PieSettings", "Path", "Missing path to PIE Settings file. Please add the path to SchedulerSettings/Tokens/PieSettings Path"); if (!File.Exists(_JarPath)) { String errorMessage = String.Format("JAR file {0} not found.", _JarPath); FileNotFoundException ex = new FileNotFoundException(errorMessage); _AppLog.WriteEntry(ex.StackTrace, System.Diagnostics.EventLogEntryType.Error); throw ex; } else { LocalLog.AddLine("Using PIE JAR found at " + _JarPath); } if (!File.Exists(_SettingsFile)) { FileNotFoundException ex = new FileNotFoundException(String.Format("PIE Settings file {0} not found.", _SettingsFile)); _AppLog.WriteEntry(ex.StackTrace, System.Diagnostics.EventLogEntryType.Error); throw ex; } else { LocalLog.AddLine("Using PIE Settings file from " + _SettingsFile); } LocalLog.AddLine("Reading service mode.."); String ServiceMode = _tokens.ResolveOptional("Scheduler", "ServiceMode", "Normal").Replace(" ", ""); if (ServiceMode.IndexOf("DatabaseBackup", StringComparison.CurrentCultureIgnoreCase) != -1) { _IsDbBackup = true; LocalLog.AddLine("*** Running as database backup machine."); } else if (ServiceMode.IndexOf("|SiteDeployment|", StringComparison.CurrentCultureIgnoreCase) != -1) { _IsSiteDeployment = true; LocalLog.AddLine("*** Running as CED site delpoyment machine."); } else { LocalLog.AddLine("Running in normal mode."); } }
public Boolean ProcessQueue() { try { if (this._IsDbBackup) { return(BackupCastDatabase()); } else { Dictionary <String, Object> aParams = new Dictionary <String, Object>(); aParams.Add(":machinename", Environment.MachineName); // Set any definitions left in_progress from previous run to error. -- turned off, to test. SqlUtilities.ExcecuteNonQuery(_ConnectionString, _SqlErrorOutInProgress, aParams); // Update next ready record with machine name int gotOne = SqlUtilities.ExcecuteNonQuery(_ConnectionString, _SqlNextJob, aParams); if (gotOne == 0) { return(false); } // Check for next request DataTable requests = SqlUtilities.GetData(_ConnectionString, _SqlNextJob, aParams); if ((requests == null) || (requests.Rows.Count == 0)) { // Ready the next application in the automated queue DataTable next_automated_queue = SqlUtilities.GetData(_ConnectionString, "SELECT scan_manager_pkey, code_loc_type, code_loc_url, version_name FROM fnma_measure8.automated_queue WHERE ready and scan_manager_pkey > 0 ORDER BY scan_order, id"); if ((next_automated_queue != null) && (next_automated_queue.Rows.Count > 0)) { int updatedCount = 0; int lastPkey = -1; foreach (DataRow dr in next_automated_queue.Rows) { int scanManagerPkey = ObjectToInteger(dr["scan_manager_pkey"], "No scan_manager_pkey is definied"); if (scanManagerPkey == lastPkey) { continue; } lastPkey = scanManagerPkey; try { int codeLocationType = ObjectToInteger(dr["code_loc_type"], "No code_loc_type definied"); String codeLocationUrl = ObjectToString(dr["code_loc_url"], "No code_loc_url defined."); String versionName = ObjectToString(dr["version_name"], "No version_name defined."); String request_date = String.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now); // Queue the next rescan updatedCount = SqlUtilities.ExcecuteNonQuery(_ConnectionString, String.Format("UPDATE fnma_measure8.scan_manager SET scan_requested=true, request_date='{0}', scan_status='Queued', code_url='{1}', code_version='{2}', action_requested='rescan', code_location_type={3} WHERE pkey={4} and machine_name is null and in_progress=false and scan_requested=false and scan_status='Completed'", request_date, codeLocationUrl, versionName, codeLocationType, scanManagerPkey)); } catch (Exception exAutomated) { String error = exAutomated.Message; // Error out this queue item and rest for this application. SqlUtilities.ExcecuteNonQuery(_ConnectionString, String.Format("UPDATE fnma_measure8.automated_queue SET ready=false, status='ERROR' WHERE ready and scan_manager_pkey={0}", scanManagerPkey)); } if (updatedCount > 0) { break; } } } //if ((next_automated_queue != null) && (next_automated_queue.Rows.Count > 0)) //{ // int scanManagerPkey = ObjectToInteger(next_automated_queue.Rows[0]["scan_manager_pkey"], "No scan_manager_pkey is definied"); // try // { // int codeLocationType = ObjectToInteger(next_automated_queue.Rows[0]["code_loc_type"], "No code_loc_type definied"); // String codeLocationUrl = ObjectToString(next_automated_queue.Rows[0]["code_loc_url"], "No code_loc_url defined."); // String versionName = ObjectToString(next_automated_queue.Rows[0]["version_name"], "No version_name defined."); // String request_date = String.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now); // // Queue the next rescan // SqlUtilities.ExcecuteNonQuery(_ConnectionString, String.Format("UPDATE fnma_measure8.scan_manager SET scan_requested=true, request_date='{0}', scan_status='Queued', code_url='{1}', code_version='{2}', action_requested='rescan', code_location_type={3}, machine_name='{5}' WHERE pkey={4} and machine_name is null", request_date, codeLocationUrl, versionName, codeLocationType, scanManagerPkey, Environment.MachineName)); // } // catch (Exception exAutomated) // { // String error = exAutomated.Message; // // Error out this queue item and rest for this application. // SqlUtilities.ExcecuteNonQuery(_ConnectionString, String.Format("UPDATE fnma_measure8.automated_queue SET ready=false, status='ERROR' WHERE ready and scan_manager_pkey={0}", scanManagerPkey)); // } //} return(false); } _JobKey = ObjectToInteger(requests.Rows[0]["pkey"], "No primary key is definied"); _JobFileName = ObjectToString(requests.Rows[0]["definition_name"], "No definition file name is defined."); _RawJobFileName = _JobFileName; // Until the UI is updated, defaulting to 'rescan' //String action_requested = ObjectToString(requests.Rows[0]["action_requested"], "Action_request value is null. Typical actions include Publish, Rescan, or Onboard."); String action_requested = ObjectToStringDefault(requests.Rows[0]["action_requested"], "rescan"); String message = "Scanning"; switch (action_requested.ToLower()) { case "publish": _JobFileName = ReadTokenValue("SelfServiceScan", "PublishDefinition", "No publish definition defined in the settings file. Please add a Tokens/SelfServiceScan PublishDefinition"); message = "Publishing"; break; case "empty": case "rescan": message = "Processing"; break; case "onboard": _JobFileName = ReadTokenValue("SelfServiceScan", "OnBoardDefinition", "No onboard definition defined in the settings file. Please add a Tokens/SelfServiceScan OnBoardDefinition"); message = "OnBoarding"; break; default: throw new Exception(String.Format("{0} is not a valid action request.", action_requested)); } return(RunDefinition(message)); } } catch (Exception ex) { LocalLog.AddLine("Queue Error: " + ex.Message); _AppLog.WriteEntry(ex.StackTrace, System.Diagnostics.EventLogEntryType.Error); if (_JobKey > 0) { Dictionary <String, Object> aParams = new Dictionary <string, object>(); aParams.Add(":jobkey", _JobKey); aParams.Add(":jobstatus", "Error"); String message = String.Format("Recorded: {0:MMMM d, yyyy HH:mm:ss}, Message: {1} ", DateTime.Now, ex.Message); if (message.Length > 99) { message = message.Substring(0, 99); } aParams.Add(":statusdescription", message); SqlUtilities.ExcecuteNonQuery(_ConnectionString, _SqlJobFinished, aParams); } } return(false); }
private void pieJobCheck(object sender, System.Timers.ElapsedEventArgs e) { try { _timer.Stop(); if (DateTime.Now > _nextMessage) { LocalLog.AddLine("Hourly status. Still checking every 30 seconds.."); } if (_CleanupLogs && (DateTime.Now > _nextLogCleanUp)) { LocalLog.AddLine("Starting to clean up old log files ..."); ScanManager.Common.LogCleanUp lc = new ScanManager.Common.LogCleanUp(_taskManager.ResolveToken("PieLogs", "Path"), _taskManager.ResolveToken("PieLogs", "FileExtension")); LocalLog.AddLine(lc.findOrphanFiles()); LocalLog.AddLine("Completed clean-up."); _nextLogCleanUp = DateTime.Now.AddHours(4); } // Look for free thread for (int i = 0; i < _threadPool.Length; i++) { if ((_threadPool[i] == null) || _threadPool[i].IsAvailable) { _threadPool[i] = new ScanManager.BackgroundProcessing(_taskManager); _threadPool[i].DoWork(); } } if (DateTime.Now > _nextMessage) { LocalLog.AddLine("Check complete."); _nextMessage = DateTime.Now.AddHours(1); } } catch (Exception ex) { LocalLog.AddLine("ERROR: " + ex.Message); } finally { String stopFile = String.Format("{0}{1}stop.txt", MiscUtilities.AppPath(), Path.DirectorySeparatorChar); if (File.Exists(stopFile)) { LocalLog.AddLine(String.Format("Stopping service, waiting for processes to complete. Stop file ({0}) located on the drive.", stopFile)); File.Delete(stopFile); Boolean threadsRunning = true; while (threadsRunning) { threadsRunning = false; for (int i = 0; i < _threadPool.Length; i++) { if ((_threadPool[i] != null) || _threadPool[i].IsBusy) { threadsRunning = true; break; } } if (threadsRunning) { Thread.Sleep(30000); } } Stop(); } else { _timer.Start(); } } }