private List <int> InventoryPendingTasks(XTRMJob thisJob)
        {
            int rc = 0;
            //XTask thisTask = new XTask(true);
            List <int>     pendingTasks = new List <int>();
            SqlDataAdapter eventQuery   = new SqlDataAdapter();

            DataRow[] theseRows = null;
            DataTable thisTable = new DataTable();

            thisTable.CaseSensitive = true;
            try
            {
                SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(eventQuery);
                String            strTemp          = "select * from WorkTasks where Job_Serial = {0} and Task_Status = 0 order by Task_Serial ";
                String            strSelectCommand = String.Format(strTemp, thisJob.jobSerial);
                eventQuery.SelectCommand = new SqlCommand(strSelectCommand, MasterConnection);
                rc = eventQuery.Fill(thisTable);
            }
            catch (Exception ex)
            {
                XLogger(1220, -1, string.Format("InventoryPendingTasks::Exception Getting Tasks (status = 0) from WorkTasks; Message={0}", ex.Message));
            }
            theseRows = thisTable.Select();
            if (theseRows.Length > 0)
            {   // Update!
                for (int i = 0; i < theseRows.Length; i++)
                {
                    int thisTaskSerial = (int)theseRows[i]["Task_Serial"];
                    pendingTasks.Add(thisTaskSerial);
                }
            }
            thisTable.Clear();
            return(pendingTasks);
        }
        private int InventoryRunningJobs(List <XTRMJob> runningJobs)
        {
            int rc = 0;

            //XJob thisJob = new XJob(false);
            //XTask thisTask = new XTask(true);
            runningJobs.Clear();
            SqlDataAdapter eventQuery = new SqlDataAdapter();

            DataRow[] theseRows = null;
            DataTable thisTable = new DataTable();

            thisTable.CaseSensitive = true;
            try
            {
                SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(eventQuery);
                String            strTemp          = "select * from WorkJobs where Job_Status = 1 ";
                String            strSelectCommand = String.Format(strTemp);
                eventQuery.SelectCommand = new SqlCommand(strSelectCommand, MasterConnection);
                rc = eventQuery.Fill(thisTable);
            }
            catch (Exception ex)
            {
                XLogger(1216, -1, string.Format("InventoryRunningJobs::Exception Getting Jobs (status = 1) from WorkJobs; Message={0}", ex.Message));
                rc = -1216;
            }
            theseRows = thisTable.Select();
            if (theseRows.Length > 0)
            {   // Update!
                for (int i = 0; i < theseRows.Length; i++)
                {
                    XTRMJob thisJob       = new XTRMJob(false);
                    int     thisJobSerial = (int)theseRows[i]["Job_Serial"];
                    rc = thisJob.Initialize(thisJobSerial);
                    if (rc >= 0)
                    {
                        rc = InventoryRunningTasks(thisJob);
                        runningJobs.Add(thisJob);
                    }
                    else
                    {
                        // Error
                        XLogger(1217, 0, string.Format("InventoryRunningJobs::thisJob.Initialize() Failure; rc={0}", rc));
                        rc = -1217;
                    }
                }
            }
            thisTable.Clear();
            return(rc);
        }
