Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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++;
                                        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);
        }
Exemplo n.º 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;
        }
Exemplo n.º 5
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;
        }
Exemplo n.º 6
0
 // XSelectRunJobs
 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();
             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;
 }