// Make Full Pass Through Processing. public int Run(int pass = 0, bool logBeat = false) { int rc = 0; bool initiatorStatus = getDictionaryEntry("ProcessJobs", "Y").Equals("Y"); // Validate Connection. if (validateConnection(MasterConnection)) { switch (pass) { // Pass 1: Evaluate current run states. // Review Active Jobs. case 1: //myLog.WriteEntry("Starting Pass 0"); if (CheckStatus() >= 0) //Initialize(); //if (bInitialized) { // Inspect each running job. //XLogger(1200, 0, string.Format("Jobs Executing={0}; Allowed={1}", runningJobs.Count, jobsAllowed)); foreach (XTRMJob thisJob in runningJobs) { //rc = thisJob.Initialize(thisJob.jobSerial); //if (rc >= 0) //{ if (thisJob.jobStatus == 1) { int lActiveTasks = 0; // Inspect each running task (should be one at most). foreach (XTRMTask thisTask in thisJob.tasks) { // Refresh Task. //rc = thisTask.Initialize(thisTask.taskSerial); //if (rc >= 0) //{ /* * if (thisTask.taskStatus > 9) * { * // Done. * // Start next one if no other active tasks. * //thisJob.tasks.Remove(thisTask); * } */ if (thisTask.taskStatus == 1) { lActiveTasks++; if (false) { // If not original task object, then let's check closer. // This can happen if XExecutive gets re-initialized with running jobs. if (!thisTask.taskOriginator) { if (thisTask.taskPID != -1) { // Future, Check the [time]-limit? // Check the PID; If exists, then wait. try { Process thisProcess = Process.GetProcessById(thisTask.taskPID); if (thisProcess.HasExited) { thisTask.taskStatus = 95; // Complete; disposition unknown. rc = thisTask.Save(); } } catch (Exception ex) { // Mark the task as complete. thisTask.taskStatus = 91; // Complete; disposition unknown. rc = thisTask.Save(); } } } } } else { // Other status values ignored (for now) - should not be pending! //XLogger(1201, 0, string.Format("Unexpected TaskStatus Value={0}", thisTask.taskStatus)); // If result < 0, then mark job for termination! if (thisTask.taskResult < 0) { XLogger(1201, 0, string.Format("Job #{0} Terminated Due to Task #{1} Result={2}.", thisJob.jobSerial, thisTask.taskSerial, thisTask.taskResult)); thisJob.jobStatus = 98; // Terminated. thisJob.jobStop = DateTime.Now.ToString(); lActiveTasks = 1; rc = thisJob.Save(); if (rc < 0) { XLogger(1227, 0, string.Format("thisJob.Save() Failed; rc={0}", rc)); } // Send email to XBot Admin. string contact = XTRMObject.getDictionaryEntry("AdminMail", "*****@*****.**"); Notify(contact, null, string.Format("Job #{0}/{1} Failed {2}", thisJob.jobSerial, thisTask.taskSerial, thisJob.jobName), string.Format("Exec={0}; Path={1}; Result={2}.", thisTask.taskExecutable, thisTask.taskPath, thisTask.taskResult)); } } //} //else //{ // Error on Initialize() of XTask. // XLogger(1202, 0, string.Format("XTask.Initialize() Failure; rc={0}", rc)); //} } //if (thisJob.tasks.Count == 0) if (lActiveTasks == 0) { // Look for next task (within this job). List <int> pendingTaskSerials = InventoryPendingTasks(thisJob); if (pendingTaskSerials.Count <= 0) { // Job Complete thisJob.jobStatus = 99; thisJob.jobStop = DateTime.Now.ToString(); rc = thisJob.Save(); if (rc < 0) { XLogger(1203, 0, string.Format("thisJob.Save() Failed; rc={0}", rc)); } //runningJobs.Remove(thisJob); } else { XTRMTask pendingTask = new XTRMTask(false); if (pendingTaskSerials[0] < 1) { // Error XLogger(1204, 0, "No Pending TaskSerials"); } rc = pendingTask.Initialize(pendingTaskSerials[0]); if (rc >= 0) { //thisJob.tasks.Add(pendingTask); //runningTasks.Add(pendingTask); rc = pendingTask.Start(); if (rc < 0) { XLogger(1205, 0, string.Format("pendingTask.Start() Failed; Serial={0}; rc={1}", pendingTask.taskSerial, rc)); // Send email to XBot Admin. string contact = XTRMObject.getDictionaryEntry("AdminMail", "*****@*****.**"); Notify(contact, null, string.Format("Job Task #{0}/{1} {2} Could Not Start", thisJob.jobSerial, pendingTask.taskSerial, pendingTask.taskName), string.Format("Exec={0}; Path={1}.", pendingTask.taskExecutable, pendingTask.taskPath)); } else { XLogger(1206, 0, string.Format("Task #{0} Started", pendingTask.taskSerial)); } //thisJob.tasks.Add(pendingTask); } else { // Error XLogger(1207, 0, string.Format("XTask.Initialize() Failure for Pending Task; rc={0}", rc)); } } } else { // Wait for other pending task(s) - should not happen! //XLogger(1208, 0, "Waiting for Job Task(s) to Complete."); } //} //else //{ // Error on Initialize() of XJob. // XLogger(1209, 0, string.Format("XJob.Initialize() Failure; rc={0}", rc)); //} } } // Purge Jobs that have completed! jobStatus >= 9 bool purgeFlag = true; while (purgeFlag) { purgeFlag = false; foreach (XTRMJob thisJob in runningJobs) { if (thisJob.jobStatus >= 9) { runningJobs.Remove(thisJob); purgeFlag = true; break; } } } } else { // Error! XLogger(1210, 0, "Executive Not Initialized in Pass 0"); } break; // Pass 0: Start New Jobs. // Review Pending Jobs. // Spawn Candidate Jobs. case 0: if (initiatorStatus) { //myLog.WriteEntry("Starting Pass 1"); // If we are under the MPR, then start a new job. if (runningJobs.Count < jobsAllowed) { rc = LoadCandidateJobs(); if (rc >= 0) { foreach (int thisJobSerial in candidateJobs) { // Mark the Job as Started. XTRMJob newJob = new XTRMJob(false); if (runningJobs.Count < jobsAllowed) { rc = newJob.Initialize(thisJobSerial); if (rc >= 0) { newJob.jobStatus = 1; newJob.jobStart = DateTime.Now.ToString(); //newJob.jobStop = DBNull.Value.ToString(); rc = newJob.Save(); if (rc >= 0) { // Job Started; first task will start in next call to case 0: runningJobs.Add(newJob); XLogger(1222, 0, string.Format("Job #{0} Started; Executing={1}; Allowed={2}", newJob.jobSerial, runningJobs.Count, jobsAllowed)); } else { // Error XLogger(1211, 0, string.Format("newJob.Save() Failure; rc={0}", rc)); } } else { // Initialize Error (XJob). XLogger(1212, 0, string.Format("newJob.Initialize() Failure; rc = {0}", rc)); } } else { XLogger(1213, 0, "Waiting for Runnng Jobs To Complete"); break; } } } else { // Error XLogger(1214, 0, string.Format("Failure in LoadCandidateJobs(); rc={0}", rc)); } } else { //XLogger(1215, 0, "Waiting for Capacity"); // Wait for capacity. } } break; } } else { myLog.WriteEntry("Master XDB Connection Failure"); } return(rc); }
public int CreateJob(XTRMJob thisJob, XTRMEvent thisEvent, Dictionary <string, string> dynConfig = null) { int rc = 0; int jobSerial = -1; int taskSerial = -1; XTRMJob newJob = new XTRMJob(); XTRMTask newTask = new XTRMTask(); newJob.Initialize(-1); newJob.jobType = thisJob.jobType; newJob.jobEvent = thisEvent.eventSerial; newJob.jobSequence = 0; newJob.jobLimit = -1; newJob.jobDisplay = "TEST"; //newJob.jobName = thisJob.jobName + " - " + thisEvent.eventParm1; newJob.jobName = ResolveParm(thisJob.jobName, thisEvent, thisJob.config, taskSerial) + " - " + thisEvent.eventParm1; newJob.jobStatus = -99; newJob.jobResult = 0; jobSerial = newJob.Save(); if (jobSerial >= 0) { XLogger(1128, 0, string.Format("Created Job Serial={0}; Name={1}", jobSerial, newJob.jobName)); // Write job config to WorkJobData! try { foreach (KeyValuePair <string, string> kvp in thisJob.config) { saveJobData(jobSerial, kvp.Key.ToString().ToUpper(), kvp.Value); } if (dynConfig != null) { foreach (KeyValuePair <string, string> kvp in dynConfig) { saveJobData(jobSerial, kvp.Key.ToString().ToUpper(), kvp.Value); } } } catch (Exception ex) { XLogger(1157, -1, string.Format("Saving Job Data; Message={0}", ex.Message)); } int position = 0; foreach (XTRMTask thisTask in thisJob.tasks) { rc = newTask.Initialize(-1); newTask.jobSerial = jobSerial; newTask.taskSerial = -1; newTask.taskSequence = position++; //newTask.taskName = thisTask.taskName; newTask.taskName = ResolveParm(thisTask.taskName, thisEvent, thisTask.config, taskSerial); newTask.taskPath = ResolveParm(thisTask.taskPath, thisEvent, thisTask.config, taskSerial); //newTask.taskExecutable = thisTask.taskExecutable; newTask.taskExecutable = ResolveParm(thisTask.taskExecutable, thisEvent, thisTask.config, taskSerial); newTask.taskPID = -1; newTask.taskStatus = 0; newTask.taskResult = 0; newTask.taskEvent = thisEvent.eventSerial; //newTask.taskStart = DateTime.Now; //newTask.taskStop = DateTime.Now; taskSerial = newTask.Save(); if (taskSerial >= 0) { // Must do Parameters! // Need to resolve {{x}} parms! if (thisTask.parms != null) { newTask.parms.Add(newTask.taskName); foreach (string thisParm in thisTask.parms) { //string resolvedParm = ResolveParm(thisParm, thisEvent, thisTask.config); newTask.parms.Add(ResolveParm(thisParm, thisEvent, thisTask.config, taskSerial, jobSerial)); } } //newTask.taskStart = DateTime.Now; //newTask.taskStop = DateTime.Now; taskSerial = newTask.Save(); if (taskSerial >= 0) { XLogger(1125, 0, string.Format("Created Task Serial={0}; Name={1}; Path={2}", taskSerial, newTask.taskName, newTask.taskPath)); } else { XLogger(1126, -1, string.Format("Unable To Create Task; rc={0}", taskSerial)); } } else { XLogger(1155, -1, string.Format("Unable To Create Task; rc={0}", taskSerial)); } } newJob.jobStatus = 0; // Activate Job! jobSerial = newJob.Save(); if (jobSerial < 0) { XLogger(1156, -1, string.Format("Unable To Activate (save) Job; rc={0}", jobSerial)); } } else { XLogger(1127, -1, string.Format("Unable To Create Job; rc={0}", jobSerial)); } return(rc); }
public override int Initialize(int lSerial = -1) { int rc = 0; Clear(); if (pState >= 0) // ONLY if we are using XDB! { // Check the Connection. if (MasterConnection != null) { if (MasterConnection.State != System.Data.ConnectionState.Open) { try { MasterConnection.Open(); } catch (SqlException ex) { XLogger(2300, ex.ErrorCode, string.Format("Serial={0}; Message={1}", lSerial, ex.Message)); rc = ex.ErrorCode; } catch (Exception ex) { XLogger(2301, -1, string.Format("Serial={0}; Message={1}", lSerial, ex.Message)); rc = -1; } } if (MasterConnection.State == System.Data.ConnectionState.Open) { try { myAdapter = new SqlDataAdapter(); // Try to get the Serial (if >= 0). string strTemp = "select * "; strTemp += " from WorkJobs a"; strTemp += " where a.Job_Serial = {0}"; String strSelectCommand = String.Format(strTemp, lSerial); myJobs.Clear(); myCommandBuilder = new SqlCommandBuilder(myAdapter); myAdapter.SelectCommand = new SqlCommand(strSelectCommand, MasterConnection); pState = myAdapter.Fill(myJobs); // add handlers myAdapter.RowUpdating += new SqlRowUpdatingEventHandler(OnRowUpdating); myAdapter.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated); myAdapter.InsertCommand = myCommandBuilder.GetInsertCommand().Clone(); myAdapter.InsertCommand.UpdatedRowSource = UpdateRowSource.Both; myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand().Clone(); myAdapter.DeleteCommand = myCommandBuilder.GetDeleteCommand().Clone(); //myAdapter.InsertCommand.CommandText = String.Concat(myAdapter.InsertCommand.CommandText, // "; SELECT MyTableID=SCOPE_IDENTITY()"); string insertSuffix = "; SELECT Job_Serial=SCOPE_IDENTITY()"; myAdapter.InsertCommand.CommandText += insertSuffix; myAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; //SqlParameter[] aParams = new SqlParameter[myCommandBuilder.GetInsertCommand().Parameters.Count]; //myCommandBuilder.GetInsertCommand().Parameters.CopyTo(aParams, 0); //myCommandBuilder.GetInsertCommand().Parameters.Clear(); SqlParameter identParam = new SqlParameter("@id", SqlDbType.BigInt, 0, "Job_Serial"); identParam.Direction = ParameterDirection.Output; myAdapter.InsertCommand.Parameters.Add(identParam); string test = myAdapter.InsertCommand.Parameters["@id"].ToString(); myAdapter.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord; } catch (SqlException ex) { XLogger(2302, ex.ErrorCode, string.Format("Serial={0}; Message={1}", lSerial, ex.Message)); rc = ex.ErrorCode; } catch (Exception ex) { XLogger(2303, -1, string.Format("Serial={0}; Message={1}", lSerial, ex.Message)); rc = -1; } } else { rc = -1; pState = -1; XLogger(2304, 0, string.Format("Connection Unable to be Opened")); } } else { rc = -1; pState = -1; XLogger(2305, 0, string.Format("No Connection Available")); } try { theseJobs = myJobs.Select(); if (theseJobs.Length > 0) { // Clear Member Values! Clear(); jobSerial = (int)theseJobs[0]["Job_Serial"]; rc = jobSerial; jobHost = (string)theseJobs[0]["Job_Host"]; jobName = (string)theseJobs[0]["Job_Name"]; jobType = (int)theseJobs[0]["Job_Type"]; if (!DBNull.Value.Equals(theseJobs[0]["Job_Start"])) { jobStart = (string)theseJobs[0]["Job_Start"].ToString(); } if (!DBNull.Value.Equals(theseJobs[0]["Job_Stop"])) { jobStop = (string)theseJobs[0]["Job_Stop"].ToString(); } jobSequence = (int)theseJobs[0]["Job_Sequence"]; jobStatus = (int)theseJobs[0]["Job_Status"]; jobResult = (int)theseJobs[0]["Job_Result"]; jobPriority = (int)theseJobs[0]["Job_Priority"]; jobLimit = (int)theseJobs[0]["Job_Limit"]; if (!DBNull.Value.Equals(theseJobs[0]["Job_Display"])) { jobDisplay = (string)theseJobs[0]["Job_Display"]; } jobRetention = (int)theseJobs[0]["Job_Retention"]; jobEvent = (int)theseJobs[0]["Job_Event"]; changeUser = (string)theseJobs[0]["Change_User"]; changeDate = (string)theseJobs[0]["Change_Date"].ToString(); // NEED TO ADD THESE! //if (!DBNull.Value.Equals(theseComponents[0]["Change_Tag"])) //{ // changeTag = (string)theseComponents[0]["Change_Tag"]; //} //if (!DBNull.Value.Equals(theseComponents[0]["Change_State"])) //{ // changeState = (int)theseComponents[0]["Change_State"]; //} XTRMTask thisTask = new XTRMTask(false); //int taskRC = thisTask.Initialize(jobSerial, "select * from WorkTasks a where a.Job_Serial = {0} and Task_Status >= 1 order by Task_Status desc"); int taskRC = thisTask.Initialize(jobSerial, "select * from WorkTasks a where a.Job_Serial = {0} and Task_Status >= 1 order by Task_Result asc"); if (taskRC > 0) { tasks.Add(thisTask); } } } catch (SqlException ex) { rc = ex.ErrorCode; XLogger(2306, ex.ErrorCode, string.Format("Serial={0}; Message={1}", lSerial, ex.Message)); } catch (Exception ex) { rc = -1; XLogger(2307, -1, string.Format("Serial={0}; Message={1}", lSerial, ex.Message)); } } return(rc); }