Beispiel #3
0
        //public Dictionary<String, String> createDictionary()
        //{
        // Populate the Dictionary!
        //    XDictionaryLoader myDictionaryLoader = new XDictionaryLoader();
        //    return myDictionaryLoader.ParseXML();
        //}

        /*
         * public new static XObject consumeXML(Dictionary<String, String> myConfig, string XmlElement, bool bDeep = false)
         * {
         *  //XDictionaryLoader myDictionaryLoader = new XDictionaryLoader();
         *  //myDictionaryLoader.Augment();
         *  //
         *  // Consume XML to create the XComponent object.
         *  // if bDeep is false, then ONLY do this object.
         *  // if bDeep is true, then also do recursive objects.
         *  return new XComponent();
         * }
         * */
        public new static XTRMObject consumeXML(Dictionary <String, String> existingConfig, string XmlFragment, int lVariant = 0, bool bDeep = false)
        {
            //XDictionaryLoader myDictionaryLoader = new XDictionaryLoader();
            //myDictionaryLoader.Augment();
            //
            // Consume XML to create the XComponent object.
            // if bDeep is false, then ONLY do this object.
            // if bDeep is true, then also do recursive objects.
            XmlTextReader               reader   = null;
            XmlParserContext            context  = null;
            Dictionary <String, String> myConfig = new Dictionary <string, string>(existingConfig);
            XTRMJob thisJob = new XTRMJob();

            try
            {
                // Load the reader with the data file and ignore all white space nodes.
                context = new XmlParserContext(null, null, null, XmlSpace.None);
                reader  = new XmlTextReader(XmlFragment, XmlNodeType.Element, context);
                reader.WhitespaceHandling = WhitespaceHandling.None;
                // Parse the file and display each of the nodes.
                bool                        bResult = reader.Read();
                string                      outerXML;
                int                         lElementType       = 0;
                XDictionaryLoader           myDictionaryLoader = new XDictionaryLoader();
                Dictionary <String, String> elementAttributes;
                while (bResult)
                {
                    bool bProcessed = false;
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                        string elementName = reader.Name;
                        switch (elementName.ToUpper())
                        {
                        case "XTRMTask":         // XTRMTask
                            outerXML = reader.ReadOuterXml();
                            XTRMTask thisTask = (XTRMTask)XTRMTask.consumeXML(myConfig, outerXML, 1, true);
                            thisJob.tasks.Add(thisTask);
                            bProcessed = true;
                            break;
                        }
                        // May wish to get all the  attributes here for new elements!
                        if (!bProcessed)
                        {
                            elementAttributes = new Dictionary <String, String>();
                            if (reader.HasAttributes)
                            {
                                reader.MoveToFirstAttribute();
                                for (int i = 0; i < reader.AttributeCount; i++)
                                {
                                    //reader.GetAttribute(i);
                                    elementAttributes.Add(reader.Name, reader.Value);
                                    reader.MoveToNextAttribute();
                                }
                                if (elementAttributes.ContainsKey("Name"))
                                {
                                    thisJob.jobName = elementAttributes["Name"];
                                }
                                if (elementAttributes.ContainsKey("Host"))
                                {
                                    thisJob.jobHost = elementAttributes["Host"];
                                }
                                reader.MoveToElement();
                            }
                            // Need to see if we are interested in this element!
                            switch (elementName.ToUpper())
                            {
                            //case "XTRMTask": // XTRMTask
                            //    outerXML = reader.ReadOuterXml();
                            //    XTRMTask thisTask = (XTRMTask)XTRMTask.consumeXML(myConfig, outerXML, 1, true);
                            //    thisJob.tasks.Add(thisTask);
                            //    break;
                            // Reset Dictionary!
                            // XConfig
                            case "BASECONFIG":
                                outerXML           = reader.ReadOuterXml();
                                myDictionaryLoader = new XDictionaryLoader();
                                myConfig           = new Dictionary <string, string>();
                                myDictionaryLoader.Augment(myConfig, outerXML);
                                break;

                            //   Add to the current dictionary!
                            // XConfig
                            case "WITHCONFIG":
                                outerXML = reader.ReadOuterXml();
                                //myDictionaryLoader = new XDictionaryLoader();
                                myDictionaryLoader.Augment(myConfig, outerXML);
                                break;

                            default:
                                bResult = reader.Read();
                                break;
                            }
                        }
                        break;

                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.ProcessingInstruction:
                        //writer.WriteProcessingInstruction(reader.Name, reader.Value);
                        bResult = reader.Read();
                        break;

                    case XmlNodeType.Comment:
                        //writer.WriteComment(reader.Value);
                        bResult = reader.Read();
                        break;

                    case XmlNodeType.EndElement:
                        //writer.WriteFullEndElement();
                        bResult = reader.Read();
                        break;

                    case XmlNodeType.Text:
                        //Console.Write(reader.Value);
                        switch (lElementType)
                        {
                        //case 1:     // PARMS
                        //thisTask.parms.Add(reader.Value);
                        default:
                            break;
                        }
                        bResult = reader.Read();
                        break;

                    default:
                        bResult = reader.Read();
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                exCount_XML++;
                XLogger(2320, -1, string.Format("XML={0}; Message={1}", XmlFragment, ex.Message));
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            thisJob.config = myConfig;
            return(thisJob);
        }
Beispiel #4
0
        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);
        }
        // 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);
        }
