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); } } } }
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); }
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); }
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); }