Beispiel #6
0
        //public Dictionary<String, String> createDictionary()
        //{
        // Populate the Dictionary!
        //    XDictionaryLoader myDictionaryLoader = new XDictionaryLoader();
        //    return myDictionaryLoader.ParseXML();
        //}
        /*
        public new static XObject consumeXML(Dictionary<String, String> myConfig, string XmlElement, bool bDeep = false)
        {
            //XDictionaryLoader myDictionaryLoader = new XDictionaryLoader();
            //myDictionaryLoader.Augment();
            //
            // Consume XML to create the XComponent object.
            // if bDeep is false, then ONLY do this object.
            // if bDeep is true, then also do recursive objects.
            return new XComponent();
        }
         * */
        public static new XTRMObject consumeXML(Dictionary<String, String> existingConfig, string XmlFragment, int lVariant = 0, bool bDeep = false)
        {
            //XDictionaryLoader myDictionaryLoader = new XDictionaryLoader();
            //myDictionaryLoader.Augment();
            //
            // Consume XML to create the XComponent object.
            // if bDeep is false, then ONLY do this object.
            // if bDeep is true, then also do recursive objects.
            XmlTextReader reader = null;
            XmlParserContext context = null;
            Dictionary<String, String> myConfig = new Dictionary<string, string>(existingConfig);
            XTRMJob thisJob = new XTRMJob();

            try
            {
                // Load the reader with the data file and ignore all white space nodes.
                context = new XmlParserContext(null, null, null, XmlSpace.None);
                reader = new XmlTextReader(XmlFragment, XmlNodeType.Element, context);
                reader.WhitespaceHandling = WhitespaceHandling.None;
                // Parse the file and display each of the nodes.
                bool bResult = reader.Read();
                string outerXML;
                int lElementType = 0;
                XDictionaryLoader myDictionaryLoader = new XDictionaryLoader();
                Dictionary<String, String> elementAttributes;
                while (bResult)
                {
                    bool bProcessed = false;
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            string elementName = reader.Name;
                            switch (elementName.ToUpper())
                            {
                                case "XTRMTask": // XTRMTask
                                    outerXML = reader.ReadOuterXml();
                                    XTRMTask thisTask = (XTRMTask)XTRMTask.consumeXML(myConfig, outerXML, 1, true);
                                    thisJob.tasks.Add(thisTask);
                                    bProcessed = true;
                                    break;
                            }
                            // May wish to get all the  attributes here for new elements!
                            if (!bProcessed)
                            {
                                elementAttributes = new Dictionary<String, String>();
                                if (reader.HasAttributes)
                                {
                                    reader.MoveToFirstAttribute();
                                    for (int i = 0; i < reader.AttributeCount; i++)
                                    {
                                        //reader.GetAttribute(i);
                                        elementAttributes.Add(reader.Name, reader.Value);
                                        reader.MoveToNextAttribute();
                                    }
                                    if (elementAttributes.ContainsKey("Name"))
                                    {
                                        thisJob.jobName = elementAttributes["Name"];
                                    }
                                    if (elementAttributes.ContainsKey("Host"))
                                    {
                                        thisJob.jobHost = elementAttributes["Host"];
                                    }
                                    reader.MoveToElement();
                                }
                                // Need to see if we are interested in this element!
                                switch (elementName.ToUpper())
                                {
                                    //case "XTRMTask": // XTRMTask
                                    //    outerXML = reader.ReadOuterXml();
                                    //    XTRMTask thisTask = (XTRMTask)XTRMTask.consumeXML(myConfig, outerXML, 1, true);
                                    //    thisJob.tasks.Add(thisTask);
                                    //    break;
                                    // Reset Dictionary!
                                    // XConfig
                                    case "BASECONFIG":
                                        outerXML = reader.ReadOuterXml();
                                        myDictionaryLoader = new XDictionaryLoader();
                                        myConfig = new Dictionary<string, string>();
                                        myDictionaryLoader.Augment(myConfig, outerXML);
                                        break;
                                    //   Add to the current dictionary!
                                    // XConfig
                                    case "WITHCONFIG":
                                        outerXML = reader.ReadOuterXml();
                                        //myDictionaryLoader = new XDictionaryLoader();
                                        myDictionaryLoader.Augment(myConfig, outerXML);
                                        break;
                                    default:
                                        bResult = reader.Read();
                                        break;
                                }
                            }
                            break;
                        case XmlNodeType.XmlDeclaration:
                        case XmlNodeType.ProcessingInstruction:
                            //writer.WriteProcessingInstruction(reader.Name, reader.Value);
                            bResult = reader.Read();
                            break;
                        case XmlNodeType.Comment:
                            //writer.WriteComment(reader.Value);
                            bResult = reader.Read();
                            break;
                        case XmlNodeType.EndElement:
                            //writer.WriteFullEndElement();
                            bResult = reader.Read();
                            break;
                        case XmlNodeType.Text:
                            //Console.Write(reader.Value);
                            switch (lElementType)
                            {
                                //case 1:     // PARMS
                                    //thisTask.parms.Add(reader.Value);
                                default:
                                    break;
                            }
                            bResult = reader.Read();
                            break;
                        default:
                            bResult = reader.Read();
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                exCount_XML++;
                XLogger(2320, -1, string.Format("XML={0}; Message={1}", XmlFragment, ex.Message));
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
            thisJob.config = myConfig;
            return thisJob;
        }
Beispiel #7
0
        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;
        }
Beispiel #8
0
        // 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++;
                                            // What was this condition originally???
                                            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();
                                            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();
                                        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;
        }
Beispiel #9
0
 // XSelectRunTasks
 private int InventoryRunningTasks(XTRMJob thisJob)
 {
     int rc = 0;
     //XTask thisTask = new XTask(false);
     SqlDataAdapter eventQuery = new SqlDataAdapter();
     DataRow[] theseRows = null;
     DataTable thisTable = new DataTable();
     thisTable.CaseSensitive = true;
     try
     {
         SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(eventQuery);
         String strTemp = "select * from WorkTasks where Job_Serial = {0} and Task_Status = 1 order by Task_Serial ";
         String strSelectCommand = String.Format(strTemp, thisJob.jobSerial);
         eventQuery.SelectCommand = new SqlCommand(strSelectCommand, MasterConnection);
         rc = eventQuery.Fill(thisTable);
     }
     catch (Exception ex)
     {
         XLogger(1218, -1, string.Format("InventoryRunningTasks::Exception Getting Tasks (status = 1) from WorkTasks; Message={0}", ex.Message));
     }
     theseRows = thisTable.Select();
     if (theseRows.Length > 0)
     {   // Update!
         for (int i = 0; i < theseRows.Length; i++)
         {
             XTRMTask thisTask = new XTRMTask();
             int thisTaskSerial = (int)theseRows[i]["Task_Serial"];
             rc = thisTask.Initialize(thisTaskSerial);
             if (rc >= 0)
             {
                 thisJob.tasks.Add(thisTask);
             }
             else
             {
                 // Error
                 XLogger(1219, 0, string.Format("InventoryRunningTasks::thisTask.Initialize() Failure; rc={0}", rc));
             }
         }
     }
     thisTable.Clear();
     return rc;
 }
Beispiel #10
0
 // XSelectPendTasks
 private List<int> InventoryPendingTasks(XTRMJob thisJob)
 {
     int rc = 0;
     //XTask thisTask = new XTask(true);
     List<int> pendingTasks = new List<int>();
     SqlDataAdapter eventQuery = new SqlDataAdapter();
     DataRow[] theseRows = null;
     DataTable thisTable = new DataTable();
     thisTable.CaseSensitive = true;
     try
     {
         SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(eventQuery);
         String strTemp = "select * from WorkTasks where Job_Serial = {0} and Task_Status = 0 order by Task_Serial ";
         String strSelectCommand = String.Format(strTemp, thisJob.jobSerial);
         eventQuery.SelectCommand = new SqlCommand(strSelectCommand, MasterConnection);
         rc = eventQuery.Fill(thisTable);
     }
     catch (Exception ex)
     {
         XLogger(1220, -1, string.Format("InventoryPendingTasks::Exception Getting Tasks (status = 0) from WorkTasks; Message={0}", ex.Message));
     }
     theseRows = thisTable.Select();
     if (theseRows.Length > 0)
     {   // Update!
         for (int i = 0; i < theseRows.Length; i++)
         {
             int thisTaskSerial = (int)theseRows[i]["Task_Serial"];
             pendingTasks.Add(thisTaskSerial);
         }
     }
     thisTable.Clear();
     return pendingTasks;
 }